As Sa
As Sa

Reputation: 31

what to do with super in java?

I was asked to do this:

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.

The Coin class is as follows:

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; 
        } 
} 

I came up with this:

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();
                }
      }

However, I keep getting syntax errors... Am I not using "super" correctly? I am completely confused; what is my mistake?

Upvotes: 1

Views: 1741

Answers (4)

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Either

remove the super(face) line from you class MonetaryCoinHW. And simple call setFace(face). But also note that the face variable is defined private in your super class so you cannot access it.

By default it will call the no argument constructor of super class.

OR

define Coin(int face) constructor in class Coin

You only need to define the special constructor in Coin class if you want to do something different.

Upvotes: 0

hajirazin
hajirazin

Reputation: 817

No you are not calling super correctly. You need to have constructor of coin with one argument that is int. i.e.

public Coin (int face) 
{ 
      this.face = face;
}

Upvotes: 0

user1131435
user1131435

Reputation:

No, you are not using super() correctly.

super() calls the superconstructor - in this case, it will call the inherited Coin(). Since no constructor exists inside Coin() for Coin(int face), your subclass can't invoke it.

There are to ways to deal with this. I believe you need to run setFace(face). This will properly initialize the value of the coin, and I think fits your problem the best. However, you could also add the Coin(int face) constructor to the Coin class. You would also have to give the Coin a way to hold a value, though.

Upvotes: 4

jagbandhuster
jagbandhuster

Reputation: 707

The class coin needs to have the following constructor for your sub-class to work correctly.

public Coin(int face) {
    this.face = face;
}

This will solve the problem super(face).

Upvotes: 0

Related Questions