Object Oriented Programming with said methods. Java - java

Rational numbers contain an integer numerator and denominator. Write the code to implement a class named Rational which stores two private ints (numer and denom) with the following methods:
public Rational(int,int)
constructor that sets the numer and denom
public Rational(Rational)
//copy constructor for a Rational object
public void setNumer(int)
//sets the numerator to the paramter value
public int getNumer()
//returns the stored numerator
public void setDenom(int)
//sets the denominator to the paramter value
public int getDenom() //returns the stored denominator
//return a new Rational object that contains the reciprocal of the object that invokes the method.
public Rational reciprocal()
//returns a new Rational object that contains the product of the two paramteres.
public static Rational multiply(Rational a, Rational b)
I am stuck at the 7th method for this class. I don't understand how to flip the numbers so that they are reciprocals. Any help will be greatly Appreciated. This is my code so far:
class Rational {
private int numer;
private int denom;
public Rational(int numer, int denom){
this.numer = numer;
this.denom = denom;
}
public Rational(Rational rational){
rational = new Rational(numer, denom);
}
public void setNumber(int fum){
numer = fum;
}
public int getNumber(){
return 5;
}
public void setDenom(int Dum){
denom = Dum;
}
public int getDenom(){
return 10;
}
public Rational reciprocal(){
;
}
}
public class Results {
public static void main(String[] args){
}
}

Math is Fun: Reciprocal of a Fraction says (in part) to get the reciprocal of a fraction, just turn it upside down.
public Rational reciprocal(){
return new Rational(this.denom, this.number);
}

You have to return a new Rational with the numbers fliped.
public Rational reciprocal(){
return new Rational(this.denom,this.numer);
}

Try this:
public Rational reciprocal(){
return new Rational(denom, numer);
}
It get the reciprocal which is just the numerator and denominator flipped. return new Rational(denom, numer); does this by creating a new rational instance with the denominator from the current one as the numerator and as the numerator as the current instance as the denominator.
Really a reciprocal is one divided by the original number as said here, but flipping the numerator and denominator does the same thing as dividing by its self.

Related

what happens when Calling Overridden method from with in it?

i was writing Rational class in java to do basic math operation and i want to override methods from
Number class and Comparable interface. i have done that for doubleValue & compareTo methods.
As we know that BigInteger class also extends Number class, so i am confused that which method is being called in doubleValue because i have already override doubleValue & compareTo and calling doubleValue with in doubleValue & compareTo with in compareTo. and it's seems silly too.
import java.math.BigInteger;
class Rational extends Number implements Comparable<Rational>{
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
// Construct a rational with default properties
Rational(){
numerator = BigInteger.ZERO;
denominator = BigInteger.ONE;
}
Rational(BigInteger numerator,BigInteger denominator){
BigInteger gcde = numerator.gcd(denominator);
BigInteger temp = BigInteger.valueOf( (long)(denominator.compareTo(BigInteger.ZERO)) );
this.numerator = temp.multiply(numerator.divide(gcde));
this.denominator = (denominator.abs()).divide(gcde);
}
// subtract a rational number to this rational a/b - r.a/r.b = a*r.b - r.a*b/r.b*b
public Rational subtract(Rational r){
BigInteger n = (numerator.multiply(r.getDenominator())).subtract(denominator.multiply(r.getNumerator()));
BigInteger d = denominator.multiply(r.getDenominator());
return new Rational(n,d);
}
// Return numerator
public BigInteger getNumerator(){
return numerator;
}
// Return denominator
public BigInteger getDenominator(){
return denominator;
}
#Override // Implement the abstract doubleValue method in Number
public double doubleValue(){
return numerator.doubleValue()/denominator.doubleValue();
}
#Override // Implement the abstract longValue method in Number
public long longValue(){
return (long)doubleValue();
}
#Override
public int compareTo(Rational r){
return (this.subtract(r).getNumerator()).compareTo(BigInteger.ZERO);
}
}
public double doubleValue(){
return numerator.doubleValue()/denominator.doubleValue();
}
numerator is a BigInteger so BigInteger.doubleValue() is being called. Same with denominator. It does not matter that your class also happens to have a doubleValue() function. numerator and denominator are a different class than your Rational class.
Number
/ \
BigInteger Rational
| |
doubleValue doubleValue

