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:


Additionally,


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:


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 -->