Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

fraction math

$
0
0
I am writing the code that will add, subtract, multiply, and divide fractions.
I am having trouble getting my methods correct. The user enter the numbers for the fractions
num1 and num2
den1 and den2

then the user chooses from a menu.. to apply the function to the fractions.
The errors are in the print statements and the return statement from each method.


import java.util.Scanner;

public class Fractions 
{

    
    public static void main (String arg[])  
    {
System.out.println ("This program will do basic math with fractions\n");
Scanner input = new Scanner (System.in);

int num1=0;
int dem1=0;
int num2=0;
int den2=0;
int choice;

 System.out.printf ("please enter the numerator for the first fraction\n");
num1 = input.nextInt();

System.out.print("please enter the demoninator for the first fraction\n");
dem1 =input.nextInt();

System.out.print("Please enter the numerator for the second fraction\n");
num2 = input.nextInt();

System.out.print ("please enter the demoninator for the Second fraction\n");
dem2 = input.nextInt();

System.out.print(" choose what you would like to do to the fractons\n");
System.out.println("1 add fractions\n");
System.out.println("2 subtract fraction\n");
System.out.println("3 divid fraction\n");
System.out.print("4 multiply fraction\n");
choice =input.nextInt();
 
switch(choice)
{
    case 1:
System.out.printf ("the fractions add to gether are %d , plus", addfract (int num1,int dem1, int num2, int dem2):
       
        break;  
    case 2:
System.out.printf ("the fractions subtracted to gether are", subfract (int num1,int dem1,int num2 int dem2,);   
break; 
        
    case 3:
    System.out.printf ("the fractions muliplied to gether are", timesfract (int num1,int dem1,int num2),
         
        break;
        
    case 4:
        System.out.printf("the fractions devided togeter are",dividefract( int num1, int den1,int num2,int den2))
}
}
public static int addfract (int num1,int den1,int num2, int den2)
        {
        int f1; // answer fraction num
        int f2; // answer fraction dem
        int x = num1 * den2; // first fraction 
        int y = den1 * den2;  // first fraction dem
        int f = num2 * den1; // second fraction num
        int a = den2 * den1; // second fraction dem
        f1 = x + y; // adding fraction 
        f2 = f + a; // keeping the same den
            return f1, f2;
    }
public static int subfract(int num1,int den1,int num2,int den2)
{
    int f1; // answer fraction num
    int f2; // answer fraction den
    int x = num1 * den2;// first fraction num
    int y = den1 * den2;// first fraction den
    int f = num2 * den1;// second fraction num
    int a = den2 * den1;// second fraction den
    f1 = x - y; // subtracting fraction
    f1 = f + a; // keeping the same den
              
    return f1, f2;
            }
public static int timesfract(int num1,int den1,int num2,int den2)
{
int tnum;
int tdem;
tnum = num1 *num2;
tden = dem1 * den2;

return tnum tden;
            }

public static int dividefract(int num1,int dem1,int num2,int den2)
{
    
int tnum = num1 * den2;
int tdem = dem1 * den2;
return tnum tden;
            }
}




Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>