Free Response Practice Question (with solution)- Mixed Number Project


<-- Back to Dice Game Project Next to Zodiac Sign Project -->




Mixed Number Project


Part 1: MixedNumber.java

Write the complete class definition for the MixedNumber class. A MixedNumber can be represented as consisting of the following:

  • wholeNum: represents the whole part of the mixed number.
  • num: represents the numerator of the fraction-part of the mixed number.
  • deno: represents the denominator of the fraction-part of the mixed number.

Additionally,

  • The MixedNumber class contains a default constructor, that initializes a generic mixed number with all value set to 0 (zero).
  • The class also consists of a parameterised constructor MixedNumber(int wholePart, int num, int deno), to initialize all the attributes of the mixed number.
  • Accessor method to access all the properties of the MixedNumber class.
  • A method (addMixedNumber(MixedNumber m2)) that takes as input another mixed number and adds the received number with the current number, thereby modifying the value of the current number.
  • A method (subMixedNumber(MixedNumber m2)) that takes as input another mixed number and subtracts the received number from the current number, thereby modifying the value of the current number.

Part 2: MixedNumberRunner.java

Write the complete class definition for the runner class named MixedNumberRunner class, that makes use of the MixedNumber class. The main method of the MixedNumberRunner class should do the following:

  • Use the generic constructor to initialize a mixed number object (genMN).
  • Use the parameterised constructors to initialize two mixed number objects (mixedNum1 and mixedNum2).
  • Use the method (addMixedNumber(MixedNumber m2)) to add the value of the two numbers (mixedNum1 + mixedNum2) and print the modified value of mixedNum1.

Note:

For adding two mixed numbers, you must do all the following steps:

Step 1: Add the whole part of the two numbers

Step 2: Add the fraction part of the numbers separately

Use the formula below to add fractions:

Hence,

Step 3: Reduce the fraction to the simplest form

Step 4: If the fraction part is in improper form, convert it to a mixed number and add this mixed number with the result obtained so far


Solution:


Part 1: MixedNumber.java


public class MixedNumber {

    int wholePart;
    int num;
    int deno;
    
    public MixedNumber()
    {
        wholePart=0;
        num=0;
        deno=0;
    }
    
    public MixedNumber(int wholePart, int num, int deno)
    {
        this.wholePart= wholePart;
        this.num = num;
        this.deno = deno;
    }
    
    public int getWholePart()
    {
        return wholePart;
    }
    
    public int getNumPart()
    {
        return num;
    }
    
    public int getDenoPart()
    {
        return deno;
    }
    
    public void addMixedNumber(MixedNumber m2)
    {
        this.wholePart = this.wholePart + m2.wholePart;
        
        //add fraction part
        int newNum = (num * m2.deno + m2.num * deno);
        int newDen = (deno * m2.deno);
        num = newNum;
        deno = newDen;
        
        //reduce the fraction
        int gcd = gcd(num, deno);
        num = num/gcd;
        deno = deno/gcd;
        
        this.wholePart= (int)(num/deno) + this.wholePart;
        this.num = (int)(num%deno);
    }
    
    public String toString(){
        if (num !=0)
            return (wholePart + " " + num + "/" + deno);
        else
            return (wholePart+"");
    }
    
    private int gcd(int n1, int n2)
    {
        int min;
        if (n1 < n2)
            min = n1;
        else
            min = n2;
        for (int i = min; i > 1; i--) {
            if (n1 % i == 0 && n2 % i == 0)
                return i;
        }
        return 1;
    }
}



Part 2:MixedNumberRunner.java


public class MixedNumberRunner {

    public static void main(String args[])
    {
        MixedNumber genMN = new MixedNumber();
        System.out.println("Generic number is: "+ genMN);
        
        MixedNumber mixedNum1 = new MixedNumber(6, 2, 7);
        MixedNumber mixedNum2 = new MixedNumber(3, 3, 7);
        System.out.println("First number is: "+mixedNum1);
        System.out.println("Second number is: "+mixedNum2);
        
        mixedNum1.addMixedNumber(mixedNum2);
        System.out.println("After adding first number with second number: "+mixedNum1);
    }
}




Java project files (with Runner code):


<-- Back to Dice Game Project Next to Zodiac Sign Project -->