I've been working through this problem for several hours, and I've made significant progress (thanks in large part to searching this site and applying tips found in similar questions) but I now seem to be at an impasse. Please take a look through what I have done and either point out where I've gone wrong and provide psuedo code to correct, or point me to a resource that can help me by filling in the gap of my understanding. I really feel as though I'm just missing a tiny detail that will make this topic make sense to me.
The purpose of the application is to add, subtract, multiply, and divide fractions based on user input of 2 numerators and 2 denominators (yes, this is for a course assignment so please don't give source code but rather pointers on where I've gone wrong conceptually). I've broken it down into steps, the first of which is getting the user's input and outputting it back for confirmation.
Method file:
package Fractions;
import java.util.Scanner;
public class FractionValues
{
// Declare integer class variables for package Fractions
int fracNum1;
int fracDenom1;
int fracNum2;
int fracDenom2;
// Obtain four integers from user input and output to console as two fractions
public static void getFractions(int fracNum1, int fracDenom1, int fracNum2, int fracDenom2)
{
Scanner inInt = new Scanner(System.in);
System.out.println("Enter an integer for the numerator of the first " +
"fraction: ");
fracNum1 = inInt.nextInt();
System.out.println("Enter an integer for the denominator of the first " +
"fraction: ");
fracDenom1 = inInt.nextInt();
System.out.println("Enter an integer for the numerator of the second " +
"fraction: ");
fracNum2 = inInt.nextInt();
System.out.println("Enter an integer for the denominator fo the second " +
"fraction: ");
fracDenom2 = inInt.nextInt();
System.out.println("===================================================" +
"=================");
}
// Return values of variables from input for use in other classes
public int getFracNum1() {return fracNum1;}
public int getFracDenom1() {return fracDenom1;}
public int getFracNum2() {return fracNum2;}
public int getFracDenom2() {return fracDenom2;}
}
main Method file:
package Fractions;
public class TestFractions2
{
public static void main(String[] args)
{
// Call getFractions method to assign variables from user input
FractionValues newFracNum1 = new FractionValues();
newFracNum1.getFracNum1();
FractionValues newFracDenom1 = new FractionValues();
newFracDenom1.getFracDenom1();
FractionValues newFracNum2 = new FractionValues();
newFracNum2.getFracNum2();
FractionValues newFracDenom2 = new FractionValues();
newFracDenom2.getFracDenom2();
System.out.println("You entered " + newFracNum1.getFracNum1() + "/" + newFracDenom1.getFracDenom2() + " and " +
newFracNum2.getFracNum2() + "/" + newFracDenom2.getFracDenom2() + " as your fractions.");
}
}
At least after some struggles, both files now compile. However, the application doesn't work. This is the output I get:
You entered 0/0 and 0/0 as your fractions.
Once this part works, I'll be adding an if statement prompting the user to respond whether they wish to continue with their choices, or return to the entry prompts.
Based on the valuable feedback below and the constraints of the assignment, I've gotten the following:
package Fractions;
import java.util.Scanner;
public class FractionValues
{
int fracNum1;
int fracDenom1;
int fracNum2;
int fracDenom2;
Scanner inInt = new Scanner(System.in);
// Obtain four integers from user input
public int getFracNum1()
{
System.out.println("Enter an integer for the first numerator: ");
return fracNum1 = inInt.nextInt();
}
public int getFracDenom1()
{
System.out.println("Enter an integer for the first denominator: ");
return fracDenom1 = inInt.nextInt();
}
public int getFracNum2()
{
System.out.println("Enter an integer for the second numerator: ");
return fracNum2 = inInt.nextInt();
}
public int getFracDenom2()
{
System.out.println("Enter an integer for the second denominator: ");
return fracDenom2 = inInt.nextInt();
}
}
and the main application method:
package Fractions;
public class TestFractions2
{
public static void main(String[] args)
{
// Call FractionValues methods to assign variables from user input
FractionValues newFracNum1 = new FractionValues();
FractionValues newFracDenom1 = new FractionValues();
FractionValues newFracNum2 = new FractionValues();
FractionValues newFracDenom2 = new FractionValues();
System.out.println("You entered " + newFracNum1.getFracNum1() + "/" +
newFracDenom1.getFracDenom2() + " and " + newFracNum2.getFracNum2() +
"/" + newFracDenom2.getFracDenom2() + " as your fractions.");
}
}
Both files compile properly, and I get the following expected output:
Enter an integer for the first numerator:
2
Enter an integer for the second denominator:
5
Enter an integer for the second numerator:
6
Enter an integer for the second denominator:
3
You entered 2/5 and 6/3 as your fractions.
Thank you very much for your help with methods and constructors, and constructive comments on naming conventions. The places your comments led me have very likely taken me from failing my exam to acing it! I've been struggling with this concept for weeks, even with the help of a very patient friend.
FractionorRationalclass with a numerator and denominator, and perhaps I/O routines.