Need help in java Thank you You must write a class name Frac

Need help in java. Thank you
You must write a class name Fraction made up of: A numerator and denominator, One default constructor that sets the Faction to 0/1 One constructor that accepts two parameters and sets the Fraction to those parameters. Two getters Two setters. A voided print method that will print a fraction in the format of for example 3/4 rightarrow public void print () An add function that adds two fractions - the one passed as a parameter will be added to the fraction object that calls this add function -- public Fraction add (Fraction a). Write a driver program name TestFraction to invoke these methods and test them. They must prompt user for the input values to create the fractions.

Solution

import java.util.*;
import java.lang.*;
import java.io.*;

final class Fraction {
private int numerator;
private int denominator;
public Fraction() {
this.numerator = 0;
this.denominator = 1;
}

public Fraction(int numerator, int denominator) {

this.numerator = numerator;
this.denominator = denominator;
}

  

public int getNumerator() {
return this.numerator;
}

public int getDenominator() {
return this.denominator;
}

public void setNumerator(int n) {
this.numerator=n;
}

public void setDenominator(int d) {
this.denominator=d;
}


public void print (){
System.out.println(this.numerator+\"/\" + this.denominator);
}


public int gcd(int a, int b)
{
if (b==0) return a;
return gcd(b,a%b);
}

public void simplify()
{
int gcd = gcd(this.numerator, this.denominator);
this.numerator /= gcd;
this.denominator /= gcd;
}

public Fraction add(Fraction a)
{
return new Fraction(this.numerator * a.getDenominator() + a.getNumerator() * this.denominator,
this.denominator * a.getDenominator());
}

}

class FractionTest {

public static void main(String[] args) {
PrintWriter stdout = new PrintWriter(System.out,true);
Fraction first = new Fraction(1,2);
Fraction second = new Fraction(3,2);
Fraction third;
Fraction fourth;


fourth = first.add(second);
stdout.print(\"The sum is: \");
stdout.println();
fourth.print();
  
}
}

Need help in java. Thank you You must write a class name Fraction made up of: A numerator and denominator, One default constructor that sets the Faction to 0/1
Need help in java. Thank you You must write a class name Fraction made up of: A numerator and denominator, One default constructor that sets the Faction to 0/1

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site