Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is written in Java, The skeleton and class for this program are pri

ID: 3766163 • Letter: T

Question

This program is written in Java, The skeleton and class for this program are printed at the end.

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 member variables that should be private: the numerator and the denominator of the fraction. The class also must have the following public member functions:

Fraction(int n, int d); // constructor that defines a fraction n/d

Fraction(); // constructor that defines a default fraction of 0/1

Fraction(Scanner s); // constructor that defines a fraction via Scanner input

double toDecimal(); // returns the decimal value of the fraction

String toString(); // returns the string form of the fraction

"numerator" if denominator is 1

"numerator/denominator (decimal, with three decimal places)" otherwise

int getNumerator(); // returns the numerator of the fraction

int getDenominator(); // returns the denominator of the fraction

Fraction plus(Fraction f); // returns fraction + parameter

Fraction minus(Fraction f); // returns fraction - parameter

Fraction times(Fraction f); // returns fraction * parameter

Fraction divides(Fraction f); // returns fraction / parameter

You have been provided a private method:

int[] simplifyFraction(int[] f); // returns simplified version of parameter

Using this method, all constructors should make sure that the fraction is in reduced form. You may not change the public API – that is, any methods not listed above must be made private.

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.

Skeleton:

Class:

Explanation / Answer

import java.util.Scanner;

class Fraction {
/**
* Error to output if denominator is zero
*/
private static String E_DEN_ZERO = "Denominator cannot be zero.";
  
/**
* Error to output if dividing by zero
*/
private static String E_DIV_ZERO = "Cannot divide by zero.";
  
private int numerator;
private int denominator;
private double decimalValue;
private String strValue;
  
/**
* 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 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 void simplifyFraction() {
int gcd1 = gcd(numerator, denominator);
numerator/=gcd1;
denominator/=gcd1;
}
  
/**
* Returns the double value of a fraction
*
* @param n numerator
* @param d denominator
* @return numerator / denominator
*/
private double computeDecimalValue(int n, int d) {
return (1.0 * n)/d;
}
  
/**
* Returns the string value of a fraction:
* - if denominator is 1, "numerator"
* - else, "numerator/denominator (decimal with three decimal places)"
*
* @param n numerator
* @param d denominator
* @param dec decimal value
* @return string formatted fraction
*/
private String computeStringValue(int n, int d, double dec) {
return ""+n+"/"+d+" ("+String.format("%.3f",dec)+")";
//your code goes here
}
  
/**
* Detects generic issues with a fraction
*
* @param f fraction to check
*/
private void validate() {
//your code goes here
if(denominator==0)
System.out.println(E_DEN_ZERO);
  
}
  
/**
* 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) {
numerator=n;
denominator = d;
if(d==0)
System.out.println(E_DEN_ZERO);
simplifyFraction();
decimalValue = computeDecimalValue(numerator, denominator);
strValue = computeStringValue(numerator, denominator, decimalValue);
}
  
/**
* Constructs a fraction as 0/1
*/
public Fraction() {
numerator = 0;
denominator = 1;
decimalValue = computeDecimalValue(numerator, denominator);
strValue = computeStringValue(numerator, denominator, decimalValue);
}
  
/**
* 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) {
final int[] input = new int[2];
System.out.print("Enter numerator: ");
numerator = s.nextInt();
System.out.print("Enter denominator: ");
denominator = s.nextInt();
validate();
simplifyFraction();
decimalValue = computeDecimalValue(numerator, denominator);
strValue = computeStringValue(numerator, denominator, decimalValue);
//your code goes here
}
  
/**
* Gets the decimal value of a fraction
*
* @return decimal version
*/
public double toDecimal() {
return decimalValue;
}
  
/**
* 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 strValue;
}
  
/**
* Gets the fraction numerator
*
* @return numerator
*/
public int getNumerator() {
return numerator;
}
  
/**
* Gets the fraction denominator
*
* @return denominator
*/
public int getDenominator() {
//your code goes here
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) {
return new Fraction(numerator*f.denominator+denominator*f.numerator, denominator*f.denominator);
}
  
/**
* 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) {
//your code goes here
return new Fraction(numerator*f.denominator-denominator*f.numerator, denominator*f.denominator);
}
  
/**
* 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) {
//your code goes here
return new Fraction(numerator*f.numerator, denominator*f.denominator);
}
  
/**
* 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) {
if(f.numerator == 0)
{
System.out.println(E_DIV_ZERO);
return null;
}
return new Fraction(numerator*f.denominator, denominator*f.numerator);
}

}

public class Lab10 {

/**
* Program execution point:
* input two fractions via the keyboard
* (using the Scanner constructor),
* output the two fractions, all pairwise
* {+ - * /} operations, and the first squared
* minus the second
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
final Scanner kbInput = new Scanner(System.in);
  
System.out.println("== F1 ==");
Fraction f1 = new Fraction(kbInput);
System.out.println("== F2 ==");
Fraction f2 = new Fraction(kbInput);
System.out.println();
  
System.out.printf("F1: %s%n", f1);
System.out.printf("F2: %s%n", f2);
  
System.out.printf("F1+F1: %s%n", f1.plus(f1));
System.out.printf("F2+F2: %s%n", f2.plus(f2));
System.out.printf("F1+F2: %s%n", f1.plus(f2));
System.out.printf("F2+F1: %s%n", f2.plus(f1));
  
System.out.printf("F1-F1: %s%n", f1.minus(f1));
System.out.printf("F2-F2: %s%n", f2.minus(f2));
System.out.printf("F1-F2: %s%n", f1.minus(f2));
System.out.printf("F2-F1: %s%n", f2.minus(f1));
  
System.out.printf("F1*F1: %s%n", f1.times(f1));
System.out.printf("F2*F2: %s%n", f2.times(f2));
System.out.printf("F1*F2: %s%n", f1.times(f2));
System.out.printf("F2*F1: %s%n", f2.times(f1));
  
System.out.printf("F1/F1: %s%n", f1.divides(f1));
System.out.printf("F2/F2: %s%n", f2.divides(f2));
System.out.printf("F1/F2: %s%n", f1.divides(f2));
System.out.printf("F2/F1: %s%n", f2.divides(f1));
  
System.out.printf("F1*F1-F2: %s%n", f1.times(f1).minus(f2));
}

}

i have executed this code here

https://code.hackerearth.com/90000eB?key=523d541135a373c2286b5a427ed0c85c

input:

1 2 3 5

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote