Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA!!! This exercise uses the MonetaryCoin class from PP9.1, and extends the co

ID: 3684928 • Letter: J

Question

JAVA!!!

This exercise uses the MonetaryCoin class from PP9.1, and extends the concepts introduced in the EnhanceBookshelf or StringSet classes from the Lesson on ArrayList.

Write a class DataSet

DataSet should have a single instance variable, an ArrayList that holds MonetaryCoin objects.

DataSet should have a single default constructor.

DataSet should have the following methods:

// mutator to add a given MonetaryCoin to the ArrayList

public void add(MonetaryCoin mc)

// accessor to return the total value in cents of all coins in the DataSet that are Heads and have the value cents

public int valueHeads(int cents)

// accessor to return the total value in cents of all coins in the DataSet that are Tails and have the value cents

public int valueTails(int cents)

/* as an example of valueHeads and valueTails, assume the following describes 6 MonetaryCoin objects added to the DataSet:
* quarter (25 cents) heads
* quarter (25 cents) heads
* quarter (25 cents) heads
* dime (10 cents) heads
* dime (10 cents) heads
* quarter (25 cents) tails
*
*
* valueHeads(25) returns 75 - 3 coins each with a value of 25 cents are heads
*
*
* valueTails(25) returns 25 - 1 coin with a value of 25 cents is heads
*
*
* valueHeads(10) returns 20 - 2 coins each with a value of 10 are heads
*
*
* valueTails(10) returns 0 - there are no coins with a value of 10 that are tails
*/

Modify the main driver you produced in PP9.1 to flip and insert (only once!) each coin into the DataSet. When all coins have been inserted, display the total values for several combinations of heads/tails and values for the DataSet.

MonetaryCoin class from pp 9.1: http://www.chegg.com/homework-help/questions-and-answers/design-implement-class-called-monetarycoin-derived-coin-class-presented-chapter-5-store-in-q3959157?trackid=6df48d23&strackid=6b3ac4f5&ii=3

public class MonetaryCoin extends Coin

{

int value;

public MonetaryCoin(int v) { super(); value = v; }

public int getValue()

{

return value;

}

public static void main(String args[])

{

MonetaryCoin m[] = new MonetaryCoin[10];

for(int i=0;i<10;i++)

{ m[i] = new MonetaryCoin(i);

System.out.println("MonetaryCoin of value "+i+" added!");

}

int sum = 0;

for(int i=0;i<10;i++)

{

System.out.println("");

sum += m[i].getValue();

System.out.println("Face of MonetaryCoin["+i+"] is "+m[i].toString());

m[i].flip();

System.out.println("Face of MonetaryCoin["+i+"] after flipping is "+m[i].toString());

}

System.out.println(" Sum of Monetary Coins' value is "+sum);

}

}

Explanation / Answer

Sample code logic for MonetaryCoin class-

public class MonetaryCoin extends Coin

{ private int value; // Cash value of coin

    public MonetaryCoin (int val)

      { value = val;

      } // constructor

    public int getValue ()

    // Return cash value of coin

      { return value;

      } // getValue    

    public String toString ()

      { if (value < 100)

          return value + "c";

        else

          return "R" + (value/100);

      } // toString

} // class MonetaryCoin

Sample code logic for 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();

   }

}

Sample code logic for 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(" " + coins [i]);

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

        Arrays.sort(coins);

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

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

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

                    + " before flipping is "

                    + coins[0].getFace()

                    + " after flipping is "

                    + coins[0].flip());

    }

}

note-the above code samples can help to answer the given question.