Creating the Lab3 and Fraction Classes
-
Open the project created in our first lab "LabAssignments"
-
Create a new package with the name "lab3"
-
Create two new classes:
-
FractionDriver - 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 FractionDriver. 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;
/**
* FractionDriver.java - driver to test the Fraction class.
* @version
* @author
* @Lab CMSC 202 - Spring 2012
* @section
*/
public class FractionDriver {
/**
* Driver to test the Fraction class.
* Precondition: None
* Postcondition: None
* @param radius: args Command-line arguments
* @return: None
*/
public static void main(String[] args) {
/*
//Create a Scanner object on System.in
Scanner scanner = new Scanner(System.in);
int numerator = 1;
int denominator = 1;
//Use the Scanner object to get the numerator and denominator of the first fraction
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();
//Create the first fraction object with input provided and print by invoking toString()
Fraction firstFraction = new Fraction(numerator, denominator);
System.out.println("The first fraction is " + firstFraction.toString());
//Create and print the reciprocal of the first fraction
Fraction reciprocalFirstFraction = new Fraction(denominator, numerator);
System.out.println("The reciprocal of first fraction is " + reciprocalFirstFraction);
//Use the Scanner object to get the numerator and denominator of the second fraction
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();
//Create the second fraction object with input provided and print by invoking toString()
Fraction secondFraction = new Fraction(numerator, denominator);
System.out.println("The Second Fraction is " + secondFraction.toString());
//Create and print the reciprocal of the second fraction
Fraction reciprocalSecondFraction = new Fraction(denominator, numerator);
System.out.println("The reciprocal of the second fraction is " +
reciprocalSecondFraction);
*/
/*
//Print the decimal value of the two fractions by invoking decimalValue()
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());
*/
/*
//Print the product of the fractions by invoking multiply()
System.out.println("The product of the two fractions is " +
firstFraction.multiply(secondFraction).toString());
//Print whether or not the fractions are equivalent by invoking equals()
System.out.println("Are these fractions equivalent? " +
firstFraction.equals(secondFraction));
*/
}
}