Question

In: Computer Science

Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...

Objectives:

1. Create classes to model objects

Instructions:

1. Create a new folder named Lab6 and save all your files for this lab into this new folder.

2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs

3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide.

Problem : The Fraction class (Filename: TestFraction.java)

Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead of returning 6 / 9, the function should return 2 / 3 as a string. This will require finding the greatest common divisor for the numerator and the denominator, and then dividing both by that number. Include a private method that returns the greatest common divisor of two integers.

Write a test program that tests several examples of fractions and displays the results.

Solutions

Expert Solution

Program code:

class Fraction
{
int numerator,denominator;
double res;
int getNumerator()               //Method to get Numerator
{
return numerator;
}
void setNumerator(int numerator)          //Method to set Numerator
{
this.numerator=numerator;
}
int getDenominator()          //Method to get Denominator
{
return denominator;
}
void setDenominator(int denominator)          //Method to set Denominator
{
this.denominator=denominator;
}
double result()                                  //Method to return the fractional value in double datatype.
{
res=(double)numerator/(double)denominator;
return res;
}
String reducedFraction()                       //Method to return the reduced fraction after numerator,denominator           

                                                           divisible by gcd;
{
int gcd=gcd(numerator,denominator);
int reduced_numerator=numerator/gcd;
int reduced_denominator=denominator/gcd;
String reduced_Fraction=reduced_numerator+"/"+reduced_denominator;
return reduced_Fraction;
}
private static int gcd(int numerator,int denominator)          //Method to get gcd of numerator and denominator;
{
int dividend=numerator;
int divisor=denominator;
int rem;
int gcd;
while(true)
{
rem=dividend%divisor;
if(rem==0)
{
gcd=divisor;
break;
}
else
{
dividend=divisor;
divisor=rem;
}
}
return gcd;
}
}

class Main
{
public static void main(String [] args)           //main method
{
Fraction obj=new Fraction();
obj.setNumerator(6);
obj.setDenominator(9);
System.out.println("\nNumerator is : "+obj.getNumerator());
System.out.println("Denominator is : "+obj.getDenominator());
System.out.println("Fraction is:"+obj.getNumerator()+"/"+obj.getDenominator());
System.out.println(obj.getNumerator()+" / "+obj.getDenominator()+" = " + obj.result());
System.out.println("Reduced Fraction is:"+obj.reducedFraction());
}
}


Output:


Related Solutions

Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
Objectives: use Scite 1. Use recursion to solve a problem 2. Create classes to model objects...
Objectives: use Scite 1. Use recursion to solve a problem 2. Create classes to model objects Problem 1: Compute greatest common divisor using recursion (filename: TestRecusiveGCD.java) The gcd(m, n) can be defined recursively as follows: If m % n is 0, gcd (m, n) is n. Otherwise, gcd(m, n) is gcd(n, m % n). Write a recursive method to find the GCD of two given integers. Write a test program that prompts the user to enter two integers, calls the...
11.1 Simple Arithmetic Program Using the instructions from Week 1 Lab, create a new folder named...
11.1 Simple Arithmetic Program Using the instructions from Week 1 Lab, create a new folder named Project01. In this folder create a new class named Project01. This class must be in the default package. Make sure that in the comments at the top of the Java program you put your name and today's date using the format for Java comments given in the Week 1 Lab. For this lab, you will write a Java program to prompt the user to...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem :...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem : The Rectangle class (Filename: TestRectangle.java) Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height....
I. At the beginning, create a folder named "test" and add the folder into the version...
I. At the beginning, create a folder named "test" and add the folder into the version control tool tracking system. Then create one file named "001" under this folder. Commit any change if necessary. II. Next, add another file named "002" in the same folder and commit the change ("adding") in the version control tool. III. Followed by adding that file, create a branch (namely, "branch-A") in the version control tool and make some changes to the contents in file...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
Instructions Create an application to accept data for an array of five CertOfDeposit objects, and then...
Instructions Create an application to accept data for an array of five CertOfDeposit objects, and then display the data. import java.time.*; public class CertOfDeposit { private String certNum; private String lastName; private double balance; private LocalDate issueDate; private LocalDate maturityDate; public CertOfDeposit(String num, String name, double bal, LocalDate issue) { } public void setCertNum(String n) { } public void setName(String name) { } public void setBalance(double bal) { } public void issueDate(LocalDate date) { } public String getCertNum() { }...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT