I have been working on this issue for several hours, and I have made significant progress (largely due to the search for this site and the application of the tips found in similar issues), but now I seem to be at a dead end. Please take a look at what I did, and indicate where I made a mistake, and give the alias code to fix, or indicate to me a resource that can help me by filling in the gap in my understanding. It really seems to me that 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 the user entering two numerators and two denominators (yes, this is for setting the course, so please do not indicate the source code, but rather pointers to where I was wrong conceptually). I broke it down into steps, the first of which receives user input and displays 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) {
After at least some fighting, both files now compile. However, the application does not work. This is the result I get:
You entered 0/0 and 0/0 as your fractions.
As soon as this part works, I will add an if statement that prompts the user to answer whether they want to continue the selection or return to the input prompts.
Based on the valuable feedback below and destination restrictions, I got 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);
and the main application method:
package Fractions; public class TestFractions2 { public static void main(String[] args) {
Both files compiled correctly, 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 for your help with the methods and constructors, as well as for the constructive comments on naming conventions. The places where your comments led me most likely made me fail to pass the exam to handle this! I struggled with this concept for several weeks, even with the help of a very patient friend.