Arithmetical operations task with Objects

I've checked many stackOverflow questions about operations with objects but I haven't find the solution.
My task is to multiply and divide two objects with these constructors:
public class Fraction {
private int denom;
private int counter;
public Fraction() {
this.counter = 0;
this.denom = 1;
}
public Fraction(int counter) {
this.counter = counter;
this.denom = 1;
}
public Fraction(int counter, int denom) {
this.counter = counter;
if (denom == 0) {
this.denom = 1;
} else
this.denom = denom;
}
}
What would be inside the "multiply" and "divide" methods?
public Fraction multiply(Fraction other) {
}
public Fraction divide(Fraction other) {
}
if this is what I need to use:
Fraction frac1 = new Fraction (2);
Fraction frac2 = new TortSzam(3,4);
fracResult = frac1.divide(frac2);
and the result is: 2.6666666666666665
What I tried by other StackOverflow Questions:
public Fraction multiply(Fraction other) {
final Fraction multi = this;
BigInteger result = new BigInteger;
result.multiply(other);
}
But didn't work.
Thanks in advance.
Multiplying two fractions just means multiplying the numerators, and then dividing that product by the multiplication of the denominators. So you may try:
public Fraction multiply(Fraction other) {
int counter = other.getCounter() * this.counter;
int denim = other.getDenominator() * this.denom;
return new Fraction(counter, denom);
}
I will leave the implementation of division up to you. As a hint, the code would be very similar to the above except that you would use the inverse of one (but not both) of the two fraction inputs.

Problems with class methods and testing these methods?

I am learning how to code and I am having trouble with my class methods and testing these methods. Here is the problem:
In this lab, we will create a Fraction class. This class is used to represent a ratio of two integers. The main method of the FractionDriver class will contain the code that tests your Fraction class. I recommend that you test your Fraction class incrementally.
Your Fraction class should have two private integer instance variables, numerator and denominator. Initially, the value of numerator should be 0 and the value of denominator should be 1.
Write two mutator methods, setNumerator() and setDenominator(), that allow the user to set the numerator and the denominator to an integer value. Your code should not allow the denominator to be set to 0. If the user tries to set the denominator to 0, the value should not be changed.
Also, include a method named getValue() that returns the value of the numerator divided by the denominator as a double.
Add a toString() method that returns a String representation of the fraction in the form numerator/denominator, for example 5/3.
Finally, add an equals method that determines whether two objects of type Fraction are equal. Note that 3/5 and 6/10 should be considered equal.
Here is my code for my Fraction class:
public class Fraction {
private int numerator = 0;
private int denominator = 1;
private double divide;
//setting numerator and denominator
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public void setDenominator(int denominator) {
if (denominator == 0) {
return;
}
this.denominator = denominator;
}
//returning value of the numerator divided by a denominator as a double
public void getValue() {
divide = numerator / denominator;
this.divide = divide;
System.out.println("The value of this fraction in decimal form is: " + divide);
}
//returning the fraction as a string #/#
public String toString() {
return "Your fraction is: " + numerator + "/" + denominator;
}
public boolean equals(Fraction other) {
if(other.divide == divide) {
return true;
}
return false;
}
}
Here is the code for my driver so far:
public class FractionDriver {
public static void main(String[] args) {
Fraction fract1 = new Fraction();
Fraction fract2 = new Fraction();
//initialize variables
fract1.setNumerator(1);
fract1.setDenominator(2);
fract2.setNumerator(5);
fract2.setDenominator(10);
for(int i = 0; i < 1; i++) {
//testing toString method
System.out.println(fract1.toString());
System.out.println(fract2.toString());
fract1.getValue();
fract2.getValue();
}
}
}
When I test my getValue() method for both fractions, each have the result of 0.0 and I am not sure what I am doing wrong in my class method.
Also, how do I test my equals method?
Dividing an int by an int gives another int. You don't get halves etc.
See Why is the result of 1/3 == 0?
Your method to get value is VOID... so that's your problem.
It should be
//returning value of the numerator divided by a denominator as a double
public double getValue() {
divide = numerator / denominator;
this.divide = divide;
return this.divide;
}
But better to make it
public double getValue() {
return numerator / denominator;
}

