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

ITCS 1213 Lab 7 Purpose: To practice designing a class that is useful for many a

ID: 3606341 • Letter: I

Question

ITCS 1213 Lab 7 Purpose: To practice designing a class that is useful for many applications. To create an object in a method. To write a method that returns a reference. To practice writing a test driver to test all the methods of a class. This is a lab requiring logic and Java skills. We suggest you do this lab with a lab partner. Use the Linear Measure class and Driver we went over in lecture this week. Step 1. You will be writing a class to hold data and perform operations on fractions. You will then test this class by writing a Driver class that tests each of the methods as you write them. Before you write any code, perform each of these operations, taking notes with your lab partner of what you are doing at each step. You must show this completed document to your TA before starting to write any code. 1. Simplify: Original Simplified Notes 8/16 6/15 8/12 14/24 9/21 2. Add and express sum in simplest form: Problem Answer Notes 3/5 + 1/5 3/8 + 5/12 5/6 + 1/5 4/9 + ¾ 3. Multiply and express product in simplest form: Problem Answer Notes ¾ * 2/3 1/5 * 3/7 2/5 * ¾ 4. Divide and express each quotient in simplest form: Problem Answer Notes 4/5 / ¼ 4/9 / 15/20 3/8 / 5/6 12/5 / 20/12 5. Compare these fractions: answer in form of ¾ > ¼ 0r 5/8 < 7/8 Problem Answer Notes 2/5 to 1/3 3/7 to 4/9 5/6 to 4/7 4/5 to 7/8 Step 2: Write a class to hold data and methods for a Fraction. The integer fields will hold the values for the numerator and denominator of a fraction. Each time you write a method, test that method in your driver class. Do not wait until you have written every method before testing. TEST AS YOU WRITE. In addition to the overloaded constructor methods write methods that will perform the following eight operations. When you create the Fraction do not accept zero as a value for the denominator. add two fractions, returning new Fraction reference that is the sum in lowest terms parameter: a reference to another Fraction return type: reference to a new Fraction subtract two fractions- returning a new Fraction reference that is difference in lowest terms (simplify) parameter: a reference to another Fraction return type: a reference to a new Fraction multiply two fractions-returning the product in lowest terms parameter: a reference to another Fraction, return type – a reference to new Fraction that is the product of the two Fractions divide two fractions-returning the quotient in lowest terms parameter: a reference to another Fraction, return type: a reference to a new Fraction that is the quotient of the two Fractions compareTo( ) method parameter: a reference to a Fraction equals( ) method parameter: a reference to a Fraction a method to determine and return the decimal value of a fraction no parameters, return type: a double a static method to generate and return a reference to a random fraction that is between 0 and 1. (this means the numerator is less than the denominator) toString( ) method that returns a reference to a String in the form of a/b Here is a simple way to find the greatest common divisor of two numbers: Start with the smaller of the two numbers. Determine if this number goes into the larger of the two numbers evenly. If it does, this is the greatest common divisor, if not; repeatedly subtract 1 from the value of the smallest number until you find a value that goes into both numbers evenly. Step 3: Write a driver class with the main( ) method that tests each of the methods of the class. Be sure to print the output for each test case. Create a project named Fraction Driver using the previous class. In this class code the main method that performs these tasks. Write the code to ask the user if he/she wants to practice with adding, subtracting, multiplying or dividing fractions. Once the user chooses an option, present five problems using the fraction methods to create two random fractions. Present the problem to the user. Read the user answer (numerator and denominator) then use your equals method to determine if the user answer is correct. Display a message stating whether the user answer is correct or not. If it is not correct, print the correct answer. Once the five questions are complete, repeat the menu. Continue repeating until the user indicates he/she is finished.

Explanation / Answer

import java.util.Scanner;

