subject

For this assignment, you will create a class to store fractions. The class will hold two integer values: a numerator and a denominator. For your class we will stick to positive fractions, meaning the numerator and the denominator must both be greater than 0.

In previous assignments, we had a requirement that your class be named Main. In this assignment, the class is required to be named Fraction.

To get started, download the template file Fraction. java (Links to an external site.). Your job will be to add the constructors and methods described in the following sections to the class Fraction that is declared in this template file.

Fraction should include two constructors:

Fraction(): default constructor which creates a fraction 1/1
Fraction(int n, int d): If n is positive, set numerator to n. Otherwise, set numerator to 1. If d is positive, set denominator to d. Otherwise, set denominator to 1.
Fraction should include the following methods:

String toString(): Returns the fraction as a string in the format "numerator/denominator". For example 1/2 or 5/3.
String mixedNumber(): Returns any improper (top-heavy) fraction as a mixed number, for example, 2 3/5. If the numerator of the fraction part is 0, return only the integer part of the mixed number. If the fraction is proper, return only the fraction part.
void add(int n, int d): If n and d are both positive, add the fraction n/d to this fraction. Otherwise, leave the fractions unchanged. In general the sum of the fractions a/b and c/d is(a*d + c*b)/(b*d).
To test your code, download the runner class student_fraction_runner. java (Links to an external site.) into the same folder that holds your Fraction. java. Execute the method student_fraction_runner. main and verify that the output matches the sample run listed below.

We will use a similar but different runner to grade the program. In order to pass all tests, you will need to change student_fraction_runner. java to test different values to make sure your program fits the requirements. Note: You will not be submitting student_fraction_runner. java. This file is provided to help you test your implementation of the class Fraction.

When you are done coding and testing, copy and paste your entire Fraction class into the Code Runner and press "Submit Answers" in order for your assignment to count as turned in. We will be returning to and improving the Fraction class in a later assignment (you may have already spotted some potential flaws in how the class is implemented) so be sure to save your work.

Sample Run

Fraction 1: 7/4
As a mixed number: 1 3/4

Fraction 2: 1/1
As a mixed number: 1

Fraction 3: 2/7
As a mixed number: 2/7
Add 2/5: 24/35

Fraction 4: 24/6
As a mixed number: 4
Attempt to add 1/-2: 24/6

Fraction 5: 5/2
As a mixed number: 2 1/2
Add 2/3: 19/6
As a mixed number: 3 1/6

Here is the template:

/* Term 2 Assignment 1 - Fraction */
/* A class which is used to represent fractions*/
public class Fraction
{
private int numerator;
private int denominator;

// TODO write default constructor
public Fraction()
{
}

// TODO write constructor for fraction n/d where n, d > 0
public Fraction(int n, int d)
{
}

// TODO write method to return fraction as a String
public String toString()
{
}

// TODO write method to return fraction as a mixed number String
public String mixedNumber()
{
}

// TODO write method to add fraction n/d to this Fraction
public void add(int n, int d)
{
}
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 22:00
What do the principles of notice, choice, onward transfer, and access closely apply to? a. privacyb. identificationc. retentiond. classification
Answers: 1
question
Computers and Technology, 21.06.2019 22:00
Match the steps of the process to julia's analysis. 1. analyze choices. current costs for making phone calls to foreign countries averages between five and ten cents a minute. 2. determine the goals. julia needs to reduce the cost of telecommunications without reducing her employees' ability to do their jobs. 3. gather data. the corporate computer network will be able to handle the increased traffic that will occur as a result of voip. 4. evaluate the decision. julia will have her it department set up voip in the smallest domestic office. 5. make the decision. julia will have employees document the benefits and problems that result from using the new technology.
Answers: 1
question
Computers and Technology, 22.06.2019 07:00
For all machines-not just hammers- the user applies force force to the machine to the machine over a certain distance. a. input b. output c. duo d. none of the above
Answers: 1
question
Computers and Technology, 23.06.2019 04:00
Laire writes a letter to her grandmother, in which she describes an amusement park she visited last week. she adds pictures of that place in her letter. which feature of a word processing program will claire to remove unwanted parts of the pictures?
Answers: 3
You know the right answer?
For this assignment, you will create a class to store fractions. The class will hold two integer val...
Questions
question
Health, 30.12.2019 21:31
Questions on the website: 13722360