Need help with a java program: Imagine that the company you work for is going to
ID: 3829985 • Letter: N
Question
Need help with a java program: Imagine that the company you work for is going to create a lot of lessons on Fractions. You are to create a robust Fraction class that will have all of the following (all examples are for a fraction half that has a numerator of 1 and a denominator of 2): Private integers numerator and denominator ; All public getter and setter functions for the numerator and denominator; Safeguard that the denominator will NEVER become 0! a default constructor with no arguments; a constructor that accepts both the numerator and denominator; a toDecimal method that returns the decimal value of the fraction, example: 1/2 will be 0.5; a toString method that will return the fraction as a string, , example: 1/2 will be "1/2"; a reduce method that will change the numerator and denominator by finding a common denominator and reducing the fraction. Example 3/12 becomes 1/4; Submit the following as part of this assignment: A word document with source code for all files Screen shots showing several different fractions and an example where the user tried to assign 0 to the denominator. Your output should show all of the methods listed above. A class should not interact with the user. If an invalid value is received it simple does not accept it. It does not display a message, it does not exit. It is up to the calling program to check if the value was accepted and display an appropriate message.
Explanation / Answer
Please find my implementation.
################ Fraction.java ########
public class Fraction {
/**
* instance variable
*/
public int numerator;
/**
* instance variable
*/
public int denominator;
// copy constructor
public Fraction(Fraction other) {
numerator = other.numerator;
denominator = other.denominator;
}
/**
* param n
* param d
*/
public Fraction(int n, int d) {
if(d == 0){
System.out.println("Denominator can not be negative");
System.exit(0);
}
// finding gcd of n and d
int gcd = reduce(n, d);
// storing numerator and denominator in reduced form
numerator = n/gcd;
denominator = d/gcd;
}
// toString method
public String toString(){
return numerator+"/"+denominator;
}
// equal method
public boolean equals(Object other)
{
if( other != null && ! (other instanceof Fraction ) ) return false;
Fraction that = (Fraction)other;
if(numerator == that.numerator && denominator == that.denominator)
return true;
else
return false;
}
/**
* function to calculate gcd of numerator and denominator
*/
private int reduce(int numerator, int denominator) {
int min = numerator;
int gcd;
if (numerator > denominator) {
min = denominator;
}
for (gcd = min; gcd > 1; gcd--) {
if (numerator % gcd == 0 && denominator % gcd == 0) {
break;
}
}
return gcd;
}
public Fraction add(Fraction other){
int num = numerator*other.denominator + denominator*other.numerator;
int deno = numerator*denominator;
int gcd = reduce(num, deno);
return new Fraction(num/gcd, deno/gcd);
}
}
###############
public class FractionDriver {
public static void main(String[] args) {
Fraction a = new Fraction(1,2);
Fraction b = new Fraction(3,4);
Fraction c = new Fraction(b);
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString());
System.out.println("c:"+c.toString());
a.add(b);
a = a.add(b);
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString());
System.out.println("c:"+c.toString());
}
}
/*
Sample output:
a:1/2
b:3/4
c:3/4
a:5/1
b:3/4
c:3/4
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.