Note that there are some explanatory texts on larger screens.

plurals
  1. POwhat to do with super in java?
    text
    copied!<p>I was asked to do this:</p> <blockquote> <p>Design and implement a class called MonetaryCoin that is derived from the Coin class presented in Chapter 5. Store a value in the monetary coin that represents its value and add getter and setter methods for the monetary value.</p> </blockquote> <p>The Coin class is as follows: </p> <pre><code>public class Coin { public final int HEADS = 0; public 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. // ----------------------------------------------- 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; } } </code></pre> <p>I came up with this:</p> <pre><code>public class MonetaryCoinHW extends Coin { public MonetaryCoinHW(int face) { setFace(face); } public int getFace() { if (isHeads()) { return HEADS; } return TAILS; } public void setFace( int newFace ) { while (newFace != getFace()) { flip(); } } </code></pre> <p>However, I keep getting syntax errors... Am I not using "super" correctly? I am completely confused; what is my mistake?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload