-1

My teacher asked us to create a class named Pizza that stores information about a single pizza, which should contain:

  • Private instance variables that store the size of the pizza (small, medium, or large), the number of cheese, pepperoni and ham toppings.

  • Constructor(s) that set all of the instance variables.

  • Public methods to get and set the instance variables.

  • A public method name calcCost() that returns a double that is the cost of the pizza.

Pizza cost is determined by size. Small = $10 + $2 per topping. Medium = $12 + $2 per topping. Large = $14 + $2 per topping.

  • A public method named getDescription() that returns a String containing the pizza size, quantity of each cost and the pizza cost as calculated by calcCost().

Here is my class:

public class Pizza
{
     private String pizzaSize;
     private int numOfCheese;
     private int numOfPepperoni;
     private int numOfHam;

public Pizza (String a, int b, int c, int d)
{
    a = pizzaSize;
    b = numOfCheese;
    c = numOfPepperoni;
    d = numOfHam;
}

public String getSize()
{return pizzaSize;}

public int getCheese()
{return numOfCheese;}

public int getPepperoni()
{return numOfPepperoni;}

public int getHam()
{return numOfHam;}

public String getDescription()
{
    return ("Size: " + pizzaSize + "," + "\t" + "Cheese Toppings: " + numOfCheese + 
        "\t" + "Pepperoni Toppings: " + numOfPepperoni + "\t" + "Ham Toppings: " + 
        numOfHam + "\t");
}

public double calcCost()
{
    double cost = 0.0;
    int numOfToppings;
    int costOfToppings;
    if (pizzaSize.equals("Small"))
    {cost = 10.0;}
    else if (pizzaSize.equals("Medium"))
    {cost = 12.0;}
    else if (pizzaSize.equals("Large"))
    {cost = 14.0;}

    numOfToppings = ( (numOfCheese + numOfPepperoni) + numOfHam);
    costOfToppings = (numOfToppings * 2);
    cost += numOfToppings;
    return cost;
    }
}

Here is the demo driver that goes with it:

    public class Question11
{
    public static void main(String[] args)
    {
        // Create a few sample pizzas and output their prices
        Pizza supreme = new Pizza ("Large",1,2,1);
        Pizza cheese = new Pizza ("Medium",2,0,0);
        Pizza pepperoni = new Pizza ("Small",0,0,2);
        System.out.printf( "%-75sCost: $%5.2f %n",     supreme.getDescription(), supreme.calcCost() );
        System.out.printf( "%-75sCost: $%5.2f %n", cheese.getDescription(), cheese.calcCost() );
        System.out.printf( "%-75sCost: $%5.2f %n", pepperoni.getDescription(), pepperoni.calcCost() );
    }
 } // Question 11

The java.lang.NullPointerException: null error comes up on the size.equals part and the supreme.calcCost() for the driver. Any help is appreciated, and yes I read the other java.lang.NullPointerException forums/questions on this site and I am still confused.

2 Answers2

2
public Pizza (String a, int b, int c, int d) {
    pizzaSize = a;       // assigns a value to pizzaSize
    numOfCheese = b;
    numOfPepperoni = c;
    numOfHam = d;
}

Change your Pizza constructor.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
1

your constructor goes backwards, you're assigning the variables from the method instead of the class fields:

public Pizza (String a, int b, int c, int d)
{
    a = pizzaSize;
    b = numOfCheese;
    c = numOfPepperoni;
    d = numOfHam;
}

should be:

public Pizza (String a, int b, int c, int d)
{
    pizzaSize = a;
    numOfCheese = b;
    numOfPepperoni = c;
    numOfHam = d;
}

I also suggest giving it more meaningful names than a, b, c, d

Nir Levy
  • 12,750
  • 3
  • 21
  • 38