Passing variable values ​​from another method

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) { // 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."); } } 

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); // 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 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.

+4
source share
3 answers

There are several problems with the code. First, the result you see is the default implementation product toString() , which is in the Object class (from which all classes, including GetFractions , are ultimately derived. Override this method to return a string representation of your instances:

 @Override public String toString() { return ... } public static void main(String[] args) { ... System.out.println("..." + newFracNum1 + "..."); } 

or instead of passing the instance to System.out.println() pass the result of calling the member access method (these methods are called getters):

 public double getSomeValue() { return ... } public static void main(String[] args) { ... System.out.println("..." + newFracNum1.getSomeValue() + "..."); } 

Note that double and other primitive types are automatically converted to strings.

Secondly, your static GetFractions() method changes its arguments, which are inefficient (no one will see the changes since they are passed by value). The method must either modify the instance variables of the same name with the same instance, and then it must not be static or it must be a factory method that creates new instances based on data provided by the user, in which case it passed the values ​​to the constructor or it should be the constructor itself. In any case, you do not want to change the parameters of the method. Here is a diagram of three solutions:

A non-static method that reads data from an input stream:

 public void fromStream(InputStream inStream) { // Read data from inStream into instance variables fracNum1, fracDenom1,... } 

Static factory method:

 public static GetFractions fromStream(InputStream inStream) { int fracNum1,... ; // Read data from inStream into local variables fracNume1, ... return new GetFractions(fracNum1, ...); } 

Constructor:

 public GetFractions(InputStream inStream) { // Read data from inStream to initialize instance variables fracNum1, fracDenom1,... } 

Note that passing an InputStream to all of these methods provides more flexibility than hardcoding System.in .

Third, you should review your naming convention. Calling a static method similar to a class, although possible, is usually bad practice. In addition, it is advisable to call classes and objects with nouns and methods with the expression of the verb. This helps develop your classes and makes the code more readable. GetFractions more suitable as the name of a method rather than a class. Fractions would make a better class name.

+3
source

The reason it prints the 0th after fixing the code is because you actually never call the static GetFractions method, which is NOT a constructor.
Never name a method similar to a constructor, perhaps only if it is a static factory method.

Also with the original code, you printed GetFractions objects causing a toString call that you did not overwrite.
Also note that calls to get ... have no effect, since return values ​​are not stored anywhere.

+1
source

your code implicitly calls the toString () method of the GetFractions class. Since you did not overwrite it, this is the toString () method from the object (superclass). The toString () method from objects returns soemthing as a class name plus hashcode (see http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString%28%29 ) . what you see on your way out.

You have two options: overwrite toString or change your code, for example, the mentioned Eng.Fouad.

+1
source

Source: https://habr.com/ru/post/1383237/


All Articles