wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes java fraction calc now compiles but output is wrong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java fraction calc now compiles but output is wrong" Watch "java fraction calc now compiles but output is wrong" New topic
Author

java fraction calc now compiles but output is wrong

Richard Scott
Ranch Hand

Joined: Jan 10, 2014
Posts: 30
so all the errors in the code is fixed but its now got the wrong output

what I get:

Enter numerator; then denominator.
5
8
5/8
Enter numerator; then denominator.
4
10
4/10
Sum:0/0

it should be:

Enter numerator; then denominator.
5
8
5/8
Enter numerator; then denominator.
4
10
4/10
Sum: 82/80 1.025
Product: 20/80 0.25
Enter numerator; then denominator.
6 0
infinity

here is the code

Fraction.java


and the driver
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Your Fraction constructor produces an invalid fraction: 0/0 has no meaning. (And worse, it makes your calculations fail.) I would suggest 0/1 for a default whose value is zero.
Richard Scott
Ranch Hand

Joined: Jan 10, 2014
Posts: 30
Paul Clapham wrote:Your Fraction constructor produces an invalid fraction: 0/0 has no meaning. (And worse, it makes your calculations fail.) I would suggest 0/1 for a default whose value is zero.


ok that's fixed but its still not printing the double or the product
Richard Scott
Ranch Hand

Joined: Jan 10, 2014
Posts: 30
Richard Scott wrote:
Paul Clapham wrote:Your Fraction constructor produces an invalid fraction: 0/0 has no meaning. (And worse, it makes your calculations fail.) I would suggest 0/1 for a default whose value is zero.


ok that's fixed but its still not printing the double or the product


ok so I have updated the driver, I realized I was using the wrong one! here it is:



and here are some more errors

Prog7.java:13: error: cannot find symbol
x.printAsDouble();
^
symbol: method printAsDouble()
location: variable x of type Fraction
Prog7.java:18: error: cannot find symbol
x.printAsDouble();
^
symbol: method printAsDouble()
location: variable x of type Fraction
Prog7.java:21: error: cannot find symbol
x.printAsDouble();
^
symbol: method printAsDouble()
location: variable x of type Fraction
3 errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It seems that the compiler can't find where you declared "printAsDouble". I'm with the compiler... I can't see where you declared that either. But since I'm programmed differently than the compiler, I can see where you declared a method with a very similar name.
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 252
    
    4
Richard Scott wrote:

ok that's fixed but its still not printing the double or the product


That's because you never call printDouble(), and you never multiply any of the Fractions together or attempt to print the result.

Edit: And extra posts have been made since I started this so my post now is not relevant!
Richard Scott
Ranch Hand

Joined: Jan 10, 2014
Posts: 30
Mike. J. Thompson wrote:
Richard Scott wrote:

ok that's fixed but its still not printing the double or the product


That's because you never call printDouble(), and you never multiply any of the Fractions together or attempt to print the result.

Edit: And extra posts have been made since I started this so my post now is not relevant!


that did it guys it works thank yall so much!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java fraction calc now compiles but output is wrong