Forums Register Login

Wrong output

+Pie Number of slices to send: Send
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..


+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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??


+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
oh nevermind.... I got it all figured out now.. thanks so much for the help guys.
+Pie Number of slices to send: Send
Kudos for that!

Well, I hope you are clear with the static thing now.
+Pie Number of slices to send: Send
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
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1824 times.
Similar Threads
Add an exception for divide by zero to my fraction class
java program using a class fraction
Please help with a simple GUI.
null pointer exception
sorting fractions problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:54:21.