Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design and implement a class called MonetaryCoin that is derived from the Coin class presented in Chapter 5. Store an integer in the MonetaryCoin that

Design and implement a class called MonetaryCoin that is derived from the Coin class presented in Chapter 5. Store an integer in the MonetaryCoin that represents its value and add a method that returns its value. Create a main driver class to instantiate and compute the sum of several MonetaryCoin objects. Demonstrate that a monetary coin inherits its parents ability to be flipped. Java Software Solutions 7th edition

//********************************************************************

// Coin.java Author: Lewis/Loftus

//

// Represents a coin with two sides that can be flipped.

//********************************************************************

public class Coin

{

private final int HEADS = 0;

private final int TAILS = 1;

private int face;

//-----------------------------------------------------------------

// Sets up the coin by flipping it initially.

//-----------------------------------------------------------------

public Coin ()

{

flip();

}

//-----------------------------------------------------------------

// Flips the coin by randomly choosing a face value.

//-----------------------------------------------------------------

public void flip ()

{

face = (int) (Math.random() * 2);

}

//-----------------------------------------------------------------

// Returns true if the current face of the coin is heads.

//-----------------------------------------------------------------

public boolean isHeads ()

{

return (face == HEADS);

}

//-----------------------------------------------------------------

// Returns the current face of the coin as a string.

//-----------------------------------------------------------------

public String toString()

{

String faceName;

if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

management therory

Answered: 1 week ago

Question

Understand how customers respond to effective service recovery.

Answered: 1 week ago