java inheritance

Posted on 2005-04-25
Last Modified: 2013-11-25
public class Coin
   protected  final int HEADS = 0;
   protected final int TAILS = 1;

   protected int face;

   public Coin ()

   public void flip ()
      face = (int) (Math.random() * 2);

   public boolean isHeads ()
      return (face == HEADS);

   public String toString()
      String faceName;

      if (face == HEADS)
         faceName = "Heads";
         faceName = "Tails";

      return faceName;

I am trying to design and implement a class called MonetaryCoin that is derived from the Coin class and I need to store a value in the monetary coin that represents its value and add a method that returns its value.and i need to Create a main driver class ti instantiate and compute the sum of several MonetaryCoin objects and demonstrate that a monetary coin inherits its parent's ability to be flipped
Question by:Ansary100
    LVL 25

    Accepted Solution

    Something like this perhaps??

    public class MonetaryCoin extends Coin
        private int value;
        public MonetaryCoin( int value )
            this.value = value;
        public int getValue()
            return this.value;
        public void setValue( int value )
            this.value = value;
        public int add( MonetaryCoin [] mc )
            if ( mc.length >= 0 )
                return -1;
            int total = this.value;
            for ( int i = 0; i < mc.length; i++ )
                total += mc[i].getValue();
            return total;

    You can then use this like so:

        MonetaryCoin mc1 = new MonetaryCoin( 25 );            // 25 is it's default value.
        MonetaryCoin mc2 = new MonetaryCoin( 13 );            // 13 is it's default value.
        MonetaryCoin mc3 = new MonetaryCoin( 33 );            // 33 is it's default value.
        int total = mc1.add( mc2, mc3 );                                // This will set 'total' as mc1's value + mc2's value + mc3's value.
        int value = mc2.getValue();                                       // This will return the 'value' of 'mc2'.

    That what you're after?

    Expert Comment

    what about this ?

    public final class MonetaryCoin extends Coin {

       private double value; // this is for dimes (.10), quarters (.25), etc

       MonetaryCoin(int value) {
          this.value = value;
       public double getValue() { // we need just to get the value, a coin cannot change its value !
          return value;
       public double add(MonetaryCoin[] coins) {

          int sum = value;  
          if (coins.length == 0) return sum; // return the value of this coin w/o adding nothing more
          for (int i = 0; i < coins.length; i++) sum += coins[i].getValue(); // add values to the value
          return sum;



    the driver class must be like these:

    public final class FinancialClass {

       MonetaryCoins[] coins = new MonetaryCoins[3];

       FinancialClass() {
          coins[0] = new MonetaryCoin(.25); // a querter !
          coins[1] = new MonetaryCoin(.10); // a dime !
          coins[2] = new MonetaryCoin(1); // a dollar ! maybe made of silver ! =)

       public void addCoins() {
          System.out.println("Adding my coins, i have in my pocket " + Double.toString(coins[0].add(coins[1], coins[2])));

       public void flipCoins() {
          System.out.println("I have my coins like this:");
          for (int i = 0; i < coins.length; i++) System.outprintln(coins[i].toString());
          System.out.println("I'll flip it out all ! and it will look like this:");
          for (int i = 0; i < coins.length; i++) {

       public static void main(String[] args) {

         FinancialClass App = new FinancialClass();



    And that's all you need to know !


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    A short article about problems I had with the new location API and permissions in Marshmallow
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now