• 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

Possible issue with inheritance? Value of variables not being passed properly

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been working on this program for quite a while, and can't figure out what's going on. In TestFraction, I use a while loop and Scanner to get two integers, a and b, from the user, and these values should be turned into the fraction a/b. This is done twice to create two fractions, fr1 and fr2. Then the class Fraction (which I created previously for another project) will perform operations on these two fractions. The issue I'm running into is that rather than getting the values of a and b from Scanner, as in my methods getFraction1 and getFraction2, the values of a and b seem to be set by their initial definition in Fraction, which is a=1, b=1. I know that these values are coming from Fraction because I've attempted to set a and b to other values when they are initialized in TestFraction. Can anyone give me some hints as to what's going on here? I'm quite new to Java, and have a feeling that I'm not understanding something about inheritance.

Here is the output I get:

Input 1st fraction numerator and denominator
1
4
Input 2nd fraction numerator and denominator
3
4
1/1 + 1/1 = 2/1
1/1 - 1/1 = 0/1
1/1 * 1/1 = 1/1
1/1 / 1/1 = 1/1
1/1 1/1 1/1

Here is TestFraction:



And Fraction:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Deeson wrote:Can anyone give me some hints as to what's going on here?



Nothing to do with inheritance. When do you actually set the values into the fraction?

Henry
 
J. Deeson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I thought that I was setting the values with getFraction1 and getFraction2, but that's apparently not happening.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Deeson wrote:Well, I thought that I was setting the values with getFraction1 and getFraction2, but that's apparently not happening.



Not in the version of the code you posted, you didn't. Those methods both create a Fraction object, but they don't do anything with it. Could you point out the code which you think is setting the values, then? There must be a misconception which needs to be cleared up.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Deeson wrote:Well, I thought that I was setting the values with getFraction1 and getFraction2, but that's apparently not happening.



Not sure from your response if you are agreeing with me -- and realized that the values are not being set in the getFraction1() and getFraction2() methods. Or is still confused and believe that it is being set.

Henry

 
J. Deeson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is where I believe I am setting it. What I meant was, I have written getNumber in such a way that I think I am using Scanner to get the integers that are typed by the user, and storing these two integers as a and b. Here is where I think I am setting the value of fr1 and fr2 (I realize that I'm not actually doing so, as it's not working):
getFraction1 uses getNumber to capture the user's input:

Then, getNumber uses Scanner to get these two integers:


Where is this going wrong?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Deeson wrote:Here is where I believe I am setting it. What I meant was, I have written getNumber in such a way that I think I am using Scanner to get the integers that are typed by the user, and storing these two integers as a and b. Here is where I think I am setting the value of fr1 and fr2 (I realize that I'm not actually doing so, as it's not working):
getFraction1 uses getNumber to capture the user's input:

Then, getNumber uses Scanner to get these two integers:


Where is this going wrong?



Another hint... when you do this?



Which "a" variable are you setting? ... and BTW, it is *not* the variable that you think it is !!

Henry
 
J. Deeson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually figured it out. I was able to simply use the setters in Fraction by adding the lines:


to set the values of a and b in getFraction1 and getFraction2.

Thanks for your help!
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic