Jacob Galvan

Greenhorn

Posts: 1

posted 3 years ago

Hi,

So in my computer science class we are making a Rational Number class. After that we must create client code to test it out. I'm not exactly sure what he is asking us to do here..

The instructions-

• Create 3 instances of RationalNumber. Remember every time you say “new” you are creating an instance.

o One of them should use the default constructor

o One of them should use the constructor with one parameter

o One of them should use the concstructor with two parameters.

• Print out the int and double value of each Rational Number

• Print out the sum (as a double) of all the numbers.

• Print out the sum (as an int) of all the numbers.

My rational number class.

public class RationalNumber{

private int numerator;

private int denominator;

public RationalNumber()

{

numerator =1;

denominator = 4;

}

public RationalNumber(int n)

{

numerator = n;

}

public RationalNumber(int n, int d)

{

numerator = n;

denominator = d;

}

public int getNumerator()

{

return numerator;

}

public int getdenominator()

{

return denominator;

}

public void setNumerator(int n)

{

numerator = n;

}

public void setDenomenator(int d)

{

denominator = d;

}

public double evaluateDouble()

{

return ((double)numerator)/denominator;

}

public int evaluate()

{

return numerator/denominator;

}

public String toString()

{

return numerator +"/" + denominator;

}

}

If anyone could help it would be greatly appreciated!

So in my computer science class we are making a Rational Number class. After that we must create client code to test it out. I'm not exactly sure what he is asking us to do here..

The instructions-

• Create 3 instances of RationalNumber. Remember every time you say “new” you are creating an instance.

o One of them should use the default constructor

o One of them should use the constructor with one parameter

o One of them should use the concstructor with two parameters.

• Print out the int and double value of each Rational Number

• Print out the sum (as a double) of all the numbers.

• Print out the sum (as an int) of all the numbers.

My rational number class.

public class RationalNumber{

private int numerator;

private int denominator;

public RationalNumber()

{

numerator =1;

denominator = 4;

}

public RationalNumber(int n)

{

numerator = n;

}

public RationalNumber(int n, int d)

{

numerator = n;

denominator = d;

}

public int getNumerator()

{

return numerator;

}

public int getdenominator()

{

return denominator;

}

public void setNumerator(int n)

{

numerator = n;

}

public void setDenomenator(int d)

{

denominator = d;

}

public double evaluateDouble()

{

return ((double)numerator)/denominator;

}

public int evaluate()

{

return numerator/denominator;

}

public String toString()

{

return numerator +"/" + denominator;

}

}

If anyone could help it would be greatly appreciated!

posted 3 years ago

Hi Jacob

Welcome to Java ranch !!

If I would have been given this exercise, I would have performed it in following way. So below mentioned is the interpretation from my side.

You need to declare a Rational Number class which should have following features

a. Should have default constructor

b. Should have one constructor with one parameters

c. Should have one constructor with two parameters

Now your Client class should use Rational Number class and perform below activities. So all these points are really the steps that you need to do in your Client class code.

For example - Your Client class should create 3 instances of Rational Number class. Now when you create these 3 instances, one of that instance should be created using default constructor. Other one should be created using one-param constructor. Next one should be created with 2-param constructor.

Like wise, when all this is done, you need to print int and double value of each Rational Number. Here, by

Hope you are now clear with Java exercise.

Feel free to reply back if you have any queries

~ abhay agarwal

Welcome to Java ranch !!

If I would have been given this exercise, I would have performed it in following way. So below mentioned is the interpretation from my side.

You need to declare a Rational Number class which should have following features

a. Should have default constructor

b. Should have one constructor with one parameters

c. Should have one constructor with two parameters

Now your Client class should use Rational Number class and perform below activities. So all these points are really the steps that you need to do in your Client class code.

• Create 3 instances of RationalNumber. Remember every time you say “new” you are creating an instance.

o One of them should use the default constructor

o One of them should use the constructor with one parameter

o One of them should use the concstructor with two parameters.

• Print out the int and double value of each Rational Number

• Print out the sum (as a double) of all the numbers.

• Print out the sum (as an int) of all the numbers.

For example - Your Client class should create 3 instances of Rational Number class. Now when you create these 3 instances, one of that instance should be created using default constructor. Other one should be created using one-param constructor. Next one should be created with 2-param constructor.

Like wise, when all this is done, you need to print int and double value of each Rational Number. Here, by

**each**Rational Number, it is meant for**each instance**of Rational Number [ because in your client code, you have just created 3 instances of Rational Number]. Similarly, you need to perform other print activities.Hope you are now clear with Java exercise.

Feel free to reply back if you have any queries

~ abhay agarwal