-2

The purpose of the program is to get two user inputs for a fraction, receive a operator from the user, and then to get two more user inputs for a second fraction. The program must check that the numbers used in both fractions range between 0-99 and have a non-zero denominator. The program also has to make sure that the user inputs a valid operator (-,+,*,/).

UPDATE: The only problem I am facing now is that none of my variables are being initialized and that I don't know how to make the output look like so:

   1     1     3
  --- + --- = ---
   8     4     8

This is the code I have so far, I am a beginner in programming and any help would be much appreciated:

import java.util.Scanner;
public class FractionCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int n1;
int n2;
int d1;
int d2;
int n;
int d;
char o;
int m1,m2; 
int tempN1, tempN2;
int lcm, x;

System.out.println("Enter a numerator for fraction 1: ");
n1 = in.nextInt();
System.out.println("Enter a denominator for fraction 1: ");
d1 = in.nextInt();

if  (d1 > 0) {
  System.out.println(); 
} else {
  System.out.println("Invalid denominator");
  System.exit(0); 
}

System.out.println("Enter an operator: ");
o = in.next().toCharArray()[0]; 
System.out.println("Enter a numerator for fraction 2: ");
n2 = in.nextInt();
System.out.println("Enter a denominator for fraction 2: ");
d2 = in.nextInt();

if (d2 > 0) {
  System.out.println(); 
} else {
  System.out.println("Invalid denominator");
  System.exit(0); 
}


switch(o){
    case '*':
        n = n1 * n2;
        d = d1 * d2;
        break;

    case '/':
        n = n1 * d2;
        d = n2 * d1;
        break;

    case '+':
        int max=n1>d1?n1:d1;
        int min=n1<d1?n1:d1; 
        for(int i=1;i<=min;i++)
            x=max*i;
            if (x%min==0)
            lcm=x;      
        tempN1=n1*m1;
        tempN2=n2*m2;
        m1=lcm/d1;
        m2=lcm/d2;
        n = tempN1 + tempN2;
        d = lcm;
        break; 

    case '-':
        n = tempN1 - tempN2;
        d = lcm;
        break;
    default:
        System.out.println("Illegal Operator: "+ o);
        break; }
 }
}
Joe Eastman
  • 69
  • 2
  • 5
  • 10
  • I'd say use a switch/case statement to insure valid operator input. – retriever123 Oct 06 '14 at 19:17
  • I'm supposed to if statements and it's throwing me off because I agree, I want to be using switch statements – Joe Eastman Oct 06 '14 at 19:22
  • not as neat, but ok... if, else if, else will work fine to match the symbol to a math method() or an error if the input is invalid. http://www.homeandlearn.co.uk/java/java_if_else_statements.html – retriever123 Oct 06 '14 at 19:29
  • You're getting a few downvotes, that's probably because it looks like you're asking SO to do you're homework for general questions that you could easily research. You'll have better luck if you make a FULL ATTEMPT to solve it and then list what bugs you're having. – retriever123 Oct 06 '14 at 19:34
  • My knowledge for computer programming is extremely minimal and what I have so far for code took me a whole 3 hours to come up with, I put effort in and made an attempt but since my knowledge is so minimal even researching is hard if you don't know how to use the components you're given from the research.. – Joe Eastman Oct 06 '14 at 20:02
  • That's why I gave you the benefit of the doubt and suggested things you could look up. but I wanted to mention a possible explanation for the downvotes and how to avoid them. I hope you ignore the completed code answers and use hints to do the work. – retriever123 Oct 06 '14 at 20:49

3 Answers3

2

Java's an object-oriented language. You shouldn't attempt this problem without a Rational or Fraction class with methods for abstracting the operations for addition, subtraction, multiplication, division, etc.

Timothy Budd's "Data Structures In C++" has a fine example of how to do it properly. It uses Euclid's GCD to automatically transform 3/12 to 1/4, etc.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

Welcome to Java/SO/Programming as a whole! You've picked a fun problem to learn on.

Let's look at checking/working on the operator. Notably, all of your valid operators (+, -, *, /) are a single character. So instead of having o be a string, let's have it be a char (because presumably if the user inputs more than one character for their operator, it's wrong - let's just take the first character they enter).

Once we have all of the inputs, we can use a switch statement on the operator to determine both if it's valid and what to actually do.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int a, b, c, d, e, f;
    char o;
    System.out.println("Enter a numerator for fraction 1: ");
    a = in.nextInt();
    System.out.println("Enter a denominator for fraction 1: ");
    b = in.nextInt();

    if  (b > 0) {
      System.out.println(); 
    } else {
      System.out.println("Invalid denominator");
      System.exit(0); 
    }

    System.out.println("Enter an operator: ");
    o = in.next().toCharArray()[0]; //Take the first character of the string entered by user
    System.out.println("Enter a numerator for fraction 2: ");
    c = in.nextInt();
    System.out.println("Enter a denominator for fraction 2: ");
    d = in.nextInt();

    if (d > 0) {
      System.out.println(); 
    } else {
      System.out.println("Invalid denominator");
      System.exit(0); 
    }

    switch(o){
        case '*':
            //Do multiplication..
            break;
        case '/':
            //Do Division..
            break;
        case '+':
            //Do addition..
            break;
        case '-':
            //Do subtraction..
            break;
        default:
            System.out.println("Illegal Operator: "+ o);
            break;
     }
  }
Mshnik
  • 7,032
  • 1
  • 25
  • 38
0

Here goes the logic.

Let the numbers be
  n1, n2, d1, d2, n, d;

For addition and subtraction, find the L.C.M of the denominators and then simply add or subtract the numerators.

Here, d=LCM(d1,d2)
Make changes to the numerator while computing LCM
n=n1+n2; or n=n1-n2;

For multiplication,

n=n1*n2;
d=d1*d2;

For Division,

n=n1*d2;
d=n2*d1;

Code to find the LCM,

int max=a>b?a:b;
int min=a<b?a:b;
int lcm, x; 
for(int i=1;i<=min;i++)
{
    x=max*i; //finding multiples of the maximum number

    if(x%min==0) //Finding the multiple of maximum number which is divisible by the minimum number.
    {
        lcm=x; //making the 1st multiple of maximum number as lcm, which is divisible by the minimum number
        break; //exiting from the loop, as we don’t need anymore checking after getting the LCM
    }
 }

Making changes to the numerator...

int m1,m2; //value to be multiplied to the numerators 
m1=lcm/d1;
m2=lcm/d2;

int tempN1, tempN2; //So that original values remain intact for displaying the result
tempN1=n1*m1;
tempN2=n2*m2;

Adding or subtracting

n=tempN1+tempN2;  //n=tempN1-tempN2; in case of subtraction
d=lcm;
Sunil Chawla
  • 351
  • 3
  • 6