Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Wrong output

Bryan Peach
Ranch Hand
Posts: 76
So I'm stuck. I have this assignement to take two fractions and do multiple operations to it including listing the numerators and denominator for each fraction. But its not working right and i get the same numbers output for both my fraction1(Frac1) and Fraction2(Frac2).
Ok... so i have my methods and constructor all set up, but when i ask the getNum(Get numerator) and getDenom (Get Denominator) methods to work for both Frac1 and Frac2 it prints out only the values for Frac2 even in Frac1's spot.

Actual output:

The numerator of Frac1 is 2 The denominator of Frac1 is 5
The numerator of Frac2 is 2 The denominator of Frac2 is 5

Even when I set the variables myself:

static Fraction Frac1= new Fraction(1,3);
static Fraction Frac2 = new Fraction(2,5);

I get the same output. I cannot figure out why it is doing this. Here is the code for my output and the getNum method.

public int getNum()
// Returns numerator
{
return numerator;
}

**************************************************************

**************************************************************

Frac1.getNum();
Frac1.getDenom();
System.out.println("The numerator of Frac1 is " + numerator +
" The denominator of Frac1 is " + denominator);
Frac2.getNum();
Frac2.getDenom();
System.out.println("The numerator of Frac2 is " + numerator +
" The denominator of Frac2 is " + denominator);

If anyone can please look at this and let me know what is going on I would be forever grateful... Thanks..

Henry Wong
author
Marshal
Posts: 21742
85
It would help if you provided code that will actually compile -- snippets don't work too well when you need to get specific.

And BTW...

If the getNum() method returns the numerator, shouldn't you save the returned value somewhere?

Henry

Bryan Peach
Ranch Hand
Posts: 76
Ok sorry.. I'm just new at this. Thanks for looking at it for me.

So i'm supposed to call all the different methods and print the results, but I can't get the Frac1 info to print right. All I get for output is Frac2 info printed twice.. see above post.
Any ideas on how to fix this?

Ramses Butarbutar
Ranch Hand
Posts: 52
Hi Bryan Peach,

Why you initiate this in beginning? what for?

static Fraction Frac1= new Fraction(1,3);
static Fraction Frac2 = new Fraction(2,5);

Just initiate when you call it.. It will replace with the latest value if you
initiate that from beginning...

try this..

Bryan Peach
Ranch Hand
Posts: 76
When I do that and Initiate the frac1 and frac2 in the main method I get compile errors from all the different methods saying they can't find the Values for frac1 and Frac2. The only way it will compile for me is when i have it where it is.

Jaydeep Vaishnav
Greenhorn
Posts: 16
Bryan,

Because numerator and denominator are static variables, it stores only the latest value. The values 1 ans 3 resp. for numerator and denominator of frac1 gets overwritten by 2 and 5 of frac2. Use the numerator and denominator variables at the object level so they store state for both the objects and perform appropriate operations. Find the corrected code below.

Ramses Butarbutar
Ranch Hand
Posts: 52
Bryan Peach wrote:When I do that and Initiate the frac1 and frac2 in the main method I get compile errors from all the different methods saying they can't find the Values for frac1 and Frac2. The only way it will compile for me is when i have it where it is.

Is it?? Hmm.. i re make your code like sample below.. I don't know why you say you got errors when you put on main method? Could you explain it??

salvin francis
Bartender
Posts: 1320
10
Here is a Philosophy:

There is no such thing as a "wrong output"

Its and output that you were not expecting.

The computer is Always right, We are Humans after all

Honestly, your program makes no sense to me,

Why would you make nominator and denominator Static ???

Next, In the Context of Meaningful programming,

Nothing in this example should be static. [You can do it but it does not mean you should do it]

are you aware of the fact that the static variables are Common to all objects ???

So, if you make ten objects of FractionUtil, they would still have same value for nominator and denominator.

Bryan Peach
Ranch Hand
Posts: 76
Thanks Jaydeep... thats works. I guess i'm confused about static variables. and i've never heard of this. before. So if i wanted the user to input the values for Frac1 and Frac2. How would i assign those values to the constructor? instead of the way i declare thme now? Does that make any sense?

Himanshu Kansal
Ranch Hand
Posts: 257
Hey Bryan,
If you plan to take values from the user, as in from standard input, then you can simply create objects of the Fraction when you actually GET the input. Prior to that you really do not need to have references to those objects.

Also, as Salvin said, here making the variables and references static does not serve a meaningful purpose that we are aware of. If you want ALL the objects of your class to share same values, i.e. if you want ALL your fractions to be same, then you should use static.

Making numerator and denominator static would mean that a fraction holds its unique(input by user) value only until a new fraction is created.

Regards

Jaydeep Vaishnav
Greenhorn
Posts: 16
Bryan,

Yes that would be a good approach to create frac1 and frac2 when you actually get the input. I think Himanshu is suggesting the same. The one approach you can use is to read the input from system.in (Standard input -i.e. command prompt) and create numerator and denominator out of that. Pass those values into the constructor to create frac1 and frac2.

Bryan Peach
Ranch Hand
Posts: 76
Yeah i understand that part, but what I'm not sure of is how do I pass those values to the constructor? Whats the syntax I guess for passing values to the constructor?

Himanshu Kansal
Ranch Hand
Posts: 257
Bryan Peach wrote:Yeah i understand that part, but what I'm not sure of is how do I pass those values to the constructor? Whats the syntax I guess for passing values to the constructor?

Er... I think I am seriously missing something here. Okay! Do you get problems when you do the following if numerator and denominator are not static :

Or do you get problems in converting the input strings to numbers?

Or if it is something else, please clarify!

Regards

Bryan Peach
Ranch Hand
Posts: 76
oh nevermind.... I got it all figured out now.. thanks so much for the help guys.

Himanshu Kansal
Ranch Hand
Posts: 257
Kudos for that!

Well, I hope you are clear with the static thing now.

Bryan Peach
Ranch Hand
Posts: 76
Yeah its becoming clear to me... Sometimes I get so frustrated with what i am trying to do that i forget some of the simplest things