Write a Fraction class – that is, write your own class that will represent a fra
ID: 3571765 • Letter: W
Question
Write a Fraction class – that is, write your own class that will represent a fraction in your program. Each variable of type Fraction represents a single fraction. That means that the class should have at least two data fields that should be private: the numerator and the denominator of the fraction. The class also must have the following public class methods:
You will also have to write a program to test your Fraction class. The following output is an example run:
== F1 ==
Enter numerator: 1 Enter denominator: 2 == F2 ==
Enter numerator: 3 Enter denominator: 5
F1: 1/2 (0.500)
F2: 3/5 (0.600) F1+F1: 1
F2+F2: 6/5 (1.200) F1+F2: 11/10 (1.100) F2+F1: 11/10 (1.100) F1-F1: 0
F2-F2: 0
F1-F2: -1/10 (-0.100) F2-F1: 1/10 (0.100) F1*F1: 1/4 (0.250) F2*F2: 9/25 (0.360) F1*F2: 3/10 (0.300) F2*F1: 3/10 (0.300) F1/F1: 1
F2/F2: 1
F1/F2: 5/6 (0.833) F2/F1: 6/5 (1.200) F1*F1-F2: -7/20 (-0.350)
The program should use the Scanner constructor, the plus/minus/times/divides methods (as opposed to directly computing the outputs), and implicitly the toString method.
Here is the code we are given:
package edu.wit.cs.comp1000;
import java.util.Scanner;
// Chris Lewis
public class Fraction {
/**
* Error to output if denominator is zero
*/
private static final String E_DEN_ZERO = "Denominator cannot be zero.";
/**
* Error to output if dividing by zero
*/
private static final String E_DIV_ZERO = "Cannot divide by zero.";
/**
* Returns the greatest common divisor (gcd) of two integers
*
* @param num1 integer 1
* @param num2 integer 2
* @return gcd of integers 1 and 2
*/
private static int gcd(int num1, int num2) {
int t;
while (num2 != 0) {
t = num2;
num2 = num1 % num2;
num1 = t;
}
return num1;
}
/**
* Returns the simplified form of a fraction
*
* @param f fraction (numerator=[0], denominator=[1])
* @return simplified fraction (numerator=[0], denominator=[1])
*/
private static int[] simplifyFraction(int[] f) {
final int gcd = gcd(f[0], f[1]);
int[] result = {f[0]/gcd, f[1]/gcd};
if ((result[0]<0 && result[1]<0) || (result[1]<0)) {
result[0] = -result[0];
result[1] = -result[1];
}
return result;
}
/**
* Constructs a fraction given a numerator/denominator
*
* If denominator = 0, exit with message
*
* @param n numerator
* @param d denominator
*/
public Fraction(int n, int d) {
// TODO: write your code here
}
/**
* Constructs a fraction as 0/1
*/
public Fraction() {
// TODO: write your code here
}
/**
* Constructs a fraction given input from a supplied Scanner
*
* If denominator = 0, exit with message
*
* @param s scanner from which to read fraction components
*/
public Fraction(Scanner s) {
// TODO: write your code here
}
/**
* Gets the decimal value of a fraction
*
* @return decimal version
*/
public double toDecimal() {
return 0; // TODO: replace with your code
}
/**
* Returns the string value of a fraction:
* - if denominator is 1, "numerator"
* - else, "numerator/denominator (decimal with three decimal places)"
*
* @return string formatted fraction
*/
public String toString() {
return ""; // TODO: replace with your code
}
/**
* Gets the fraction numerator
*
* @return numerator
*/
public int getNumerator() {
return 0; // TODO: replace with your code
}
/**
* Gets the fraction denominator
*
* @return denominator
*/
public int getDenominator() {
return 0; // TODO: replace with your code
}
/**
* Adds a fraction to this fraction
*
* @param f fraction to add
* @return a new fraction that sums this and the supplied parameter
*/
public Fraction plus(Fraction f) {
return new Fraction(); // TODO: replace with your code
}
/**
* Subtracts a fraction from this fraction
*
* @param f fraction to subtract
* @return a new fraction that results from subtracting the supplied parameter from this
*/
public Fraction minus(Fraction f) {
return new Fraction(); // TODO: replace with your code
}
/**
* Multiplies a fraction with this fraction
*
* @param f fraction to multiply
* @return a new fraction that is the product of this and the supplied parameter
*/
public Fraction times(Fraction f) {
return new Fraction(); // TODO: replace with your code
}
/**
* Divides a fraction into this fraction
*
* @param f fraction with which to divide
* @return a new fraction that is the quotient of this and the supplied parameter
*/
public Fraction divides(Fraction f) {
return new Fraction(); // TODO: replace with your code
}
}
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
############### Fraction.java ################
import java.util.Scanner;
//TODO: document this class
public class Fraction {
// instance variable
private int numerator;
private int denominator;
/**
* Error to output if denominator is zero
*/
private static final String E_DEN_ZERO = "Denominator cannot be zero.";
/**
* Error to output if dividing by zero
*/
private static final String E_DIV_ZERO = "Cannot divide by zero.";
/**
* Returns the greatest common divisor (gcd) of two integers
* @param num1 integer 1
* @param num2 integer 2
* @return gcd of integers 1 and 2
*/
private static int gcd(int num1, int num2) {
int t;
while (num2 != 0) {
t = num2;
num2 = num1 % num2;
num1 = t;
}
return num1;
}
/**
* Returns the simplified form of a fraction
* @param f fraction (numerator=[0], denominator=[1])
* @return simplified fraction (numerator=[0], denominator=[1])
*/
private static int[] simplifyFraction(int[] f) {
final int gcd = gcd(f[0], f[1]);
int[] result = {f[0]/gcd, f[1]/gcd};
if ((result[0]<0 && result[1]<0) || (result[1]<0)) {
result[0] = -result[0];
result[1] = -result[1];
}
return result;
}
/**
* Constructs a fraction given a numerator/denominator
* If denominator = 0, exit with message
* @param n numerator
* @param d denominator
*/
public Fraction(int n, int d) {
// TODO: write your code here
if(d == 0){
System.out.println(E_DEN_ZERO);
System.exit(0);
}
numerator = n;
denominator = d;
}
// Constructs a fraction as 0/1
public Fraction() {
// TODO: write your code here
numerator = 0;
denominator = 1;
}
/**
* Constructs a fraction given input from a supplied Scanner
* If denominator = 0, exit with message
* @param s scanner from which to read fraction components
*/
public Fraction(Scanner s) {
// TODO: write your code here
System.out.print("Enter numerator: ");
numerator = s.nextInt();
System.out.print("Enter denominator: ");
denominator = s.nextInt();
if(denominator == 0){
System.out.println(E_DEN_ZERO);
System.exit(0);
}
}
/**
* Gets the decimal value of a fraction
* @return decimal version
*/
public double toDecimal() {
return (double)numerator/(double)denominator;
}
/**
* Returns the string value of a fraction:
* - if denominator is 1, "numerator"
* - else, "numerator/denominator (decimal with three decimal places)"
* @return string formatted fraction
*/
public String toString() {
String f = "";
if(denominator == 1)
f = String.valueOf(numerator);
else
f = numerator+"/"+denominator;
f = f + " ("+String.format("%.3f", toDecimal()) +")";
return f;
}
/**
* Gets the fraction numerator
* @return numerator
*/
public int getNumerator() {
return numerator;
}
/**
* Gets the fraction denominator
* @return denominator
*/
public int getDenominator() {
return denominator;
}
/**
* Adds a fraction to this fraction
* @param f fraction to add
* @return a new fraction that sums this and the supplied parameter
*/
public Fraction plus(Fraction f) {
int num = numerator*f.getDenominator() + f.numerator*denominator;
int den = denominator*f.denominator;
int r[] = simplifyFraction(new int[]{num, den});
return new Fraction(r[0], r[1]);
}
/**
* Subtracts a fraction from this fraction
*
* @param f fraction to subtract
* @return a new fraction that results from subtracting the supplied parameter from this
*/
public Fraction minus(Fraction f) {
int num = numerator*f.getDenominator() - f.numerator*denominator;
int den = denominator*f.denominator;
int r[] = simplifyFraction(new int[]{num, den});
return new Fraction(r[0], r[1]);
}
/**
* Multiplies a fraction with this fraction
*
* @param f fraction to multiply
* @return a new fraction that is the product of this and the supplied parameter
*/
public Fraction times(Fraction f) {
int num = numerator*f.numerator;
int den = denominator*f.denominator;
int r[] = simplifyFraction(new int[]{num, den});
return new Fraction(r[0], r[1]);
}
/**
* Divides a fraction into this fraction
* @param f fraction with which to divide
* @return a new fraction that is the quotient of this and the supplied parameter
*/
public Fraction divides(Fraction f) {
int num = numerator*f.denominator;
int den = denominator*f.numerator;
int r[] = simplifyFraction(new int[]{num, den});
return new Fraction(r[0], r[1]);
}
}
################### TestFraction.java ###################
import java.util.Scanner;
public class TestFraction {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num, denom;
System.out.println("== F1 ==");
System.out.print("Enter numerator: ");
num = sc.nextInt();
System.out.print("Enter denominator: ");
denom = sc.nextInt();
Fraction f1 = new Fraction(num, denom);
System.out.println("== F2 ==");
System.out.print("Enter numerator: ");
num = sc.nextInt();
System.out.print("Enter denominator: ");
denom = sc.nextInt();
Fraction f2 = new Fraction(num, denom);
System.out.println("F1: "+f1);
System.out.println("F2: "+f2);
System.out.println("F1+F2: "+f1.plus(f2));
System.out.println("F1-F2: "+f1.minus(f2));
System.out.println("F2-F2: "+f2.minus(f2));
System.out.println("F1*F2: "+f1.times(f2));
System.out.println("F1/F2: "+f1.divides(f2));
}
}
/*
Sample run:
== F1 ==
Enter numerator: 1
Enter denominator: 2
== F2 ==
Enter numerator: 3
Enter denominator: 4
F1: 1/2 (0.500)
F2: 3/4 (0.750)
F1+F2: 5/4 (1.250)
F1-F2: -1/4 (-0.250)
F2-F2: 0 (0.000)
F1*F2: 3/8 (0.375)
F1/F2: 2/3 (0.667)
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.