Question

In: Computer Science

Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...

Java

Implement a class named “Fraction” with the following properties:

  1. numerator: int type, private

  2. denominator: int type, private

and the following methods:

  1. one default constructor which will create a fraction of 1/1.

  2. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters.

  3. int getNum() : retrieves the value of numerator

  4. int getDenom(): retrieves the value of the denominator

  5. Fraction add(Fraction frac): adds with another Fraction number and returns the result in a new Fraction object.

  6. Fraction sub(Fraction frac): is subtracted by another Fraction frac and returns the result in a new Fraction object.

  7. Fraction mult(Fraction frac): multiplies with another Fraction number and returns the result in a new Fraction object.

  8. Faction div(Fraction frac): is divided by another Fraction number and returns the result in a new Fraction object.

  9. 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);
}

Solutions

Expert Solution

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;

       }

}

}


Related Solutions

How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
The denominator of a fraction is 4 more than the numerator. If both the numerator and...
The denominator of a fraction is 4 more than the numerator. If both the numerator and the denominator of the fraction are increased by 3, the new fraction is 5/6 . Find the original fraction.
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2 can be expressed as the list [1,2]. (a) Write a function called factionAdd() that takes two fractions as lists and adds them. For example, fraction([1,2], [3,4]) returns [5,4] (b) Write a function fractionMult() that multiplies two fractions that are passed as lists. [HINT: You may use the following function gcd ( x ; y ) to help you calculate the Greatest Common Divisor, which...
JAVA -The next three questions use the following class: class Identification { private int idNum;   ...
JAVA -The next three questions use the following class: class Identification { private int idNum;    public Identification() { this(0); }    public Identification(int startingIdNum) { idNum = startingIdNum; }    public int getIdNum() { return idNum; }    public void setIdNum(int idNum) { this.idNum = idNum; } } Here is one program using the above class: public class Main {    public static void main(String[] args) {        Identification i1 = new Identification();        Identification i2 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT