• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java fraction calc now compiles but output is wrong

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
reply
    Bookmark Topic Watch Topic
  • New Topic