• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2614
  • Last Modified:

java inheritance

public class Coin
{
   protected  final int HEADS = 0;
   protected final int TAILS = 1;

   protected int face;

   
   public Coin ()
   {
      flip();
   }

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

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

   
   public String toString()
   {
      String faceName;

      if (face == HEADS)
         faceName = "Heads";
      else
         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
0
Ansary100
Asked:
Ansary100
1 Solution
 
InteractiveMindCommented:
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?
0
 
delphoCommented:
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++) {
         coins[i].flip();
         System.out.println(coins[i].toString());
      }
   }

   public static void main(String[] args) {

     FinancialClass App = new FinancialClass();
     App.addCoins();
     App.flipCoins();

   }

}

And that's all you need to know !
=)


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now