Intro Frequently in the design of classes a class will conta
Intro
Frequently in the design of classes, a class will contain as one of its data members an instance of another class. By doing so the containing class may invoke methods from the other class to do part of its work. In this lab you will create a class MixedNumber to represent numbers which have both a whole number and a fractional part. Ex. 3 2/3, 7 2/5, 8 0/1. In our class design we must allow for situations where a fraction may not be present and where the whole number is zero. Below is the UML design diagram for this class
Mixed Number
-whole : int = 1
-fractionPart: Fraction = 0/1
MixedNumber(): default constructor
MixedNumber(in whole:int, in fract: Fraction)
MixedNumber( in fract: Fraction)
setWhole(in num: int): void
getWhole(): int
setFraction (in newFract: Fraction):void
getFraction(): Fraction
toString(): String
add ( in B: MixedNumber): MixedNumber
add ( in B: Fraction): MixedNumber
subtract (in B: MixedNumber): MixedNumber
subtract (in B: Fraction): MixedNumber
multiply( in B: MixedNumber): MixedNumber
multilpy( in B: Fraction): MixedNumber
divide ( in B: MixedNumber): MixedNumber
divide ( in B: Fraction): MixedNumber
convertToImproperFraction(): Fraction
Requirements
Fulfill the requirements as given by the instructions.
Test your MixedNumber class using (ideally) a JUnit Test class or at least a separate main() program.
Solution
Hi,
I have given the class structure here with add method implementation... other methods can be implemented on similar lines like add method. Please try for yourself.
CODE:
// create class MixedNumber
public class MixedNumber {
// Inner class to contain the fraction
class Fraction{
private int numerator, denominator;
// constructor for Fraction.
public Fraction(int numer, int denom) {
super();
if (denom == 0)
denom = 1;
// Make the numerator \"store\" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
// getters for numerator and denominator.
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
// add the current number with the given number.
public Fraction add (Fraction op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new Fraction (sum, commonDenominator);
}
// subtract the given number from current number
public Fraction subtract (Fraction op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new Fraction (difference, commonDenominator);
}
// Multiply the given number with the current number.
public Fraction multiply (Fraction op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new Fraction (numer, denom);
}
// divide the given number with the current number.
public Fraction divide (Fraction op2)
{
return multiply (op2.reciprocal());
}
// get the reciprocal of the fraction.
public Fraction reciprocal ()
{
return new Fraction (denominator, numerator);
}
// reduces the fraction to the simplest form
private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
// computes the gcd required for reduction.
private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
}
// whole number and fraction part declared here
int whole;
Fraction fractionPart;
// default constructor
public MixedNumber(){
whole = 0;
fractionPart = new Fraction(0, 1);
}
// parameterised constructor
public MixedNumber(int whole, int num,int den) {
this.whole = whole;
this.fractionPart = new Fraction(num, den);
}
public MixedNumber(int newWhole, Fraction newFrac) {
this.whole = newWhole;
this.fractionPart = newFrac;
}
// get the whole number
public int getWhole() {
return whole;
}
// set the whole number
public void setWhole(int whole) {
this.whole = whole;
}
// get the fraction part
public Fraction getFractionPart() {
return fractionPart;
}
// set the fraction part.
public void setFractionPart(Fraction fractionPart) {
this.fractionPart = fractionPart;
}
// tostring method to display in the required format.
@Override
public String toString() {
return \"MixedNumber [whole=\" + whole + \", fraction=\" + fractionPart.numerator + \"/\" + fractionPart.denominator;
}
// add methods
// This method will add the current whole number and fraction with the
// function parameter.
public MixedNumber add(MixedNumber nmbr)
{
int newWhole = whole + nmbr.whole;
Fraction newFrac = fractionPart.add(nmbr.fractionPart);
MixedNumber temp = new MixedNumber(newWhole,newFrac);
return temp;
}
public MixedNumber add(Fraction nmbr)
{
return null;
}
// subtract methods
public MixedNumber subtract(MixedNumber nmbr)
{
return null;
}
public MixedNumber subtract(Fraction nmbr)
{
return null;
}
// multiply methods
public MixedNumber multiply(MixedNumber nmbr)
{
return null;
}
public MixedNumber multiply(Fraction nmbr)
{
return null;
}
// divide methods
public MixedNumber divide(MixedNumber nmbr)
{
return null;
}
public MixedNumber divide(Fraction nmbr)
{
return null;
}
// convert to improper fraction
Fraction convertToImproperFraction(){
return fractionPart;
}
public static void main(String[] args){
MixedNumber firstNumber = new MixedNumber(1,4,3);
MixedNumber secondNumber = new MixedNumber(2,5,3);
MixedNumber res = firstNumber.add(secondNumber);
System.out.println(res.toString());
}
}
OUTPUT:
MixedNumber [whole=3, fraction=3/1




