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

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?

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

At the center of non-violence stands the principle of love

.... Martin Luther King, Jr.

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.

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

At the center of non-violence stands the principle of love

.... Martin Luther King, Jr.

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.

I agree. Here's the better link: Salvin.in

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

Experience and talent are independent of age

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

Experience and talent are independent of age