Java Programs


Seconds to Hour, Minutes and Seconds


  • Program that accepts input of a number of seconds, validates it and outputs the equivalent number of hours, minutes and seconds.

Program

import java.util.Scanner;

public class SecondsToTime {

            private static Scanner sc;
            public static void main(String[] args) {
                        int time,sec,min,hour;
                        sc=new Scanner(System.in);
                        System.out.println("Enter the seconds:");
                        time=sc.nextInt();
                        sec=time%60;
                        min=((time%3600)-sec)/60;
                        hour=time/3600;
                        System.out.println("Time is:\n"+hour+" hours "+min+" minutes "+sec+" seconds");

            }

}

Output

Enter the seconds:
9999
Time is:
2 hours 46 minutes 39 seconds


Add or Multiply Two Fractions


  • Program that can either add or multiply two fractions. The two fractions and the operation to be performed are taken as input and the result is displayed as output.

Program

import java.io.IOException;
import java.util.Scanner;

public class AddMulFractions {

            private static Scanner sc;
            public static String x,y;
            public void multiply(int x1,int y1,int x2,int y2) {
                       
                        int gcd;
                        x1=x1*x2;
                        y1=y1*y2;
                        AddMulFractions ob=new AddMulFractions();
                        gcd=ob.reduce(x1,y1);
                        x1=x1/gcd;
                        y1=y1/gcd;
                        System.out.println("The product of "+x+" and "+y+" is "+x1+"/"+y1);
            }
            public static int lcm(int y1,int y2) {
                        if(y2>y1) {
                                    int temp=y1;
                                    y1=y2;
                                    y2=temp;
                        }
                        for(int i=1;i<=y2;i++) {
                                    if((y1*i)%y2==0) {
                                                return (i*y1);
                                    }
                        }
                        throw new Error("Error!!");
            }
            public void add(int x1,int y1,int x2,int y2) {
                       
                        int l,sum;
/*If the denominator<0, then multiply both denominator and numerator with -1
so that the denominator always remains positive, making it easier to find the LCM*/
                        if(y1<0) {
                                    x1=x1*-1;
                                    y1=y1*-1;
                        }
                        if(y2<0) {
                                    x2=x2*-1;
                                    y2=y2*-1;
                        }
                        l=AddMulFractions.lcm(y1,y2);//Calculate the LCM of the denominators and assign it to l.
                        x1=x1*(l/y1);
                        x2=x2*(l/y2);
                        sum=x1+x2;
                        AddMulFractions ob= new AddMulFractions();
                        int gcd=ob.reduce(sum, l);//reducing the fraction to much simpler form
                        sum=sum/gcd;
                        l=l/gcd;
                        System.out.println("The sum of "+x+" and "+y+" is "+sum+"/"+l);
            }
            // reduce() function calculates the greatest common factor(gcd)
            public int reduce(int n,int d) {
                       
                        int temp;
                        while(d!=0) {
                                    temp=d;
                                    d=n%d;
                                    n=temp;
                        }
                        //If gcd is less than zero, convert it to a positive value
                        //so that the numerator remains negative and the denominator remains positive.
                        if(n<0) {
                                    n=n*-1;
                        }
                        return n;
            }
            public static void main(String[] args) throws IOException {
                       
                        int choice,len=0,num1,den1,num2,den2;
                        char proceed;
                        sc=new Scanner(System.in);
                        AddMulFractions ob=new AddMulFractions();
                        do {
                        System.out.println("Enter your choice:\n1.Addition\n2.Multiplication");
                        choice=sc.nextInt();
                        System.out.println("Enter the first fraction (numerator/denominator)");
                        x=sc.next();
                        //Extracting the numerator and denominator of the first fraction
                        for(int i=1;x.charAt(i)!='/';i++) {
                                    len++;
                        }
                        num1=Integer.valueOf(x.substring(0,len+1));
                        den1=Integer.valueOf(x.substring(len+2, x.length()));
                        if(den1==0) {
                                    System.out.println("Denominator cannot be zero");
                                    return;
                        }
                        len=0;
                        System.out.println("Enter the second fraction (numerator/denominator)");
                        y=sc.next();
                        //Extracting the numerator and denominator of the second fraction
                        for(int i=1;y.charAt(i)!='/';i++) {
                                    len++;
                        }
                        num2=Integer.valueOf(y.substring(0,len+1));
                        den2=Integer.valueOf(y.substring(len+2, y.length()));
                        if(den2==0) {
                                    System.out.println("Denominator cannot be zero");
                                    return;
                        }
                        switch(choice) {
                       
                        case 1: ob.add(num1,den1,num2,den2);
                                                break;
                        case 2: ob.multiply(num1,den1,num2,den2);
                                                break;
                        default:           System.out.println("Invalid choice");
                                                            break;
                        }
                        System.out.println("Do you want to continue:(y/n)?");
                        proceed=(char) System.in.read();
                        }while(proceed=='Y'||proceed=='y');
                       
            }

}

Output

Enter your choice:
1.Addition
2.Multiplication
1
Enter the first fraction (numerator/denominator)
3/8
Enter the second fraction (numerator/denominator)
6/17
The sum of 3/8 and 6/17 is 99/136
Do you want to continue:(y/n)?
y
Enter your choice:
1.Addition
2.Multiplication
2
Enter the first fraction (numerator/denominator)
3/8
Enter the second fraction (numerator/denominator)
6/17
The product of 3/8 and 6/17 is 9/68
Do you want to continue:(y/n)?
n


No comments:

Post a Comment