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, 22.06.2019 09:30
What are the steps involved in accepting all the changes in a document? arrange these in order click edit. click accept or reject. click changes. click accept all.
Answers: 1
question
Computers and Technology, 22.06.2019 13:00
Which part of the cpu accepts data?
Answers: 1
question
Computers and Technology, 22.06.2019 20:00
What statement best describes operating systems? it’s possible for modern computers to function without operating systems. most operating systems are free or very inexpensive. operating systems are managed by the computer’s microprocessor (cpu). operating systems manage the computer’s random access memory (ram).
Answers: 1
question
Computers and Technology, 22.06.2019 22:40
Write a program that defines symbolic names for several string literals (chars between quotes). * use each symbolic name in a variable definition. * use of symbolic to compose the assembly code instruction set can perform vara = (vara - varb) + (varc - vard); ensure that variable is in unsigned integer data type. * you should also further enhance your symbolic logic block to to perform expression by introducing addition substitution rule. vara = (vara+varb) - (varc+vard). required: debug the disassembly code and note down the address and memory information.
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
Mathematics, 07.12.2020 20:00
question
English, 07.12.2020 20:00
question
History, 07.12.2020 20:00
question
Mathematics, 07.12.2020 20:00
question
Mathematics, 07.12.2020 20:00
question
Geography, 07.12.2020 20:00
Questions on the website: 13722367