Question
You will be developing a class representing a fraction. Fields Your fields are the properties of the object. It is what will be stored in
You will be developing a class representing a fraction.
Fields
Your fields are the properties of the object. It is what will be stored in memory to define every distinguishable instance (object) of the class. Since we are representing a number, the object needs to be immutable. Numbers and letters cannot be changed
/**fields**/ final long numerator; final long denominator;
Constructor
You need to provide the class with multiple constructors. Take a moment and think about what are the possible inputs you can provide in order to build a fraction. The constructor's job is to give your fields values upon the creation of an object. Write six constructors with the following method signatures:
/**Constructors**/ Fraction() Fraction (long numerator) Fraction (short numerator) Fraction (int numerator) Fraction(long numerator, long denominator) Fraction(short numerator, short denominator) Fraction(int numerator, int denominator)
Add
Again, take some time and think about what can be added to a fraction. Since we will be using integers to represent the values, we will want to avoid decimal representation. So, don't don't use doubles and floats. Write add methods with the following signatures:
/**Addition**/ Fraction add (long num) Fraction add (int num) Fraction add (short num) Fraction add (long numerator, long denominator) Fraction add (int numerator, int denominator) Fraction add (short numerator, short denominator) Fraction add (Fraction num)
Subtract
Do the same thing that you did for addition but for subtraction. Write methods with matching method signatures for subtracting operations.
/**Subtraction**/ Fraction subtract (long num) Fraction subtract (int num) Fraction subtract (short num) Fraction subtract (long numerator, long denominator) Fraction subtract (int numerator, int denominator) Fraction subtract (short numerator, short denominator) Fraction subtract (Fraction num)
Multiply
Write methods with matching method signatures for Multiplying operations.
/**Multiplication**/ Fraction multiply (long num) Fraction multiply (short num) Fraction multiply (int num) Fraction multiply (long numerator, long denominator) Fraction multiply (int numerator, int denominator) Fraction multiply (short numerator, short denominator) Fraction multiply (Fraction num)
Divide
Write methods with matching method signatures for Multiplying operations.
/**Division**/ Fraction divide (long num) Fraction divide (int num) Fraction divide (short num) Fraction divide (long numerator, long denominator) Fraction divide (int numerator, int denominator) Fraction divide (short numerator, short denominator) Fraction divide (Fraction num)
Overriding inherited methods
All objects inherit from a class called Object (Links to an external site.)Links to an external site.. Meaning that before you write any methods, you automatically get the methods defined for the Object class added to your class. Because we are writing a class with specific usage, we need to Override the methods inherited from the Object class and make them perform behaviour appropriate to what a fraction embodies:
The methods you need to override are the following:
/**Object defined methods**/ Object clone() String toString() boolean equals()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started