public class Fraction{

private long num,den; // the numerator and denominator

// constructor to build a fraction of form a/b

public Fraction(long a,long b){

num = a;

den = b;

reduce(); // reduce to simplest form

}

// constructor to build a fraction with 1 as denominator

public Fraction(long a){

num = a;

den = 1;

}

// a function to multiply 2 fractions a and b

public static Fraction multiply(Fraction a, Fraction b){

// create a copy of fraction a

Fraction ans = new Fraction(a.num, a.den);

// multiply corresponding integers i.e numerator and denominator

ans.num *= b.num;

ans.den *= b.den;

// simplify the result

ans.reduce();

return ans;

}

// a function to add 2 fractions a and b

public static Fraction add(Fraction a, Fraction b){

long gcdOfDen = gcd(a.den, b.den);

long lcmOfDen = (a.den * b.den)/gcdOfDen;

long num = ((a.num * lcmOfDen)/a.den) + ((b.num * lcmOfDen)/b.den);

Fraction ans = new Fraction(num, lcmOfDen);

ans.reduce();

return ans;

}

// a function to find difference of 2 fractions a and b

// note that taking difference of 2 fractions

// guarentees the result to be positive.

public static Fraction difference(Fraction a, Fraction b){

long gcdOfDen = gcd(a.den, b.den);

long lcmOfDen = (a.den * b.den)/gcdOfDen;

long num = ((a.num * lcmOfDen)/a.den) - ((b.num * lcmOfDen)/b.den);

num = (long)Math.abs(num);

Fraction ans = new Fraction(num, lcmOfDen);

ans.reduce();

return ans;

}

// a function to divide 2 fractions

public static Fraction divide(Fraction a, Fraction b){

// dividing 2 fractions is same as multiplying one with

// reciprocal of another

b.reciprocal(); // take reciprocal first

Fraction ans = multiply(a,b);

b.reciprocal(); // take reciprocal again so as to prevent any data loss

return ans;

}

// a function to compare 2 fractions

public int compareTo(Fraction f){

long gcdOfDen = (this.den * f.den) / gcd(this.den, f.den);

long a = (this.num * gcdOfDen) / this.den;

long b = (f.num * gcdOfDen) / f.den;

if(a > b)

return 1;

else{

if(a < b)

return -1;

else

return 0;

}

}

// a function to find decimal value of this fraction

// correct to 3 decimal places

public double equals(){

double ans = num;

ans /= den;

ans = (double)Math.round(ans * 1000d) / 1000d; // round off to 3 decimal places

return ans;

}

// a random function that creates a random Fraction

// which is less than 1

// For simplicity, this function will return a fraction

// whose numerator will be max 10, and

// denominator will be max 20

public static Fraction random(){

double a = Math.random();

a *= 100;

int num = (int)a;

num %= 11;

double b = Math.random();

b *= 100;

int den = (int)a;

den %= 20;

den += 11; // to ensure we get a fraction less than 1

Fraction ans = new Fraction(num, den);

return ans;

}

// a function to convert Fraction into a string

// of form a/b

public String toString(){

StringBuilder ans = new StringBuilder();

ans.append(num);

ans.append('/');

ans.append(den);

return ans.toString();

}

// utility function to reduce a fraction

// into simplest form

private void reduce(){

long hcf = gcd(num,den);

num /= hcf;

den /= hcf;

}

// utility function to find GCD of 2 numbers

// using Euclid's method

private static long gcd(long a, long b){

if(a==0) return b;

else return gcd(b%a,a);   

}

// utility function to take reciprocal of a fraction

private void reciprocal(){

long temp = num;

num = den;

den = temp;

}

}

// Driver Class

class Main{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.println("Do you want to practice with adding, subtracting, multiplying or dividing fractions?");

String userInput = input.nextLine();

while(userInput.charAt(0) == 'Y' || userInput.charAt(0) == 'y'){

System.out.println("We have 5 questions for you:-");

for(int i = 1; i <= 5; i++){

Fraction a = Fraction.random(), b = Fraction.random();

System.out.println("Question #"+i+" :");

System.out.println("Given 2 fractions - "+a.toString() + " and "+b.toString());

System.out.println("What is the result of adding them? (Enter in decimal form correct upto 3 decimal places) - ");

double ans = input.nextDouble();

Fraction add = Fraction.add(a,b);

if(add.equals() == ans)

System.out.println("Corrent Answer!!");

else

System.out.println("Incorrent Answer! The correct answer should be - "+add.equals());

System.out.println("What is the result of taking their difference? (Enter in decimal form correct upto 3 decimal places) - ");

ans = input.nextDouble();

Fraction diff = Fraction.difference(a,b);

if(diff.equals() == ans)

System.out.println("Corrent Answer!!");

else

System.out.println("Incorrent Answer! The correct answer should be - "+diff.equals());

System.out.println("What is the result of multiplying them? (Enter in decimal form correct upto 3 decimal places) - ");

ans = input.nextDouble();

Fraction mult = Fraction.multiply(a,b);

if(mult.equals() == ans)

System.out.println("Corrent Answer!!");

else

System.out.println("Incorrent Answer! The correct answer should be - "+mult.equals());

System.out.println("What is the result of dividing them? (Enter in decimal form correct upto 3 decimal places) - ");

ans = input.nextDouble();

Fraction div = Fraction.divide(a,b);

if(div.equals() == ans)

System.out.println("Corrent Answer!!");

else

System.out.println("Incorrent Answer! The correct answer should be - "+div.equals());

System.out.println("-------------------------------------------------- ");

}

System.out.println("Do you want to practice with adding, subtracting, multiplying or dividing fractions?");

userInput = input.nextLine();

}

}

}

For better visibility of code, kindly use a code-editor.
Let me know if there are any errors or you need more explanation regarding the code.
Hope it helps!!

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