LittleRedHat
Instructor
I thought I was doing fine until I came to try to "insert at index". Now I'm lost!!!
My Topping and Base Classes don't appear to be causing any problems.
The Pizza class compiles.
The TestPizza wont compile with ...
Pizza1.insertTopping ("cheese", 40F, 1.5F);
It gives the error ...
insertTopping(int,java.lang.String,float,float) in Pizza cannot be applied to (java.lang.String,float,float)
Pizza1.insertTopping ("cheese", 40F, 1.5F);
if I exclude the line ... it compliles, but when run throws a NullPointerException at Pizza1.addTopping("tomatoPaste", 25F, 1F); referencing ...
Pizza Class : toppingList[numToppings] [numToppings] = new Topping(newType, newWeight, newCost);
numToppings++ ;
With apologies for the long, long listings (I don't know where or how many errors I have made, so have included all!) ...
And thanks in advance for any help ...
LRH
public class Pizza {
// CREATE INSTANCE VARIABLES
Topping[ ] [ ] toppingList; // List of toppings
int numToppings; // Number of toppings
String pizzaName;
Base pizzaBase;
// USE CONSTANTS TO SET MAXIMUM NUMBER OF TOPPINGS
// AND COST OF DOUGH
public static final int MAXTOPPINGS = 6; // Maximum number of Toppings
static final float DOUGHCOST = 0.5F; // Cost of Dough - pence per cc
//DEFAULT CONSTRUCTOR TO SET UP "BASIC" PIZZA
public Pizza ( ) {
pizzaBase = new Base( );
pizzaName = "Basic";
numToppings = 0;
toppingList = new Topping[MAXTOPPINGS] [ ];
for(int i = 0; i <numToppings; i++)
{
toppingList = new Topping( );
}
}
//OVERLOAD CONSTRUCTOR USING 2-DIMENSION ARRAY TO INDEX AVAILABLE TOPPINGS
public Pizza (String newType, float newWeight, float newCost) {
toppingList = new Topping[6] [2];
toppingList[0] [1] = new Topping("tomatoPaste", 0F, 0F);
toppingList[0] [2] = new Topping(null, 0F, 0F);
toppingList[1] [1] = new Topping("cheese", 0F, 0F);
toppingList[1] [2] = new Topping(null, 0F, 0F);
toppingList[2] [1] = new Topping("onions", 0F, 0F);
toppingList[2] [2] = new Topping("peppers", 0F, 0F);
toppingList[3] [1] = new Topping("pepperoni", 0F, 0F);
toppingList[3] [2] = new Topping("ham", 0F, 0F);
toppingList[4] [1] = new Topping("extraCheese", 0F, 0F);
toppingList[4] [2] = new Topping(null, 0F, 0F);
toppingList[5] [1] = new Topping("pineapple", 0F, 0F);
toppingList[5] [2] = new Topping("mushroom", 0F, 0F);
toppingList[6] [1] = new Topping("tuna", 0F, 0F);
toppingList[6] [2] = new Topping("anchovies", 0F, 0F);
}
// METHOD TO ADD TOPPINGS
public void addTopping (String newType, float newWeight, float newCost){
if (numToppings == MAXTOPPINGS)
{
System.out.println("Only 6 toppings allowed. The last topping will not be added"
;
return;
}
toppingList[numToppings] [numToppings] = new Topping(newType, newWeight, newCost);
numToppings++ ;
}
// METHOD TO REMOVE TOPPING
public void removeTopping(String type, float weight, float cost){
int index = findIndex( new Topping(type, weight, cost));
if (index == -1)
{
System.out.println("Topping not found - unable to remove."
;
return;
}
for(int i = index; i <numToppings - 1 ; i++ )
toppingList = toppingList [i + 1];
numToppings-- ;
}
// METHOD TO LIST TOPPINGS
public void listPizza( ){
System.out.println("\n\n PIZZA NAME - " + pizzaName);
for (int i = 0; i <numToppings ; i ++)
System.out.println(" " + toppingList .type);
}
// METHOD TO LIST ALL TOPPING INFORMATION
public void listAllToppings( ){
System.out.println("\n\n Topping details for " + pizzaName + " pizza: "
;
for (int i = 0; i <numToppings ; i ++)
System.out.println( toppingList.toString( ));
}
// METHOD TO INSERT TOPPING
public void insertTopping (int index, String newType, float newWeight, float newCost){
if (numToppings == MAXTOPPINGS)
{
System.out.println("Only 6 toppings allowed.\nThe last topping will not be inserted"
;
return;
}
if (index >numToppings)
addTopping (newType, newWeight, newCost );
else
{
for (int i =numToppings -1; i >= (index-1); i-- )
toppingList [i + 1] = toppingList;
toppingList [index-1] [index-1]= new Topping( newType, newWeight, newCost);
numToppings++ ;
}
}
// METHOD FOR PIZZA ASSEMBLY
public void outputInstructions ( ){
System.out.println("\n\n Instructions for making " + pizzaName + " pizza:"
;
System.out.println(" Take base of diameter " + pizzaBase.width +
"mm and thickness " + pizzaBase.thickness + "mm."
;
for (int i = 0; i <numToppings; i++)
{
System.out.println(" Add " + toppingList .weight +
" grams of " + toppingList .type + "."
;
}
}
// METHOD TO CALCULATE PIZZA WEIGHT
public float getWeight ( ){
float w = pizzaBase.volume()/1000;
for (int i = 0; i <numToppings; i++)
w = w + toppingList .weight;
return w;
}
// METHOD TO CALCULATE PIZZA COST
public float getCost ( ){
float c = pizzaBase.volume()/1000 * DOUGHCOST;
for (int i = 0; i <numToppings; i++)
c = c + (toppingList .cost);
return c;
}
// METHOD TO CHECK FOR IDENTICAL TOPPINGS
private int findIndex(Topping top){
for(int i = 0; i <numToppings ; i++ )
if (toppingList.equals (top))
{
System.out.println("Topping recognised."
;
return i ;
}
return -1 ;
}
}
import java.util.* ;
public class TestPizza {
public static void main(String args[ ]){
//Create new pizza for testing
Pizza Pizza1 = new Pizza();
Pizza1.addTopping("tomatoPaste", 25F, 1F);
Pizza1.addTopping("cheese", 40F, 1.5F);
Pizza1.addTopping("peppers", 15.5F, 3F);
Pizza1.addTopping("pepperoni", 30F, 3.5F);
//List pizza toppings
System.out.println ("Pizza Toppings"
;
Pizza1.listPizza( );
//List all topping information
System.out.println ("Pizza Toppings Details"
;
Pizza1.listAllToppings( );
waitForKey( );
System.out.println ("test for adding toppings"
;
//Test adding toppings methods - incl. exceed maximum
System.out.println ("\n Add Extra Cheese topping..."
;
Pizza1.addTopping("extraCheese", 40F, 1.5F);
System.out.println ("\n Add Mushrooms topping..."
;
Pizza1.addTopping("mushroom", 25F, 2F);
System.out.println ("\n Add Tuna topping... \n"
;
Pizza1.addTopping("tuna", 15F, 3F);
//List pizza toppings
Pizza1.listPizza( );
waitForKey( );
System.out.println ("Test for removing and replacing toppings"
;
//Test "equals" method - topping does not exist
System.out.println ("Remove Onions topping..."
;
Pizza1.removeTopping ("Onions", 20F, 2F);
//Test removing toppings
System.out.println ("Remove Extra Cheese topping..."
;
Pizza1.removeTopping ("extraCheese", 40F, 1.5F);
// Test adding replacement topping
System.out.println ("\n Add Anchovies topping... \n"
;
Pizza1.addTopping( "Anchovies", 15F, 3F);
// Remove 1 topping to leave below maximum for testing "insert topping"
System.out.println ("Remove Peppers topping..."
;
Pizza1.removeTopping ("peppers", 15.5F, 3F);
//List pizza toppings
Pizza1.listPizza( );
waitForKey ( );
//System.out.println ("Insert Onions topping..."
;
//Pizza1.insertTopping ("Onions", 20F, 2F);
System.out.println ("Pizza Assembly Test"
;
//Test Pizza assembly
Pizza1.outputInstructions( );
waitForKey ( );
System.out.println ("Weight Calculation Test"
;
//Test weight calculation
System.out.println ("The pizza weight is ... " + Pizza1.getWeight( ));
waitForKey( );
System.out.println ("Cost Calculation Test"
;
//Test cost calculation
System.out.println ("The pizza cost is ... " + Pizza1.getCost( ));
waitForKey( );
}
//METHOD TO PAUSE OUTPUT, USING RETURN TO CONTINUE
private static void waitForKey( ){
System.out.println("\nPRESS RETURN TO CONTINUE "
;
Input.readChar( );
}
}
My Topping and Base Classes don't appear to be causing any problems.
The Pizza class compiles.
The TestPizza wont compile with ...
Pizza1.insertTopping ("cheese", 40F, 1.5F);
It gives the error ...
insertTopping(int,java.lang.String,float,float) in Pizza cannot be applied to (java.lang.String,float,float)
Pizza1.insertTopping ("cheese", 40F, 1.5F);
if I exclude the line ... it compliles, but when run throws a NullPointerException at Pizza1.addTopping("tomatoPaste", 25F, 1F); referencing ...
Pizza Class : toppingList[numToppings] [numToppings] = new Topping(newType, newWeight, newCost);
numToppings++ ;
With apologies for the long, long listings (I don't know where or how many errors I have made, so have included all!) ...
And thanks in advance for any help ...
LRH
public class Pizza {
// CREATE INSTANCE VARIABLES
Topping[ ] [ ] toppingList; // List of toppings
int numToppings; // Number of toppings
String pizzaName;
Base pizzaBase;
// USE CONSTANTS TO SET MAXIMUM NUMBER OF TOPPINGS
// AND COST OF DOUGH
public static final int MAXTOPPINGS = 6; // Maximum number of Toppings
static final float DOUGHCOST = 0.5F; // Cost of Dough - pence per cc
//DEFAULT CONSTRUCTOR TO SET UP "BASIC" PIZZA
public Pizza ( ) {
pizzaBase = new Base( );
pizzaName = "Basic";
numToppings = 0;
toppingList = new Topping[MAXTOPPINGS] [ ];
for(int i = 0; i <numToppings; i++)
{
toppingList = new Topping( );
}
}
//OVERLOAD CONSTRUCTOR USING 2-DIMENSION ARRAY TO INDEX AVAILABLE TOPPINGS
public Pizza (String newType, float newWeight, float newCost) {
toppingList = new Topping[6] [2];
toppingList[0] [1] = new Topping("tomatoPaste", 0F, 0F);
toppingList[0] [2] = new Topping(null, 0F, 0F);
toppingList[1] [1] = new Topping("cheese", 0F, 0F);
toppingList[1] [2] = new Topping(null, 0F, 0F);
toppingList[2] [1] = new Topping("onions", 0F, 0F);
toppingList[2] [2] = new Topping("peppers", 0F, 0F);
toppingList[3] [1] = new Topping("pepperoni", 0F, 0F);
toppingList[3] [2] = new Topping("ham", 0F, 0F);
toppingList[4] [1] = new Topping("extraCheese", 0F, 0F);
toppingList[4] [2] = new Topping(null, 0F, 0F);
toppingList[5] [1] = new Topping("pineapple", 0F, 0F);
toppingList[5] [2] = new Topping("mushroom", 0F, 0F);
toppingList[6] [1] = new Topping("tuna", 0F, 0F);
toppingList[6] [2] = new Topping("anchovies", 0F, 0F);
}
// METHOD TO ADD TOPPINGS
public void addTopping (String newType, float newWeight, float newCost){
if (numToppings == MAXTOPPINGS)
{
System.out.println("Only 6 toppings allowed. The last topping will not be added"
return;
}
toppingList[numToppings] [numToppings] = new Topping(newType, newWeight, newCost);
numToppings++ ;
}
// METHOD TO REMOVE TOPPING
public void removeTopping(String type, float weight, float cost){
int index = findIndex( new Topping(type, weight, cost));
if (index == -1)
{
System.out.println("Topping not found - unable to remove."
return;
}
for(int i = index; i <numToppings - 1 ; i++ )
toppingList = toppingList [i + 1];
numToppings-- ;
}
// METHOD TO LIST TOPPINGS
public void listPizza( ){
System.out.println("\n\n PIZZA NAME - " + pizzaName);
for (int i = 0; i <numToppings ; i ++)
System.out.println(" " + toppingList .type);
}
// METHOD TO LIST ALL TOPPING INFORMATION
public void listAllToppings( ){
System.out.println("\n\n Topping details for " + pizzaName + " pizza: "
for (int i = 0; i <numToppings ; i ++)
System.out.println( toppingList.toString( ));
}
// METHOD TO INSERT TOPPING
public void insertTopping (int index, String newType, float newWeight, float newCost){
if (numToppings == MAXTOPPINGS)
{
System.out.println("Only 6 toppings allowed.\nThe last topping will not be inserted"
return;
}
if (index >numToppings)
addTopping (newType, newWeight, newCost );
else
{
for (int i =numToppings -1; i >= (index-1); i-- )
toppingList [i + 1] = toppingList;
toppingList [index-1] [index-1]= new Topping( newType, newWeight, newCost);
numToppings++ ;
}
}
// METHOD FOR PIZZA ASSEMBLY
public void outputInstructions ( ){
System.out.println("\n\n Instructions for making " + pizzaName + " pizza:"
System.out.println(" Take base of diameter " + pizzaBase.width +
"mm and thickness " + pizzaBase.thickness + "mm."
for (int i = 0; i <numToppings; i++)
{
System.out.println(" Add " + toppingList .weight +
" grams of " + toppingList .type + "."
}
}
// METHOD TO CALCULATE PIZZA WEIGHT
public float getWeight ( ){
float w = pizzaBase.volume()/1000;
for (int i = 0; i <numToppings; i++)
w = w + toppingList .weight;
return w;
}
// METHOD TO CALCULATE PIZZA COST
public float getCost ( ){
float c = pizzaBase.volume()/1000 * DOUGHCOST;
for (int i = 0; i <numToppings; i++)
c = c + (toppingList .cost);
return c;
}
// METHOD TO CHECK FOR IDENTICAL TOPPINGS
private int findIndex(Topping top){
for(int i = 0; i <numToppings ; i++ )
if (toppingList.equals (top))
{
System.out.println("Topping recognised."
return i ;
}
return -1 ;
}
}
import java.util.* ;
public class TestPizza {
public static void main(String args[ ]){
//Create new pizza for testing
Pizza Pizza1 = new Pizza();
Pizza1.addTopping("tomatoPaste", 25F, 1F);
Pizza1.addTopping("cheese", 40F, 1.5F);
Pizza1.addTopping("peppers", 15.5F, 3F);
Pizza1.addTopping("pepperoni", 30F, 3.5F);
//List pizza toppings
System.out.println ("Pizza Toppings"
Pizza1.listPizza( );
//List all topping information
System.out.println ("Pizza Toppings Details"
Pizza1.listAllToppings( );
waitForKey( );
System.out.println ("test for adding toppings"
//Test adding toppings methods - incl. exceed maximum
System.out.println ("\n Add Extra Cheese topping..."
Pizza1.addTopping("extraCheese", 40F, 1.5F);
System.out.println ("\n Add Mushrooms topping..."
Pizza1.addTopping("mushroom", 25F, 2F);
System.out.println ("\n Add Tuna topping... \n"
Pizza1.addTopping("tuna", 15F, 3F);
//List pizza toppings
Pizza1.listPizza( );
waitForKey( );
System.out.println ("Test for removing and replacing toppings"
//Test "equals" method - topping does not exist
System.out.println ("Remove Onions topping..."
Pizza1.removeTopping ("Onions", 20F, 2F);
//Test removing toppings
System.out.println ("Remove Extra Cheese topping..."
Pizza1.removeTopping ("extraCheese", 40F, 1.5F);
// Test adding replacement topping
System.out.println ("\n Add Anchovies topping... \n"
Pizza1.addTopping( "Anchovies", 15F, 3F);
// Remove 1 topping to leave below maximum for testing "insert topping"
System.out.println ("Remove Peppers topping..."
Pizza1.removeTopping ("peppers", 15.5F, 3F);
//List pizza toppings
Pizza1.listPizza( );
waitForKey ( );
//System.out.println ("Insert Onions topping..."
//Pizza1.insertTopping ("Onions", 20F, 2F);
System.out.println ("Pizza Assembly Test"
//Test Pizza assembly
Pizza1.outputInstructions( );
waitForKey ( );
System.out.println ("Weight Calculation Test"
//Test weight calculation
System.out.println ("The pizza weight is ... " + Pizza1.getWeight( ));
waitForKey( );
System.out.println ("Cost Calculation Test"
//Test cost calculation
System.out.println ("The pizza cost is ... " + Pizza1.getCost( ));
waitForKey( );
}
//METHOD TO PAUSE OUTPUT, USING RETURN TO CONTINUE
private static void waitForKey( ){
System.out.println("\nPRESS RETURN TO CONTINUE "
Input.readChar( );
}
}