In: Computer Science
Given the main method of a driver class, write a Fraction class. Include the following instance methods:
add,
multiply,
print,
printAsDouble,
and a separate accessor method for each instance variable.
Separate accessor methods for each instance variable in the Fraction class.
Provide a driver class, FractionDemo, that demonstrates this Fraction class. The driver class should contain this main method:
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Fraction c, d, x; // Fraction objects
System.out.println("Enter numerator; then denominator.");
c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
c.print();
System.out.println("Enter numerator; then denominator.");
d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
d.print();
x = new Fraction(); // create a fraction for number 0
System.out.println("Sum:");
x.add(c).add(d);
x.print();
x.printAsDouble();
x = new Fraction(1, 1); // create a fraction for number 1
System.out.println("Product:");
x.multiply(c).multiply(d);
x.print();
x.printAsDouble();
System.out.println("Enter numerator; then denominator.");
x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
x.printAsDouble();
} // end main
Note that this demonstration driver does not call the accessor methods. That’s OK. Accessor methods are often implemented regardless of whether there’s an immediate need for them. That’s because they are very useful methods in general and providing them means that future code can use them when the need arises.
Sample session:
Enter numerator; then denominator.
5
8
5/8
Enter numerator; then denominator.
4
10
4/10
Sum:
82/80
1.025
Product:
20/80
0.25
Enter numerator; then denominator.
6
0
infinity
Sample session using negative numbers and reduction:
Enter numerator; then denominator.
-5
-8
5/8
Enter numerator; then denominator.
4
-10
-2/5
Sum:
9/40
0.225
Product:
-1/4
-0.25
Enter numerator; then denominator.
0
-0
indeterminate
Fraction.java
public class Fraction {
private int numerator;
private int denominator;
Fraction(){
numerator = 0;
denominator = 1;
}
Fraction(int n, int d){
//If both numerator and denominator
are negative
if(n<0 && d<0)
{
this.numerator =
-n;
this.denominator
= -d;
}
//If numerator is positive,
denominator is negative
else if (n>0 &&
d<0){
numerator =
-n;
denominator =
-d;
}
//Otherwise
else {
numerator =
n;
denominator =
d;
}
}
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
//Adds Fraction f to the
current Fraction object, and returns new Fraction
public Fraction add(Fraction f) {
int num =
this.numerator*f.denominator + this.denominator*f.numerator;
int denom =
this.denominator*f.denominator;
this.numerator = num;
this.denominator = denom;
return this;
}
//Multiplies Fraction f to the current Fraction
object, and returns new Fraction
public Fraction multiply(Fraction f) {
int num = this.numerator *
f.numerator;
int denom = this.denominator *
f.denominator;
this.numerator = num;
this.denominator = denom;
return this;
}
public void print() {
if(numerator == 0 &&
denominator == 0)
System.out.println("Indeterminate");
else if (denominator==0)
System.out.println("Infinity");
else
System.out.println(numerator + "/" + denominator);
}
public void printAsDouble() {
System.out.println((double)numerator/denominator);
}
}
FractioDemo.java
import java.util.Scanner;
public class FractionDemo {
public static void main(String[] args) {
Scanner stdIn = new
Scanner(System.in);
Fraction c, d,
x; // Fraction objects
System.out.println("Enter
numerator; then denominator.");
c = new Fraction(stdIn.nextInt(),
stdIn.nextInt());
c.print();
System.out.println("Enter
numerator; then denominator.");
d = new Fraction(stdIn.nextInt(),
stdIn.nextInt());
d.print();
x = new
Fraction(); // create a fraction for number
0
System.out.println("Sum:");
x.add(c).add(d);
x.print();
x.printAsDouble();
x = new Fraction(1, 1); // create a
fraction for number 1
System.out.println("Product:");
x.multiply(c).multiply(d);
x.print();
x.printAsDouble();
System.out.println("Enter
numerator; then denominator.");
x = new Fraction(stdIn.nextInt(),
stdIn.nextInt());
x.printAsDouble();
}
}