Assignment # 5 –   

 

Design and implement a class called MonetaryCoin that is derived from the Coin class below.  MonetaryCoin should inherit its parent’s ability to be “flipped.”

 

MonetaryCoin should

 

·          extend Coin

·          store an integer value when constructed (either 1, 5, 10, 25, 50, 100, 500 or 1000). 

·          have a getValue() method that returns its integer value. 

·          implement the Comparable interface

·          override the toString()method to return its value.

 

MonetaryCoin value of:

toString( ) should return:

1

penny

5

nickel

10

dime

25

quarter

50

half dollar

100

dollar

500

five dollars

1000

ten dollars

 

 

            Use the Main class to drive MonetaryClass.

 

            import java.util.*;

public class Main {

    public static void main(String [] args) {

        Random gen = new Random();

        int [ ] value = { 1, 5, 10, 25, 50, 100, 500, 1000};

        int x, i;

        MonetaryCoin [] coins = new MonetaryCoin[10];

        for (i = 0; i < coins.length; i++) {

            x = Math.abs(gen.nextInt()) % value.length;

            coins [i] = new MonetaryCoin(value[x]);

            }

        System.out.println("Before the coins are sorted:");

        for (i = 0; i < coins.length; i++)

            System.out.println("\t" + coins [i]);

        System.out.println("\nAfter the coins are sorted:");

        Arrays.sort(coins);

        for (i = 0; i < coins.length; i++)

           System.out.println("\t" + coins [i]);

        System.out.println("\n" + coins[0]

                    + " before flipping is "

                    + coins[0].getFace()

                    + " after flipping is "

                    + coins[0].flip());

    }

}

 

            The Coin class.

 

import java.util.*;

public class Coin {

   private Random gen = new Random();

   private final int HEADS = 0;

   private int face;

  

   public Coin () {

      flip();

   }

   public String flip () {

      face = (int) (Math.abs(gen.nextInt()) % 2);

      return getFace();

   }

   public String getFace(){

     String faceName;

      if (face == HEADS)

         faceName = "Heads";

      else

         faceName = "Tails";

      return faceName;

    }    

   public String toString() {

     return getFace();

   }

}

 

 

////////////////////////////////////////////////////////////////////////

 

Do not make any modifications to the Coin class or to the Main class.

Create a MonetaryCoin class

Test your program for correctness and accuracy, prior to submitting it for grading.

Compare your program to the Program Deduction Check List to avoid penalty points.

 

If the program works correctly, the output will appear similar to the following:

 

Before the coins are sorted:

 

        nickel

        dollar

        dime

        penny

        penny

        dollar

        five dollars

        dime

        ten dollars

        ten dollars

 

After the coins are sorted:

        penny

        penny

        nickel

        dime

        dime

        dollar

        dollar

        five dollars

        ten dollars

        ten dollars

 

penny before flipping is Tails after flipping is Tails

 

 

 

Send only the MonetaryCoin.java file for grading

 

All assignments MUST be received as email prior to the class session due date.  

·           Email sent to jpetlick@condor.depaul.edu

 

 

NOTE: When sending this e-mail assignment be sure to use the following header format or else the email assignment will NOT be graded:

 

To: jpetlick@condor.depaul.edu

cc: 
Subj: 212  HW5 Your_name(s)