Question: Part A ) 1. Implement a class (eg, Fraction.java) for handling fractions. The Fraction class has two instance variables - an integer numerator and an

Part A)

1. Implement a class (eg, Fraction.java) for handling fractions. The Fraction class has two instance variables - an integer numerator and an integer denominator. The two instance variables can be publicly accessible. Supply an input method to get a fraction (as two separate integers) from the user. Supply an output method to print out the fraction on the screen. The fraction is to be outputted in the form: numerator/denominator. Half, for example, should be printed out as 1 / 2 and not 0.5.

Implement a client program (eg FractionDemo.java) to test your fraction class. The program should loop around getting fractions from the user and displaying them on the screen. Stop when the numerator is negative.

Note that the client program must use the fraction class methods to read and display fractions.

Provide the UML class diagram for the Fraction class - this can be submitted as a document file.

Part B)

1. Change your fraction class so that its instance variables numerator and denominator are hidden (i.e. private). Check that your old client class will now not compile (i.e. if the Fraction class instance variable name 'numerator' was used in the client class). Explain why.

2. Add a public method isZero to your fraction class for testing whether the calling fraction is equal to the number zero (instead of a negative number). Modify your old client class so that it uses the isZero method to loop until a numerator of zero is entered.

Provide the UML class diagram for the new fraction class.

3. Add two more public methods: equals which determines whether two fractions represent the same number (compare both the numerator and denominator of the two fractions) and add which adds another fraction to the calling fraction and returns a fraction result. The client should loop around getting two fractions from the user each time and output their sum and whether or not they are equal. Keep looping until the first fraction's numerator is zero.

Provide the updated UML class diagram for the fraction class.

4. Now you are required to change the implementation of your fraction class without making any changes to the client program from question 3. You are required to ensure that fractions are stored with denominators which are positive integers. You can require the user to only enter such fractions but you should check their input and request them to re-enter the denominator if it is invalid.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!