Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert at index

Status
Not open for further replies.

LittleRedHat

Instructor
Apr 20, 2001
202
GB
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(&quot;tomatoPaste&quot;, 0F, 0F);
toppingList[0] [2] = new Topping(null, 0F, 0F);
toppingList[1] [1] = new Topping(&quot;cheese&quot;, 0F, 0F);
toppingList[1] [2] = new Topping(null, 0F, 0F);
toppingList[2] [1] = new Topping(&quot;onions&quot;, 0F, 0F);
toppingList[2] [2] = new Topping(&quot;peppers&quot;, 0F, 0F);
toppingList[3] [1] = new Topping(&quot;pepperoni&quot;, 0F, 0F);
toppingList[3] [2] = new Topping(&quot;ham&quot;, 0F, 0F);
toppingList[4] [1] = new Topping(&quot;extraCheese&quot;, 0F, 0F);
toppingList[4] [2] = new Topping(null, 0F, 0F);
toppingList[5] [1] = new Topping(&quot;pineapple&quot;, 0F, 0F);
toppingList[5] [2] = new Topping(&quot;mushroom&quot;, 0F, 0F);
toppingList[6] [1] = new Topping(&quot;tuna&quot;, 0F, 0F);
toppingList[6] [2] = new Topping(&quot;anchovies&quot;, 0F, 0F);
}

// METHOD TO ADD TOPPINGS

public void addTopping (String newType, float newWeight, float newCost){
if (numToppings == MAXTOPPINGS)
{
System.out.println(&quot;Only 6 toppings allowed. The last topping will not be added&quot;);
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(&quot;Topping not found - unable to remove.&quot;);
return;
}
for(int i = index; i <numToppings - 1 ; i++ )
toppingList = toppingList [i + 1];
numToppings-- ;
}

// METHOD TO LIST TOPPINGS

public void listPizza( ){
System.out.println(&quot;\n\n PIZZA NAME - &quot; + pizzaName);
for (int i = 0; i <numToppings ; i ++)
System.out.println(&quot; &quot; + toppingList .type);
}

// METHOD TO LIST ALL TOPPING INFORMATION

public void listAllToppings( ){
System.out.println(&quot;\n\n Topping details for &quot; + pizzaName + &quot; pizza: &quot;);
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(&quot;Only 6 toppings allowed.\nThe last topping will not be inserted&quot;);
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(&quot;\n\n Instructions for making &quot; + pizzaName + &quot; pizza:&quot;);
System.out.println(&quot; Take base of diameter &quot; + pizzaBase.width +
&quot;mm and thickness &quot; + pizzaBase.thickness + &quot;mm.&quot;);
for (int i = 0; i <numToppings; i++)
{
System.out.println(&quot; Add &quot; + toppingList .weight +
&quot; grams of &quot; + toppingList .type + &quot;.&quot;);
}
}

// 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(&quot;Topping recognised.&quot;);
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(&quot;tomatoPaste&quot;, 25F, 1F);
Pizza1.addTopping(&quot;cheese&quot;, 40F, 1.5F);
Pizza1.addTopping(&quot;peppers&quot;, 15.5F, 3F);
Pizza1.addTopping(&quot;pepperoni&quot;, 30F, 3.5F);

//List pizza toppings
System.out.println (&quot;Pizza Toppings&quot;);
Pizza1.listPizza( );

//List all topping information
System.out.println (&quot;Pizza Toppings Details&quot;);
Pizza1.listAllToppings( );
waitForKey( );

System.out.println (&quot;test for adding toppings&quot;);

//Test adding toppings methods - incl. exceed maximum
System.out.println (&quot;\n Add Extra Cheese topping...&quot;);
Pizza1.addTopping(&quot;extraCheese&quot;, 40F, 1.5F);
System.out.println (&quot;\n Add Mushrooms topping...&quot;);
Pizza1.addTopping(&quot;mushroom&quot;, 25F, 2F);
System.out.println (&quot;\n Add Tuna topping... \n&quot;);
Pizza1.addTopping(&quot;tuna&quot;, 15F, 3F);

//List pizza toppings
Pizza1.listPizza( );
waitForKey( );

System.out.println (&quot;Test for removing and replacing toppings&quot;);

//Test &quot;equals&quot; method - topping does not exist
System.out.println (&quot;Remove Onions topping...&quot;);
Pizza1.removeTopping (&quot;Onions&quot;, 20F, 2F);

//Test removing toppings
System.out.println (&quot;Remove Extra Cheese topping...&quot;);
Pizza1.removeTopping (&quot;extraCheese&quot;, 40F, 1.5F);

// Test adding replacement topping
System.out.println (&quot;\n Add Anchovies topping... \n&quot;);
Pizza1.addTopping( &quot;Anchovies&quot;, 15F, 3F);

// Remove 1 topping to leave below maximum for testing &quot;insert topping&quot;
System.out.println (&quot;Remove Peppers topping...&quot;);
Pizza1.removeTopping (&quot;peppers&quot;, 15.5F, 3F);

//List pizza toppings
Pizza1.listPizza( );
waitForKey ( );

//System.out.println (&quot;Insert Onions topping...&quot;);
//Pizza1.insertTopping (&quot;Onions&quot;, 20F, 2F);

System.out.println (&quot;Pizza Assembly Test&quot;);

//Test Pizza assembly
Pizza1.outputInstructions( );
waitForKey ( );

System.out.println (&quot;Weight Calculation Test&quot;);

//Test weight calculation
System.out.println (&quot;The pizza weight is ... &quot; + Pizza1.getWeight( ));
waitForKey( );

System.out.println (&quot;Cost Calculation Test&quot;);

//Test cost calculation
System.out.println (&quot;The pizza cost is ... &quot; + Pizza1.getCost( ));
waitForKey( );
}

//METHOD TO PAUSE OUTPUT, USING RETURN TO CONTINUE

private static void waitForKey( ){
System.out.println(&quot;\nPRESS RETURN TO CONTINUE &quot;);
Input.readChar( );
}

}
 
Please cancel this question. I have now worked through the multiple errors of my ways and resolved the problem! [spin2]

LRH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top