Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Resolve this java code class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; // Sets up
Resolve this java code
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; }}class MonetaryCoin1 extends Coin { int value; // Returns value of MonetaryCoin. public int getValue() { return value; }}public class MonetaryCoin { public static void main(String[] args) { MonetaryCoin1 coin1 = new MonetaryCoin1(); coin1.value = 5; System.out.println("Value of coin1 is: " + coin1.getValue()); MonetaryCoin1 coin2 = new MonetaryCoin1(); coin2.value = 10; System.out.println("Value of coin2 is: " + coin2.getValue()); MonetaryCoin1 coin3 = new MonetaryCoin1(); coin3.value = 1; System.out.println("Value of coin3 is: " + coin3.getValue()); System.out.println("Sum of all coins is: " + (coin1.getValue() + coin2.getValue() + coin3.getValue())); System.out.println("coin1 is: " + coin1.toString()); coin1.flip(); System.out.println("After flip coin1 is: " + coin1.toString()); }}
so above is my code and my expected output is suppose to be
Heads1?Heads5?Heads10?Heads25?Heads50?Tails100?Tails100??TotalValue:291?
But the actual output I get is
Valueofcoin1is:5?Valueofcoin2is:10?Valueofcoin3is:1?Sumofallcoinsis:16?coin1is:Tails?Afterflipcoin1is:Heads?
What part of my code would I need to change to get the desired output, I'm confused. Also note I have MonetaryCoin and MonetaryCoin1 there is a difference. Below is the original question if its of any help
9.1: MonetaryCoin 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. Add a toString method that appends a space followed by the coin's value to the Coin's String representation. Driver. Instead of creating a main driver class, include a main method in the MonetaryCoin class itself to exercise the class's behavior. Your method should create an array of 7 MonetaryCoin objects with values 1,5,10,25,50,100,100 and then iterate through the array, flipping in each coin. The method should then iterate through the array again, invoking the getValue method and adding up the resulting values. The String representation of each coin should then be printed on a line by itself, followed by, on a line by itself, the sum of the values that was computed (preceded by the label "Total Value: ".
Step by Step Solution
★★★★★
3.54 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
The issue in your code is that the toString method in the MonetaryCoin1 class does not correctly app...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started