Ansary100
asked on
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
{
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
}
public void flipCoins() {
System.out.println("I have my coins like this:");
for (int i = 0; i < coins.length; i++) System.outprintln(coins[i]
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
}
}
public static void main(String[] args) {
FinancialClass App = new FinancialClass();
App.addCoins();
App.flipCoins();
}
}
And that's all you need to know !
=)