Question

Step 1: Declare a base class called Coin that contains a Boolean data element called face,...

Step 1: Declare a base class called Coin that contains a Boolean data element called face, and a random generator to fill the face element with a Boolean value (true or false). True = heads, false = tails. The class needs a default constructor, a method to flip the coin, a method to return the face element, and a toString to display “Heads” if the face is true and “Tails” if not.

Step 2: Declare a subclass of Coin that describes what kind of coin it is and what value it represents. It needs a constructor that receives its value, a method that returns its value, and a toString method that returns a string with the face description (“Heads” or “Tails”) and its monetary value.

Step 3: Design and code a driver program that creates an array of 7 coins, assigns each a random coin value (use the following values: 1,5,10,25,50,100), flips them all, then sums the values of only the coins that are “Heads”, and finally displays all the coins and the sum.

Step 4: Using the JAVADOC format, document the program and every method (main is a method, not a constructor) as displayed in the sample programs found in the downloaded files for classroom activity. Completeness, accuracy, and spelling count.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

/**
* Coin class
*/
class Coin{
//face variable
private boolean face;
//Constructor that takes face
Coin(boolean face){
this.face = face;
}
//Populate face with random value, heads/tails
Coin(){
Random r = new Random();
face = r.nextInt(2)==1;
}

/**
* Returns face of the coin
* @return
*/
public boolean getFace(){
return this.face;
}

/**
* Flips the coin
*/
public void flip(){
this.face = !this.face;
}

/**
* Returns Heads or Tails of the coin
* @return
*/
public String toString(){
if(face){
return "Heads";
}
else{
return "Tails";
}
}
}

/**
* This class extends Coin class
*/
class MyCoin extends Coin{
//Value of the coin
private int value;
  
//Constructor that takes value of the coin
MyCoin(int value){
this.value =value;
}

/**
* Returns the value of the coin
* @return
*/
public int getValue(){
return value;
}

public String toString(){
return value+" "+super.toString();
}
}

class Main{
public static void main(String[] args) {
//Initiailze random variable
Random r = new Random();
//Initialize array of 7 MyCoins
MyCoin coins[] = new MyCoin[7];
//Populate them with random value
for(int i=0;i<7;i++){
coins[i] = new MyCoin(r.nextInt(10)+1);
}
//Flipping all the coins
for(int i=0;i<7;i++){
coins[i].flip();
}
int ans = 0;
System.out.println("Coins are : ");
//Print the coins and find total value of all Head coins
for(int i=0;i<7;i++){
System.out.println(coins[i]);
if(coins[i].getFace()){
ans+=coins[i].getValue();
}
}
System.out.println(ans);
}
}
OUTPUT :

THANK YOU!!!!

Add a comment
Know the answer?
Add Answer to:
Step 1: Declare a base class called Coin that contains a Boolean data element called face,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Comment your code. At the top of the program include your name, a brief description of...

    Comment your code. At the top of the program include your name, a brief description of the program and what it does and the due date. The program must be written in Java and submitted via D2L. The code matches the class diagram given above. The code uses Inheritance. In the following, you are given code for two classes: Coin and TestCoin. You study these classes first and try to understand the meaning of every line. You can cut and...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • implement coinclass and driver program within one source file, i.e. do not separate class specifications with...

    implement coinclass and driver program within one source file, i.e. do not separate class specifications with implementation Its a c++ problem 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following member variable: • A string named sideUp. The sideUp member variable will hold either "heads" or "tails" indicating the side of the coin that is facing up. The Coin class should have the following member functions: • A default constructor that randomly determines...

  • # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss...

    # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...

  • Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

    Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method...

  • c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

    c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still...

    ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still an Account class, however it has an additional field and some additional functionality. But it will still retain all the behavior of the Account class. Write a class called GoldAccount. This class will have an additional private field called bonusPoints. This field will require no get or set methods. But it will need to be initialized to 100 in the constructor. Thereafter, after a...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT