Write a driver and fraction class
Not what you're looking for?
Basic Requirements
Write a driver and fraction class that performs addition, multiplication, prints the fraction, and prints as a double.
YourNameProg7.java
Provide a driver class, YourNameProg7, 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.
16
Fraction.java
Write a Fraction class called Fraction.java 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.
Sample Output
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
Purchase this Solution
Solution Summary
The expert writes a driver and fraction class which provides addition and multiplications.
Purchase this Solution
Free BrainMass Quizzes
Word 2010: Tables
Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.
Word 2010: Table of Contents
Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.
Inserting and deleting in a linked list
This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.
C++ Operators
This quiz tests a student's knowledge about C++ operators.
Javscript Basics
Quiz on basics of javascript programming language.