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 paste the code to Eclipse and run it.
The following Java codes are adapted from (John Lewis and William Loftus (2007). “Software Solutions Foundations of Program Design, 5th edition”, Pearson Education, Inc. pp. 220-222).
public class Coin {
private final int HEADS = 0;
private 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;
}
}
public class TestCoin {
public static void main(String[] args) {
Coin myCoin = new Coin();
myCoin.flip();
System.out.println(myCoin);
if(myCoin.isHeads()){
System.out.println("The coin has head and you win.");
}else{
System.out.println("The coin has tail and you lose.");
}
}
}
To help your understanding, I give the following brief explanation. The Coin class stores two integer constants (HEADS and TAILS) that represent the two possible states of the coin, and an instance variable called face that represents the current state of the coin. The Coin constructor initially flips the coin by calling the flip method, which determines the new state of the coin by randomly choosing a number (either 0 or 1). Since the random() method returns a random value in the range [0-1) (the left end is closed and the right end is open), the result of the expression (int)(Math.random()*2) is a random 0 or 1. The isHeads() method returns a Boolean value based on the current face value of the coin. The toString() method uses an if-else statement to determine which character string to return to describe the coin. The toString() method is automatically called when the myCoin object is passed to println() in the main method.
The TestCoin class instantiates a Coin object, flips the coin by calling the flip method in the Coin object, then uses an if-else statement to determine which of two sentences gets printed based on the result.
After running the above two classes, the following is one of the possible outputs:
Tails
The coin has tail and you lose.
Design and implement a new class MonetaryCoin that is a child of the Coin class
The new class should satisfy the following requirement. Besides all the features existed in the Coin class, the MonetaryCoin will have an extra instance variable called value to store the value of a coin. The variable value is the type of integer and has the range of 0-10. The derived class should also have an extra method getValue() that returns the value. The new class should have two constructors: one is the no-argument constructor MonetaryCoin(), the other takes one parameter aValue of integer that is used to initialize the instance variable value. According to good practice, in the body of constructor, the first line should call its super class’ no-arg constructor. To demonstrate that your new class not only has the old feature of keeping track of heads or tails but also has the new feature of keeping track of value, you should overwrite/override the method toString(). The specification for the new toString() is:
Header: String toString()
This method gives values of all instance variables as a string.
Parameters: no.
Precondition: no.
Returns: a string that takes one of the following value:
If the face is 0, value is x (here it represents an integer in the range 0..10).
The returned string should be “The coin has head and value is x.”
If the face is 1, value is x,
The returned string should be “The coin has tail and value is x.”
As a summary, this class should have four methods:
Constructor: public MonetaryCoin()
Constructor: public MonetaryCoin(int aValue)
Getter method: public int getValue()
toString method: public String toString()()
package HomeworkLib1;
public class MonetaryCoin extends Coin{
private int value;
MonetaryCoin(){
super();
this.value = 0;
}
MonetaryCoin(int aValue){
super();
if(aValue>=0 && aValue<=10)
this.value= aValue;
}
public int getValue() {
return this.value;
}
public String toString() {
String str="";
if(this.isHeads()) {
str += str + "The coin has head and the value is " + this.getValue();
}
else
str += str + "The coin has tail and the value is " + this.getValue();
return str;
}
}
package HomeworkLib1;
public class TestCoin {
public static void main(String[] args) {
Coin myCoin = new Coin();
myCoin.flip();
System.out.println(myCoin);
if(myCoin.isHeads()){
System.out.println("The coin has head and you win.");
}else{
System.out.println("The coin has tail and you lose.");
}
MonetaryCoin mc = new MonetaryCoin();
mc.flip();
System.out.println(mc);
mc = new MonetaryCoin(5);
mc.flip();
System.out.println(mc);
mc.flip();
mc.flip();
System.out.println(mc);
}
}

Comment your code. At the top of the program include your name, a brief description of...
Please write this C# code- Your main program will use a class named Coin, which has the following field: • A private 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 has 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 SideUp field accordingly. • A void method...
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...
# 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...
Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store { public final double SALES_TAX_RATE = 0.06; private String name; /** * Constructor to create a new store * @param newName */ public Store(String newName) { name = newName; } ...
Java Homework Help. Can someone please fix my code and have my
program compile correctly? Thanks for the help.
Specifications:
The class should have an int field named monthNumber that holds
the number of the month. For example, January would be 1, February
would be 2, and so forth. In addition, provide the following
methods.
A no- arg constructor that sets the monthNumber field to 1.
A constructor that accepts the number of the month as an
argument. It should...
In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...
import java.util.Scanner; public class Client{ public static void main(String args[]){ Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5); Scanner keyboard = new Scanner(System.in); int i = 0; int total = 0; while(true){ i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue(); dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...
Classes A Customer Points class maintains information about a customer. This information is the customer name (entire name as a String) and the amount of points (as an int) the customer has accrued. Methods: Constructor: Receives a name and sets the name field to the received name and sets the points field to 0 (zero). getName: Returns the customer's name getPoints: Returns the customer's current points toString: Returns a String representation of the current state in the form-name points, i.e....
***IN PYTHON 2.7****Augment the following code with a new class named 'Coin'. Coin should inherit from Die, with the following modifications; The constructor should not take any arguments; a coin always has two sides add a flip() method that uses the roll() method from the parent class. If roll returns 1; flip should return "HEADS". If roll returns a 2, flip should return "TAILS" Do not override the roll or rollMultiple methods from the parent class #!/usr/bin/python # your class...
Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...