posted 1 year ago

Greeting all. I am hoping you will be able to clarify something for me.

Just a little information so you know what is going on:

First, I am calling my getAverage method in the FractionsArrays class from the main menu:

FractionsArrays class method:

From here, I am calling my Fractions class within the for loop so that I can add many fractions. In my fraction class I created a default constructor to assign something to the numerator or denominator in case a value was not provided and an explicit as well.

The problem is here:

As you can see in the results, the fraction located at index array[0] which I stored in my instance of the class

the keyword

The first fraction is 5/4 and the addition of that one plus 7/8 is 17/8. The second time the for loop "loops", the

Just a little information so you know what is going on:

First, I am calling my getAverage method in the FractionsArrays class from the main menu:

FractionsArrays class method:

From here, I am calling my Fractions class within the for loop so that I can add many fractions. In my fraction class I created a default constructor to assign something to the numerator or denominator in case a value was not provided and an explicit as well.

The problem is here:

As you can see in the results, the fraction located at index array[0] which I stored in my instance of the class

**answer**works fine. When I invokethe keyword

*this*works fine the first time. After it loops, the*this*does not work for answer anymore.The first fraction is 5/4 and the addition of that one plus 7/8 is 17/8. The second time the for loop "loops", the

*this*for**answer**does not read my 17/8 anymore. It uses the value of 5 and 1 that I gave to the default constructor. Why? I hope I was clear enough in my explanation
Norm Radder

Rancher

Posts: 2240

28

posted 1 year ago

Hi Daniel,can you please show us your full clean code.then it would be easy to locate the error or problem because i really found it difficult to get what actually is going in your add method or in some other part acc. To your given information.

Kind regards,

Praveen

Kind regards,

Praveen

Try to enjoy your work while doing it,it will Automatically convert in Hard Work...

posted 1 year ago

I think I can't do that. The programs are a bit extensive and it would be hard to simplify it to show only the part where there is a problem. I will show the code that is relevant to the issue. The add method contains another method within to get the greatest common divisor. I don't include it because I don't want people to try and analyze it as it takes a while to go line by line to know what is going on. The method works fine which is why I want you guys to just focus on where the problem is.

The second time the for loop in the FractionsArrays calls the add method in the Fractions class it does not use the value in numerator and denominator currently in the array

This is the FractionsArrays class:

In the add method the first time is called, the fraction located in answer[0] which is 5/4 is added to 7/8 which is value in (array[x]);. This.numerator is 5 and this.denominator is 4. The owner of this is answer from answer = answer.add(array[x]) The second time it loops is the problem as you can see. It should be 17/8 plus 9/8 but now the this.numerator and this.numerator supposedly from answer does not work. Now numerator and denominator are assigned the values of 5 and 1 in the Fractions method.

This is the output which is correct up until I am trying to add all fractions within an array:

The program will now create 3 arrays

of random sizes and prompt for values.

Read the first array.

This array has 3 elements

For cell number 1

Enter a number for the numerator:

5

Enter a number for the denominator:

4

For cell number 2

Enter a number for the numerator:

7

Enter a number for the denominator:

8

null

For cell number 3

Enter a number for the numerator:

9

Enter a number for the denominator:

8

The fractions in the first array are:

5/4 ,7/8 ,9/8 ,

5 num in the add method 4 den in the add method

17/8

5 num in the add method 1 den in the add method

49/8

5 3

The average of Fraction array one is 5/3

praveen kumaar wrote:Hi Daniel,can you please show us your full clean code.then it would be easy to locate the error or problem because i really found it difficult to get what actually is going in your add method or in some other part acc. To your given information.

Kind regards,

Praveen

I think I can't do that. The programs are a bit extensive and it would be hard to simplify it to show only the part where there is a problem. I will show the code that is relevant to the issue. The add method contains another method within to get the greatest common divisor. I don't include it because I don't want people to try and analyze it as it takes a while to go line by line to know what is going on. The method works fine which is why I want you guys to just focus on where the problem is.

The second time the for loop in the FractionsArrays calls the add method in the Fractions class it does not use the value in numerator and denominator currently in the array

*answer*.

This is the FractionsArrays class:

In the add method the first time is called, the fraction located in answer[0] which is 5/4 is added to 7/8 which is value in (array[x]);. This.numerator is 5 and this.denominator is 4. The owner of this is answer from answer = answer.add(array[x]) The second time it loops is the problem as you can see. It should be 17/8 plus 9/8 but now the this.numerator and this.numerator supposedly from answer does not work. Now numerator and denominator are assigned the values of 5 and 1 in the Fractions method.

This is the output which is correct up until I am trying to add all fractions within an array:

The program will now create 3 arrays

of random sizes and prompt for values.

Read the first array.

This array has 3 elements

For cell number 1

Enter a number for the numerator:

5

Enter a number for the denominator:

4

For cell number 2

Enter a number for the numerator:

7

Enter a number for the denominator:

8

null

For cell number 3

Enter a number for the numerator:

9

Enter a number for the denominator:

8

The fractions in the first array are:

5/4 ,7/8 ,9/8 ,

5 num in the add method 4 den in the add method

17/8

5 num in the add method 1 den in the add method

49/8

5 3

The average of Fraction array one is 5/3

Norm Radder

Rancher

Posts: 2240

28

posted 1 year ago

I see. Well, thank you either way! I'll keep trying to fix it.

Norm Radder wrote:Sorry, the posted code does not compile without lots of errors. It looks like the end is missing.

Also I can't see the comments in the posted output where the output is not what you wanted. I would expect to see a marker like: <<<**** wrong here >> should be 5/3

I see. Well, thank you either way! I'll keep trying to fix it.

posted 1 year ago

Thank you and everyone for the response. I finally was able to identify the problem. I created an array but I did not allocate space in memory for it so in my loop, I was using the numbers set in the instances variables. I will come back in the future I'm sure but this time I will make sure to post the entire code

Ole Sandum wrote:Would you kindly post the code again, with the missibg end of that method added?

Thank you and everyone for the response. I finally was able to identify the problem. I created an array but I did not allocate space in memory for it so in my loop, I was using the numbers set in the instances variables. I will come back in the future I'm sure but this time I will make sure to post the entire code

It is sorta covered in the JavaRanch Style Guide. |