Question

Assignment 1, Fraction Instructions For this assignment, you will create a class to store fractions. The...

Assignment 1, Fraction

Instructions

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

Milestones

As you work on this assignment, you can use the milestones below to inform your development process:

Milestone 1: Write the two class constructors Fraction() and Fraction(int n, int d). Test these compile by adding dummy return statements to the String methods.

Milestone 2: Write the two accessor methods toString() and mixedNumber().

Milestone 3: Write the mutator method add(int n, int d). Compile and run the student_fraction_runner class and check the results are as expected, fixing any errors identified.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Short Summary:

  • Implemented Fraction class
    • Private members: numerator and denominator
    • Default and parameterized constructors
    • Member functions: toString(), mixedNumber() and add(int n, int d).
  • Implemented a sample test runner class to test all the functions

Source Code:

Fraction.java

/* Fraction Java Class */
public class Fraction
{
//private variables
private int numerator;
private int denominator;

//Default constructor
//Assigns 1 to both numerator and denominator
public Fraction()
{
numerator = 1;
denominator = 1;
}

//Parameterized constructor
Fraction(int n, int d)
{
/*numerator and the denominator must both be greater than 0*/

//If n is positive, set numerator to n.
//Otherwise, set numerator to 1
if (n > 0)
{
numerator = n;
}
else
{
numerator = 1;
}

//If d is positive, set denominator to d.
//Otherwise, set denominator to 1.
if (d > 0)
{
denominator = d;
}
else
{
denominator = 1;
}   
}

//Returns the fraction as a string
public String toString()
{
//in the format “numerator/denominator”. For example 1/2 or 5/3.
String temp = numerator + "/" + denominator;
return temp;
}

//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).
public void add(int n, int d)
{
// check input parameters are positive
if ((n > 0) && (d > 0))
{
numerator = (numerator * d) + (denominator * n);
denominator = denominator * d;
}
}

//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.
public String mixedNumber()
{
//Get the quotient using operator '/'
int quotient = numerator / denominator;
//Get the remainder using operator '%'
int remainder = numerator % denominator;

//If remainder is zero, return the whole number
if (remainder == 0)
{
return String.valueOf(quotient);
}
else
{
//format the output - for example, 2 3/5.
if (quotient == 0)
{
return String.valueOf(remainder) + "/" + String.valueOf(denominator);
}
else
{
return String.valueOf(quotient) + " " + String.valueOf(remainder) +
"/" + String.valueOf(denominator);
}
}
}
}

Refer the following screenshot for code indentation:

Sample test class:

public class Main
{
//student_fraction_runner sample
   public static void main(String[] args)
   {
   //test parameterized constructor
   Fraction MyFraction1 = new Fraction(7, 4);
   System.out.println("Fraction 1: " + MyFraction1.toString());
   System.out.println("As a mixed number: " + MyFraction1.mixedNumber());
  
   System.out.println();
  
   //test default constructor
   Fraction MyFraction2 = new Fraction();
   System.out.println("Fraction 2: " + MyFraction2.toString());
   System.out.println("As a mixed number: " + MyFraction2.mixedNumber());
  
   System.out.println();
  
   //test add and mixed number
   Fraction MyFraction3 = new Fraction(2, 7);
   System.out.println("Fraction 3: " + MyFraction3.toString());
   System.out.println("As a mixed number: " + MyFraction3.mixedNumber());
   MyFraction3.add(2, 5);
   System.out.println("Add 2/5: " + MyFraction3.toString());
  
   System.out.println();
  
   //test adding negative integers
   //the numerator and denominator should remain unchanged
   Fraction MyFraction4 = new Fraction(24, 6);
   System.out.println("Fraction 4: " + MyFraction4.toString());
   System.out.println("As a mixed number: " + MyFraction4.mixedNumber());
   MyFraction4.add(1, -2);
   System.out.println("Add 1/-2: " + MyFraction4.toString());
  
   System.out.println();
  
   //test adding positive integers
   //the sum of the fractions a/b and c/d is(a*d + c*b)/(b*d)
   Fraction MyFraction5 = new Fraction(5, 2);
   System.out.println("Fraction 5: " + MyFraction5.toString());
   System.out.println("As a mixed number: " + MyFraction5.mixedNumber());
   MyFraction5.add(2, 3);
   System.out.println("Add 2/3: " + MyFraction5.toString());
   System.out.println("As a mixed number: " + MyFraction5.mixedNumber());
   }
}

Sample Output:

Feel free to rate the answer. Let me know if you have any questions through comment section. I am happy to answer.

Happy Studying!!

Add a comment
Know the answer?
Add Answer to:
Assignment 1, Fraction Instructions For this assignment, you will create a class to store fractions. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a...

    Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the...

    Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the following API: Constructor: Fraction(int numerator, int denominator) . String toString() so instances of the class can be printed. The format used above is sufficient. Fraction simplify() This method returns a new Fraction that is arithmetically equal to that given, but for which the numerator and denominator have no common factors. Create a testing program, TestFraction, that prompts the user to provide the numerator and...

  • In C# programming. Create a fractions class that represents fractions in the form a/b Your class...

    In C# programming. Create a fractions class that represents fractions in the form a/b Your class should implement the following members: int Numerator int Denominator Fraction(int numerator, int denominator) ­ creates a new Fraction double ToDecimal() ­ returns the fraction as a double Fraction Add(Fraction f) ­ adds the fraction to the one passed in and simplifies the result Fraction Multiply(Fraction f) ­ multiplies the fraction by the one passed in and simplifies the result Fraction Simplify() ­ simplifies the...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

  • This in in C# There are two classes, class Fraction and class FractionDemo The user needs...

    This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT