Creating the Lab3 and Fraction Classes
-
Create a new project with the name "Lab3"
-
Create a new package with the name "lab3"
-
Create two new classes:
-
Lab3 - this will serve as our driver and contain our main method. It will read in input from the user, create instances of the Fraction object, and print output.
-
Fraction - this class will represent a fraction. It will implement methods for usual tasks done with fractions (adding, multiplying, converting to decimal, etc.).
-
Copy and paste the following code into Lab3. This is
the code that will drive and test Fraction. We'll be un-commenting
sections of the code as we implement Fraction.
package lab3;
import java.util.Scanner;
/**
* Lab3.java - driver to test the Fraction class.
*
*/
public class Lab3 {
/**
* Driver to test the Fraction class.
* It reads input from user to construct instances of the Fraction and tests all methods of Fraction
*
* @param args Command-line arguments
*/
public static void main( String[] args )
{
/*
Scanner scanner = new Scanner(System.in);
int numerator = 1;
int denominator = 1;
System.out.println("Enter the numerator of the first fraction ");
numerator = scanner.nextInt();
System.out.println("Enter the denominator of the first fraction ");
denominator = scanner.nextInt();
Fraction firstFraction = new Fraction(numerator, denominator);
System.out.println("The first fraction is " + firstFraction.toString());
Fraction reciprocalFirstFraction = new Fraction(denominator, numerator);
System.out.println("The reciprocal of first fraction is " + reciprocalFirstFraction.toString());
System.out.println("Enter the numerator of the second fraction ");
numerator = scanner.nextInt();
System.out.println("Enter the denominator of the second fraction ");
denominator = scanner.nextInt();
Fraction secondFraction = new Fraction(numerator, denominator);
System.out.println("The Second Fraction is " + secondFraction.toString());
Fraction reciprocalSecondFraction = new Fraction(denominator, numerator);
System.out.println("The reciprocal of the second fraction is " + reciprocalSecondFraction.toString());
System.out.printf("The decimal value of the first fraction is %.2f\n", firstFraction.decimalValue());
System.out.printf("The decimal value of the second fraction is %.2f\n" , secondFraction.decimalValue());
System.out.println("The product of the two fractions is " + firstFraction.multiply(secondFraction).toString());
System.out.println("Are these fractions equivalent? " + firstFraction.equals(secondFraction));
*/
}
}