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: 3605773 • 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.

Explanation / Answer

import java.util.*;

class Fraction

{

private int numerator;

private int denominator;

public Fraction() //default constructor

{

numerator = 0;

denominator = 1;

}

public Fraction(int numerator,int denominator) //parameterized constructor

{

this.numerator = numerator;

this.denominator = denominator;

}

public Fraction add(Fraction f1,Fraction f2) // add two fractions

{

this.numerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator;

this.denominator = f1.denominator * f2.denominator;

return this;

}

public Fraction sub(Fraction f1,Fraction f2) // subtract a fraction from another

{

this.numerator = f1.numerator * f2.denominator - f2.numerator * f1.denominator;

this.denominator = f1.denominator * f2.denominator;

return this;

}

public Fraction mul(Fraction f1,Fraction f2) //multiply two fractions

{

this.numerator = f1.numerator * f2.numerator;

this.denominator = f1.denominator * f2.denominator;

return this;

}

public Fraction div(Fraction f1,Fraction f2) //division of fractions

{

this.numerator = f1.numerator * f2.denominator;

this.denominator = f1.denominator * f2.numerator;

return this;

}

public String toString() //override toString() method

{

return " "+numerator+"/"+denominator;

}

public boolean equals(Fraction f) //compare two fractions

{

if(numerator == f.numerator && denominator == f.denominator)

return true;

else

return false;

}

}

class FractionDemo

{

public static void main (String[] args)

{

Scanner scan = new Scanner(System.in);

System.out.println("Enter the numerator and denominator of f1 ->");

int num1 = scan.nextInt();

int den1 = scan.nextInt();

Fraction f1 = new Fraction(num1,den1);

System.out.println("Enter the numerator and denominator of f2 ->");

int num2 = scan.nextInt();

int den2 = scan.nextInt();

Fraction f2 = new Fraction(num2,den2);

System.out.println("f1 = "+f1.toString());

System.out.println("f2 = "+f2.toString());

Fraction f3 = new Fraction();

Fraction f4 = new Fraction();

f3 = f3.mul(f1,f2);

f4 = f4.mul(f2,f1);

System.out.println("f3 = f1 * f2 = "+ f3.toString());

System.out.println("f4 = f2 * f1 = "+ f4.toString());

   if(f3.equals(f4))

   System.out.println( f3.toString()+ " and" + f4.toString() + " are equal");

   else

   System.out.println( f3.toString()+ " and" + f4.toString() + " are not equal");

}

}

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