In: Computer Science
Java
Implement a class named “Fraction” with the following properties:
numerator: int type, private
denominator: int type, private
and the following methods:
one default constructor which will create a fraction of 1/1.
one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters.
int getNum() : retrieves the value of numerator
int getDenom(): retrieves the value of the denominator
Fraction add(Fraction frac): adds with another Fraction number and returns the result in a new Fraction object.
Fraction sub(Fraction frac): is subtracted by another Fraction frac and returns the result in a new Fraction object.
Fraction mult(Fraction frac): multiplies with another Fraction number and returns the result in a new Fraction object.
Faction div(Fraction frac): is divided by another Fraction number and returns the result in a new Fraction object.
void print(): prints the Fraction number out
This is what I have so far, but I am getting hung up..
import java.util.Scanner;
public class Fraction {
private int numerator;
private int denominator;
//Default constructor that sets 1/1 fraction
Fraction(){
numerator=1;
denominator=1;
}
//Constructor that creates fraction with specified parameters
Fraction(int newNumerator, int newDenominator){
numerator = newNumerator;
denominator = newDenominator;
}
//Retreives value of numerator
public int getNum(){
return numerator;
}
//Retreives value of denominator
public int getDenom(){
return denominator;
}
//Adds fraction with another and returns value as Fraction
object
public Fraction add(Fraction frac){
if(frac.denominator==denominator){
int num= frac.numerator+numerator;
int denom = denominator;
}else{
int denom = this.getDenom() * frac.getDenom();
int num = this.getNum()*frac.getDenom() +
frac.getNum()*this.getDenom();
}
return new Fraction(num,denom);
}
//Subtracts fraction with another and returns value as Fraction
object
public Fraction sub(Fraction frac){
int num = frac.numerator+numerator;
int denom= frac.denominator-denominator;
return new Fraction(num,denom);
}
//Multiplies fraction with another and returns value as Fraction
object
public Fraction mult(Fraction frac){
int num = frac.getNum()*numerator;
int denom = frac.getDenom()*denominator;
return new Fraction(num,denom);
}
//Divides fraction with another and returns value as Fraction
object
public Fraction div(Fraction frac){
int num = frac.getDenom()*numerator;
int denom = frac.getNum()*denominator;
return new Fraction(num,denom);
}
public void print(){
System.out.print(numerator + "/" + denominator);
}
import java.util.*;
class Fraction{
private int numer;
private int denom;
private Fraction answer;
public Fraction()
{
numer = 1;
denom = 1;
}
//Definition
public Fraction(int num, int den)
{
numer = num;
denom = den;
simplify();
}
//Simplify
void simplify()
{
int gcd = findGCD(numer, denom);
numer /= gcd;
denom /= gcd;
}
//GCD Function
int findGCD(int a, int b)
{
if(b==0)
return a;
return findGCD(b,a%b);
}
//GetNumerator
public int getNumer()
{
return numer;
}
//GetDenominator
public int getDenom()
{
return denom;
}
//Fraction Add Method
Fraction add(Fraction x)
{
Fraction answer;
if(denom == x.denom)
{
answer = new Fraction(x.numer + numer, denom);
}
else
{
int den = this.getDenom() * x.getDenom();
int num = this.getNumer() * x.getDenom() + x.getNumer() *
this.getDenom();
answer = new Fraction(num, den);
}
return answer;
}
//Fraction Subtract Method
Fraction subtract(Fraction x)
{
Fraction answer;
if(x.denom == denom)
{
answer = new Fraction(numer - x.numer, denom);
}
else
{
int den = this.getDenom() * x.getDenom();
int num = this.getNumer() * x.getDenom() - x.getNumer() *
this.getDenom();
answer = new Fraction(num, den);
}
return answer;
}
//Fraction Multiply Method
Fraction multiply(Fraction x)
{
Fraction answer;
int num = numer *
x.getNumer();
int den = denom * x.getDenom();
answer = new Fraction(num, den);
return answer;
}
//Fraction Divide Method
Fraction divide(Fraction x)
{
Fraction answer;
int num = numer * x.getDenom();
int den = denom * x.getNumer();
answer = new Fraction(num, den);
return answer;
}
//@Override
public boolean equals(Fraction x)
{
boolean answer = false;
if(numer == x.numer && denom == x.denom)
{
answer = true;
}
return answer;
}
//Prints Method
public String prints()
{
return (Integer.toString(numer) + "/"
+Integer.toString(denom));
}
//Main Method
public static void main(String[]args)
{
Scanner sc = new
Scanner(System.in);
Fraction a = new Fraction();
Fraction b = new Fraction();
a.numer = sc.nextInt();
a.denom = sc.nextInt();
a = new Fraction(a.numer,
a.denom);
b.numer = sc.nextInt();
b.denom = sc.nextInt();
b = new Fraction(b.numer,
b.denom);
int choice = sc.nextInt();
switch(choice)
{
case 1:
Fraction c = a.add(b);
System.out.println(c.prints());
break;
case 2:
c = a.subtract(b);
System.out.println(c.prints());
break;
case 3:
c = a.multiply(b);
System.out.println(c.prints());
break;
case 4:
c =
a.divide(b);
System.out.println(c.prints());
break;
}
}
}