Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

TestFraction.java import java.util.Scanner ; public class TestFraction { private static Scanner in; public static void main(String[] args) { in = new Scanner(System.in); int numerator, denominator

image text in transcribed

image text in transcribed

TestFraction.java

import java.util.Scanner ;

public class TestFraction { private static Scanner in;

public static void main(String[] args) { in = new Scanner(System.in); int numerator, denominator ; while(true) { System.out.print("Enter a numerator : "); numerator = in.nextInt(); if(numerator > 0) { System.out.print("Enter a denominator : "); denominator = in.nextInt(); Fraction f = new Fraction(numerator, denominator); System.out.println("Your fraction is: " + f); } else { System.out.println("exiting"); System.exit(0); } }

} }

-----------------------------------------------------------------------------

Fraction.java

public class Fraction { int numerator ; int denominator ; public Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; if(this.numerator >= 0 && this.denominator Before starting the exercises, make sure you have read the relevant material. 1. Add another public method called add() to your Fraction class. This method adds another fraction to the calling object. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: we can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions. 2. Write a client program that loops around getting two fractions from the user, output their sum, and state whether or not they are equal. Keep looping until the first fraction entered is a zero fraction. 3. Update your UML diagram from last week to include the added behaviour of your Fraction class. 4. Add another public method dblValue() to your Fraction class which returns the double precision approximation value of the fraction. That is, the floating point result of actually dividing numerator by denominator. N.B. this method does not do any display itself, but can be called by a client program to be used in an output statement. Eg: if a client has a fraction frac that represents 1/2, then a method call to frac.dblValue() should return the double number 0.5. Use your client program to test this functionality; i.e. provide an output statement to display the double value of a fraction. 5. Add a toString() method to your Fraction class that returns the fraction as a String in the form "x / y", where x and y are numerator and denominator respectively. This is similar to your display method, but it does not actually do any output itself; it only returns the Fraction as a String -- nothing else. N.B. as the method does not do any display itself, the output can be done by a client program that calls the method in an output statement. Use your client program to test this functionality; i.e. provide an output statement to display a fraction as its String representation. 6. Add a private method simplify() to your Fraction class that converts a fraction to its simplest form. For example, the fraction 20 / 60 should be stored in the class instance variables as 1 / 3 (i.e. numerator = 1, denominator = 3). N.B. you will need a method to determine the Greatest Common Divisor (GCD. Remember, both of these methods (simplify and ged) must be private. As these methods are private, client programs cannot access them. So, how are they to be used? They can only be accessed within the Fraction class. Given their purpose, it would mean that any Fraction class method that modifies the instance variables (e.g.: input, add, constructor, set) should call the simplify0 method to reduce the instance variables to their minimum values. Thus, these methods are used only for housekeeping, they are not to be used by client programs. 7. Write a client program that allows the user to add up any number of non-zero fractions. The program should display the running total as an exact fraction (in simplified form as numerator / denominator), and as an approximate double value. The user can finish by entering a fraction that represents a zero fraction. 8. Update your UML diagram from point 3 to include all added behaviour of your Fraction class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

Students also viewed these Databases questions

Question

LO4 Identify a system for controlling absenteeism.

Answered: 1 week ago