Help with homework problem. Almost done.

Design and implement a class called MonetaryCoin that is derived from the Coin class presented in Chapter 5. Store an integer in the MonetaryCoin that represents its value and add a method that returns its value. Create a main driver class to instantiate and compute the sum of several MonetaryCoin objects with user entered values. Validate your Input. Demonstrate that a monetary coin inherits its parent’s ability to be flipped.


It's throwing this error message:
Exception in thread "main" java.lang.NullPointerException
at TestMonetaryCoin.main(TestMonetaryCoin.java:19)
at line
y = Math.abs(gen.nextInt()) % value.length;
Can't figure it out please help.
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;
}
}
public class MonetaryCoin extends Coin {

protected 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) {

double 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;*/

}

}
import java.util.*;

public class TestMonetaryCoin {

static double sum;
private static Random gen;
public static void main(String [] args) {
Random r = new Random();
//double sum;
int [ ] value = { 1, 2, 3, 4, 5, 6, 7, 8};

for (int j = 0; j < value.length; j++){

sum+=value[j];}
System.out.println("\t" + sum);
int y, j;
MonetaryCoin [] coins = new MonetaryCoin[10];
for (j = 0; j < coins.length; j++) {
y = Math.abs(gen.nextInt()) % value.length;
coins [j] = new MonetaryCoin(value[y]);
}
/*System.out.println("Before the coins are sorted:");
for (i = 0; i < coins.length; i++)
System.out.println("\t" + coins [i]);
System.out.println("\nAfter the coins are sorted:");
Arrays.sort(coins);*/
for (j = 0; j < coins.length; j++)
System.out.println("\t" + coins [j]);
System.out.println("\n" + coins[0]
+ " before flipping is "
+ coins[0].getValue());
System.out.println("after flipping is ");
		coins[0].flip(); //Flip the coin
		System.out.println("after flipping is " + coins[0].getValue()); //Print out the value of the coin after flipping 

}
} 

Comments

  • This line?
    y = Math.abs(gen.nextInt()) % value.length;
    

    It looks like you're trying to use your gen Random class but you haven't even instantiated it or assigned it to anything. Did you mean to do:
    y = Math.abs(r.nextInt()) % value.length;
    

    Instead? Because you've instantiated r however it doesn't appear you've used it anywhere.
  • I think you should through an exception ie try and catch(Exception e){ in the methods to check where the error is coming from. Then get back.
Sign In or Register to comment.