please help The Fraction Class is the definition for a Fraction object. Classes
ID: 3687010 • Letter: P
Question
please help
The Fraction Class is the definition for a Fraction object. Classes contain Attributes (the data) and Behaviors (the methods). The
Fraction Attributes
A fraction is an object that contains a numerator and denominator. Your Fraction class definition must therefore contain two private attributes to represent the numerator and denominator. These attributes should be integers.
The Fraction Behaviors
Your Fraction class must contain public definitions for the following methods. The methods are the public interface to a Fraction object so you are NOT allowed to change the return type, method name, or what is sent to the method (notice that nothing is sent to any of the methods). public String toString( ) This method returns a String that looks like the fraction (ex. 34/250). This method does NOT display anything on the screen. public double getDecimal( ) This method returns the decimal equivalent of the Fraction by dividing the numerator by denominator. public void reduce( ) This method potentially changes the numerator and denominator of the object by reducing the object to lowest terms. This is the only method that can change the numerator or denominator. reduce( ) is the only mutator in the Fraction class definition. public String toMixed( ) This method returns a String which includes the whole number and a “reduced” fractional component. NOTE: this method DOES NOT MODIFY the numerator or denominator of the fraction – it simply returns a String. For example, if the object is the Fraction 35/10, the toMixed( ) method will return the String: ”3 1/2" . Remember, the numerator will remain 35, and the denominator will remain 10, after this method is called. If there is no whole number part, do not display 0. You must also account for negative values in the numerator and denominator position when the toMixed() method is called.
The Constructor Methods:
Along with the above methods, you must also be able to instantiate, or create, a new Fraction object. This is done by using the new operator and calling the constructor method. Define the following overloaded constructor: Fraction(int n, int d) This constructor initializes the numerator and denominator of the object to the values (n and d) that are sent to the constructor. Fraction( ) This constructor initializes the numerator and denominator to
1. The Test Program (
main method) Test your Fraction class. You must also create a small test program (main method) in a separate java class file to test your fraction class. Your program should do the following: create 5 different Fraction objects. Hard code the values of the numerator and denominator so that the user does not have to enter these values. Part of your evaluation on this assignment is based on the quality of the Fractions that you instantiate to test your code. Next, allow the user to choose which method to perform. You could display a menu such as the following: Choose 1. Test the toString() method 2. Test the reduce() method 3. Test the toMixed() method etc. Be sure to include a choice to quit the program. When the user makes the choice, invoke that method on all 5 fraction objects, displaying the returned value of the fraction objects on the screen. Do not display anything if the method is void (in the case of reduce( ) ). Redisplay the menu so the user may choose other options before quitting the program. Hint: For ease of coding the main method, I strongly recommend that you create an array of 5 Fraction objects. If you don’t, you will not receive full credit for this assignment.
Explanation / Answer
Fraction.java
package org.students;
public class Fraction
{
//Declaring instance Variables.
private int numerator;
private int denominator;
//Default Constructor
public Fraction() {
super();
}
//Parameterized constructor
public Fraction(int numerator, int denominator) {
super();
this.numerator = numerator;
this.denominator = denominator;
}
//Method which will convert a fraction into decimal value
public double getDecimal()
{
return numerator/denominator;
}
//Method which will convert a fraction into reduced format
public String reduce( )
{
// determine the greatest common divisor
int gcd = this.greatestCommonDivisor(numerator, denominator);
// if GCD is negative, change to positive
if (gcd < 0)
{
gcd = -gcd;
}
// divide gcd into both numerator and denominator
numerator = numerator / gcd;
denominator = denominator / gcd;
return numerator+"/"+denominator;
}
//Method which will find Greatest Common Divisor
private Integer greatestCommonDivisor(Integer a, Integer b)
{
// "private"
// % is modulus which is the remainder of a division
// base case
if ((a % b) == 0) {
return b;
}
// recursive case
else {
return greatestCommonDivisor(b, a % b);
}
}
//Method which will convert a fraction into mixed fraction.
public String toMixed( )
{ int a=0,b=0;
if(numerator>denominator){
a = numerator/denominator;
b = numerator-(a*denominator);
}
return "["+a+" "+b+" / "+denominator+"]";
}
//Method which will display the contents of the Fraction Object.
@Override
public String toString() {
return "Fraction [" + numerator + "/" + denominator
+ "]";
}
}
__________________________________________________________________________________________
Test.java
package org.students;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Creating an array of Fraction Objects
Fraction fr[]={
new Fraction(8, 6),
new Fraction(3, 2),
new Fraction(9, 2),
new Fraction(28, 6),
new Fraction(8, 3),
new Fraction(17, 3)};
//Displaying menu
//Loop will continue to iterate until user enter 'N'
while (true) {
System.out.println("Menu");
System.out.println("1. Test the toString() method");
System.out.println("2. Test the reduce() method");
System.out.println("3. Test the toMixed() method");
System.out.println("4. Test the toDecimal() method");
System.out.println("5. Quit ");
System.out.print("select choice ::");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
//Switch Block
switch (num) {
case 1:
for(int i=0;i<6;i++)
{
String tostr= fr[i].toString();
System.out.println(tostr);
}
System.out.println("=============================");
continue;
case 2:
for(int i=0;i<6;i++)
{
String str1 = fr[i].reduce();
System.out.println("The Reduced form is:"+str1);
}
System.out.println("=============================");
continue;
case 3:
for(int i=0;i<6;i++)
{
String str1 = fr[i].toMixed();
System.out.println("The Mixed form is:"+str1);
}
System.out.println("=============================");
continue;
case 4:
for(int i=0;i<6;i++)
{
double deci= fr[i].getDecimal();
System.out.println("The Decimal Number is:"+deci);
}
System.out.println("=============================");
continue;
case 5:
System.out.println(":: Program Exit ::");
System.exit(0);
break;
}
}
}
}
__________________________________________________________________________________________________
output:
Menu
1. Test the toString() method
2. Test the reduce() method
3. Test the toMixed() method
4. Test the toDecimal() method
5. Quit
select choice ::1
Fraction [8/6]
Fraction [3/2]
Fraction [9/2]
Fraction [28/6]
Fraction [8/3]
Fraction [17/3]
=============================
Menu
1. Test the toString() method
2. Test the reduce() method
3. Test the toMixed() method
4. Test the toDecimal() method
5. Quit
select choice ::2
The Reduced form is:4/3
The Reduced form is:3/2
The Reduced form is:9/2
The Reduced form is:14/3
The Reduced form is:8/3
The Reduced form is:17/3
=============================
Menu
1. Test the toString() method
2. Test the reduce() method
3. Test the toMixed() method
4. Test the toDecimal() method
5. Quit
select choice ::3
The Mixed form is:[1 1 / 3]
The Mixed form is:[1 1 / 2]
The Mixed form is:[4 1 / 2]
The Mixed form is:[4 2 / 3]
The Mixed form is:[2 2 / 3]
The Mixed form is:[5 2 / 3]
=============================
Menu
1. Test the toString() method
2. Test the reduce() method
3. Test the toMixed() method
4. Test the toDecimal() method
5. Quit
select choice ::4
The Decimal Number is:1.0
The Decimal Number is:1.0
The Decimal Number is:4.0
The Decimal Number is:4.0
The Decimal Number is:2.0
The Decimal Number is:5.0
=============================
Menu
1. Test the toString() method
2. Test the reduce() method
3. Test the toMixed() method
4. Test the toDecimal() method
5. Quit
select choice ::5
:: Program Exit ::
_________________________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.