what is the rational class in java?

I was assigned to make a rational class by java but I really don't understand what is required as below:
Rational Implement a rational number class: Rational Augment your class with methods for:
Initialization (Constructor): parameters are numerator and
denominator as integers. You must have 3 constructors as follows:
No Parameters: 0 / 1
One Parameter (x): x / 1
Two Parameters (x, y): x / y
float getValue(): returns the value of the number
[bonus] Rational add(Rational r): adds to another rational number
All your numbers should be saved in Reduced Form
Augment your code with a driver class (that contains "main" method)
that constructs two Rational numbers, get the average of the two
numbers and prints it on the screen.
This code implements some of your requirement, but the [bonus] task, and the usage of the reduced form is missing, it is up to you to finish it.
class Rational {
private int nominator;
private int denominator;
public Rational() {
this(0, 1);
}
public Rational(int nominator) {
this(nominator, 1);
}
public Rational(int nominator, int denominator) {
this.nominator = nominator;
this.denominator = denominator;
}
public float getValue() {
return nominator / (float) denominator;
}
}

Why is my while loop being skipped?

I think my program is skipping my while loop, but I'm honestly not sure exactly what is happening. The function is supposed to reduce fractions by finding the GCD and then dividing numerator and denominator by that number.
class Rational {
private int numerator, denominator;
//Constructor
public Rational (int num, int den) {
numerator = num;
denominator = den;
}
//Method for multiplying fractions
public Rational times (Rational that) {
Rational x = new Rational (this.numerator*that.numerator, this.denominator*that.denominator);
x = x.reduce();
return x;
}
//Method for displaying fractions as strings
public String toString() {
return new String(numerator+"/"+denominator);
}
//Method for adding fractions
public Rational plus(Rational that) {
Rational x = new Rational ((this.numerator*that.denominator)+(that.numerator*this.denominator),
this.denominator*that.denominator);
//x = x.reduce();
return x;
}
//Method for subtracting fractions
public Rational minus(Rational that) {
Rational x = new Rational ((this.numerator*that.denominator)-(that.numerator*this.denominator),
this.denominator*that.denominator);
//x = x.reduce();
return x;
}
//Method for dividing fractions
public Rational divideBy(Rational that) {
Rational x = new Rational (this.numerator*that.denominator, this.denominator*that.numerator);
//x = x.reduce();
return x;
}
public Rational reduce() {
int a = Math.abs(this.numerator);
int b = Math.abs(this.denominator);
int c = Math.min(a, b);
System.out.println(c);
System.out.println(a%c);
System.out.println(b%c);
if (a==0) {
return new Rational (0,1);
}
else {
while (((a%c)!= 0) && ((b%c)!= 0)) {
c = c-1;
System.out.println(c);
}
System.out.println(c);
return new Rational (this.numerator/c,this.denominator/c);
}
}
}
public class RationalTester {
public static void main(String[] args) {
Rational x = new Rational (6,4); //The fraction 6/4
Rational y = new Rational (5,2); //The fraction 5/2
Rational z = x.times(y); //Their product
Rational w = x.plus(y); //Their sum
Rational v = x.minus(y); //Their difference
Rational u = x.divideBy(y); //Their quotient
JOptionPane.showMessageDialog(null, x.toString()+" * "+y.toString()+" = "+z.toString());
JOptionPane.showMessageDialog(null, x.toString()+" + "+y.toString()+" = "+w.toString());
JOptionPane.showMessageDialog(null, x.toString()+" - "+y.toString()+" = "+v.toString());
JOptionPane.showMessageDialog(null, x.toString()+" / "+y.toString()+" = "+u.toString());
}
}
I'm getting the absolute value of the numerator and denominator to ensure that if the fraction is negative I'll be keeping that at the end. If the numerator is 0, I was asked to return (0,1). The question is about the while loop... it seems that it's being skipped completely. Any suggestions?
Because always its condition is false.
In the first lines you set c equal to either a or b. So there are two possibilities:
If c == a, then a%c will be zero. So the while condition is false.
If c == b, then b%c will be zero. So the while condition is false.

Categories