• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Fractions Java

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I have an example program given by my teacher that is meant to help with our abstract fraction program. I mostly understand what it's supposed to do, but it doesn't compile because there's a couple of issues with it (why he would bother giving and uncompilable program is beyond me, but hey, at least he gave me something for once). I'm going to post it and then point out the issues, and if anyone could tell me how to fix them so I can use it to compare to my own assignment, that would be great.





The parts of the code I highlighted in red are the problems. The "Savitch.In" is an issue, and the "common = convert(common)" is also. This is the output I'm getting for that program.

1/3 - 1/6 = 0/1
1/6 - 1/3 = 0/1

Fraction 1:
Please enter an integer for numerator: Please enter a non-zero integer for denominator:
Fraction 2:
Please enter an integer for numerator: Please enter a non-zero integer for denominator:
1/3 * 1/6 = 1/18
1/3 / 1/6 = 2/1
BUILD SUCCESSFUL (total time: 1 second)


Thanks.
 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops, I guess it won't color the text within a code. Sorry.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find the source code to the SavitchIn class on the net. It seems to be widely used.
 
Marshal
Posts: 80621
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the colours do not appear; I have removed them because the idea is that you can copy and paste the code from the code tags (click the view plain link) and execute it yourself. The colour tags would prevent that.

Why have you got a no‑arguments constructor? I would suggest you get rid of that because I don't think you create a Fraction with no numbers in. If yo udo keep it, there are two things I suggest you change:-
  • 1: Don't have two assignments on the same line: poor style.
  • 2: Assign your denominator to 1 otherwise you have the dangerous error of denominator 0 which can cause division by 0.
  • I think you need some debugging code. Start with this at the end of each constructor:-That will show you whether you are successfully initialising your objects.
    The second part of the output, with 1/18 and 2/1, looks correct to me. When your debugging code gets to subtraction, you will need lots of printouts like that to find the error. What formula are you using? I think it should be

    d1 × n2 - d2 × n1
          d1 × d2
    reply
      Bookmark Topic Watch Topic
    • New Topic