Create a java program to demonstrate this moneyCoin class. In this program, inst
ID: 3734293 • Letter: C
Question
Create a java program to demonstrate this moneyCoin class. In this program, instantiate four objects of the moneyCoin class: one representing a quarter, one representing a dime, one representing a nickel, and one representing a penny. That is, the frontValue for them would be 25, 10, 5, and 1, respectively. After tossing the four coins, show the total by dollars. The total is calculated based on the front value of the coins landed heads-up.
The program will ask the user whether he/she would like to toss again until the user chooses not to.
The output should look like this:
This program tosses four coins and get the total of values for coins landing on heads.
Ready to toss? (Y/N) y
The penny lands on Tails
The nickel lands on Tails
The dime lands on Tails
The quarter lands on Tails
The totla of the coin-tossing result is $0.0
Would yoiu like to toss again? (Y?N) yes
The penny lands on Heads
The nickel lands on Tails
The dime lands on Heads
The quarter lands on Tails
The total of the coin-tossing result is $0.11
Would you like to toss again? (Y?N) y
The penny lands on Tails
The nickel lands on Heads
The dime lands on Heads
The quarter lands on Tails
The total of the coin-tossing result is $0.15
Would you like to toss again (Y?N) n
The coin class that is used is
public class moneyCoin {
//field
String sideUp;
// constructor
public moneyCoin () {
toss () ;
Random rand = new Random () ;
if (rand.nextInt (2) == 0)
sideUp = "Heads" ;
else
sideUp = "Tails" ;
}
public String getSideUp() {
return sideUp;
}
public void toss () {
Random rand = new Random () ;
if (rand.nextInt (2) == 0)
sideUp = "Heads" ;
else
sideUp = "Tails" ;
}
}
Explanation / Answer
Please find my code:
Coin.java
import java.util.Random;
public abstract class Coin {
//Declaring instance variables
private String name;
private String sideUp;
private double val;
Random r=null;
//Parameterized constructor
public Coin(String name) {
this.name = name;
}
public Coin(String name,double val) {
this.name = name;
this.val=val;
}
public String getName() {
return name;
}
public double getValue()
{
return val;
}
//This method will randomly generate Head or Tail
public void toss() {
r = new Random();
int random = r.nextInt(2);
if (random == 1)
this.sideUp = "Heads";
else if (random == 0)
this.sideUp = "Tails";
}
//This method displaying the face of the Coin
public String getSideUp() {
toss();
return sideUp;
}
}
____________________
Quarter.java
public class Quarter extends Coin {
//Parameterized constructor
public Quarter() {
super("Quarter",0.25);
}
}
____________________
Dime.java
public class Dime extends Coin {
//Parameterized constructor
public Dime() {
super("Dime",0.10);
}
}
_________________
Nickle.java
public class Nickle extends Coin {
//Parameterized constructor
public Nickle() {
super("Nickle",0.05);
}
}
_________________
Penny.java
public class Penny extends Coin {
//Parameterized constructor
public Penny() {
super("Penny",0.01);
}
}
_________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
double tot;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
Coin coins[] = { new Quarter(), new Dime(),
new Nickle(), new Penny() };
while (true) {
tot=0.0;
for (int i = 0; i < coins.length; i++) {
String face = coins[i].getSideUp();
System.out.println("The " + coins[i].getName() + " lands on "
+ face);
if (face.equals("Heads")) {
tot += coins[i].getValue();
}
}
System.out.printf("The total of the coin-tossing result is $%.2f ",tot);
// Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to toss again (Y/N) ::");
char ch = sc.next(".").charAt(0);
if (ch == 'Y' || ch == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}
_____________________
Output:
The Quarter lands on Heads
The Dime lands on Tails
The Nickle lands on Heads
The Penny lands on Heads
The total of the coin-tossing result is $0.31
Do you want to toss again (Y/N) ::y
The Quarter lands on Tails
The Dime lands on Heads
The Nickle lands on Heads
The Penny lands on Heads
The total of the coin-tossing result is $0.16
Do you want to toss again (Y/N) ::y
The Quarter lands on Heads
The Dime lands on Heads
The Nickle lands on Heads
The Penny lands on Heads
The total of the coin-tossing result is $0.41
Do you want to toss again (Y/N) ::y
The Quarter lands on Tails
The Dime lands on Heads
The Nickle lands on Heads
The Penny lands on Tails
The total of the coin-tossing result is $0.15
Do you want to toss again (Y/N) ::y
The Quarter lands on Tails
The Dime lands on Tails
The Nickle lands on Tails
The Penny lands on Heads
The total of the coin-tossing result is $0.01
Do you want to toss again (Y/N) ::n
:: Program Exit ::
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.