This exercise uses the MonetaryCoin class from PP9.1, and extends the concepts i
ID: 665494 • Letter: T
Question
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.
The classes required for the project are below:
public class MonetaryCoin extends Coin {
protected int num;
public MonetaryCoin(int m)
{
num=m;
}
public int getValue()
{
return num;
}
public int sum(MonetaryCoin[]coins){
int total=num;
if (coins.length==0)
return total;
for (int i=0;i {
total+=coins[i].getValue();
}
return total;
}
}
import java.util.*;
public class TestCoin {
public static void main(String[] args) {
int i, j, x;
Random r=new Random();
int [] numbers={4,7,2,9,6,1,3,5,8,10};
MonetaryCoin[] coins=new MonetaryCoin[10];
for(i=0;i {
x=Math.abs(r.nextInt())%numbers.length;
coins[i]=new MonetaryCoin(numbers[x]);
}
System.out.println("Before the coins are sorted:");
for (i=0;i System.out.println(" "+coins[i]);
System.out.println(" After the coins are sorted:");
for (j=0;j System.out.println(" "+coins[j]);
System.out.println(" " +coins[0]+"before flipping is:"+coins[0].getValue());
System.out.println("After flipping is:"+coins[0].flip());
}
}
import java.util.*;
public class EnhancedBookShelf {
private ArrayList Date = new ArrayList();
private Book earliest;
public EnhancedBookShelf()
{
earliest = null;
}
public Book getEarliest()
{
return earliest;
}
public ArrayList findBooksWithCopyright(int copyright)
{
return Date;
}
public void addBook(Book bookToAdd)
{
if (earliest == null)
{
earliest = bookToAdd;
}
else
{
if (earliest.getCopdate() > bookToAdd.getCopdate())
{
earliest = bookToAdd;
}
}
}
}
public class Coin {
public final int HEADS = 0;
public final int TAILS = 1;
private int face;
public Coin() {
flip();
}
public void flip() {
face = (int) (Math.random() * 2);
}
public boolean isHeads() {
return (face == HEADS);
}
public String toString() {
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
return faceName;
}
}
Explanation / Answer
public class MonetaryCoin extends Coin {
protected int num;
public MonetaryCoin(int m)
{
num=m;
}
public int getValue()
{
return num;
}
public int sum(MonetaryCoin[]coins){
int total=num;
if (coins.length==0)
return total;
for (int i=0;i {
total+=coins[i].getValue();
}
return total;
}
}
import java.util.*;
public class TestCoin {
public static void main(String[] args) {
int i, j, x;
Random r=new Random();
int [] numbers={4,7,2,9,6,1,3,5,8,10};
MonetaryCoin[] coins=new MonetaryCoin[10];
for(i=0;i {
x=Math.abs(r.nextInt())%numbers.length;
coins[i]=new MonetaryCoin(numbers[x]);
}
System.out.println("Before the coins are sorted:");
for (i=0;i System.out.println(" "+coins[i]);
System.out.println(" After the coins are sorted:");
for (j=0;j System.out.println(" "+coins[j]);
System.out.println(" " +coins[0]+"before flipping is:"+coins[0].getValue());
System.out.println("After flipping is:"+coins[0].flip());
}
}
import java.util.*;
public class EnhancedBookShelf {
private ArrayList Date = new ArrayList();
private Book earliest;
public EnhancedBookShelf()
{
earliest = null;
}
public Book getEarliest()
{
return earliest;
}
public ArrayList findBooksWithCopyright(int copyright)
{
return Date;
}
public void addBook(Book bookToAdd)
{
if (earliest == null)
{
earliest = bookToAdd;
}
else
{
if (earliest.getCopdate() > bookToAdd.getCopdate())
{
earliest = bookToAdd;
}
}
}
}
public class Coin {
public final int HEADS = 0;
public final int TAILS = 1;
private int face;
public Coin() {
flip();
}
public void flip() {
face = (int) (Math.random() * 2);
}
public boolean isHeads() {
return (face == HEADS);
}
public String toString() {
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
return faceName;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.