a) Write a Fraction class that implements these methods: add This method receive
ID: 3662731 • Letter: A
Question
a) Write a Fraction class that implements these methods:
add This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.
multiply This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.
print This method prints the fraction using fraction notation (1/4, 21/14, etc.)
printAsDouble This method prints the fraction as a double (0.25, 1.5, etc.)
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
b) Modify the code so that it can handle negative numerators and negative denominators, and provide a helping method that performs fraction reduction.
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
Explanation / Answer
Driver Program:
}
Function program:
import Function.java; public class FirstnameLastnameProg7 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Fraction a, b, c; // Fraction objects System.out.println("Enter numerator; then denominator."); a = new Fraction(stdIn.nextInt(), stdIn.nextInt()); a.print(); System.out.println("Enter numerator; then denominator."); b = new Fraction(stdIn.nextInt(), stdIn.nextInt()); b.print(); c = new Fraction(); System.out.println("Sum:"); c.add(a).add(b); c.print(); } // end main}
Function program:
public class Function { //declares that it's a class that can be intantiated private int numer; // private intager for numerator private int denom; // private intager for denominator public Function( int numer, int denom ) //constructor that handles the private intagers { this.numer = numer; this.denom = denom; } // end of constructor //Accessor methods public Fraction add (Function fracB) //this adds the fractions together { numer = (numer * fracB.denom) + (fracB.numer * denom); // adds the numeraton demon = denom * fracB.denom;//adds the deniminator return this;//returns result } public Fraction multiply (Function fracB)//this multiplies the fractions { numer = (numer * fracB.numer);//multiplies the numerator denom = (denom * fracB.denom);//multiplies the denominator return this;//returns the result } public void print ( )// this prints the fraction { { System.out.println(numer + "/" + denom);// prints the fraction to screen } } public void printDouble ( )//prints fraction as a double { double decimal = (double) numer / denom;// converts to a decimal System.out.println(decimal);// prints the decimal } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.