Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java fraction calc now compiles but output is wrong

 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic