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...
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder....
Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder. For this assignment you will be creating a total of 2 classes that meet the definitions below. This assignment will allow the user to enter in the coefficients for a polynomial function of degree 2. The user will then be asked to enter a number. The program will use that number as an argument to the function, and will print the corresponding function value....
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT