I’m in Java programming (I’m on my first year at uni). I’ve got an assessment where I need to create 3 classes. In one of the classes I need to have a constructor with a parameter of String type. It needs to allow me to create a new object (pack of crisps) in which I am supposed to state the flavour of it.
Whenever I try to create the object I get the textbox where I can type in the flavour of crisps but when I press ok I get the error (e.g. ) Cannot find symbol – variable bbq.
I had a similar example before which took int type and it worked fine.
When I compare my code with other – working examples – I really can’t find what I am doing wrong.
I would appreciate if someone could advise.
public class PackOfCrisps
{
private String flavour;
private int numOfCrisps;
private boolean open;
/**
* Constructor for a single pack of crisps.
*/
public PackOfCrisps(String newFlavour)
{
flavour = newFlavour;
numOfCrisps = 10;
open = true;
}
/**
* Return whether the packet is empty.
*/
public boolean isEmpty()
{
if (numOfCrisps == 0)
{
return true;
} else
return false;
}
/**
* Return whether the packet is closed.
*/
public boolean isClosed()
{
return open;
}
/**
* Change the boolean value of 'open' variable.
*/
public void open()
{
open = !open;
}
/**
* Return the flavour of the packet of crisps.
*/
public String getFlavour()
{
return flavour;
}
/**
* Decrease the number of crisps by 1 until the packet is empty.
*/
public void eatCrisps()
{
if (open)
{
System.out.println("Need to open the packet first!");
} else if ((numOfCrisps > 1) && (numOfCrisps <= 10)){
numOfCrisps = numOfCrisps - 1;
System.out.println(numOfCrisps);
} else {
System.out.println("The packet is empty!");
}
}
}
UPDATE
I’m using BlueJ for my programming.
I would imagine that it must be something pretty ‘simple’ that I am missing because it is my 3rd week at university and we literally started from scratch.
UPDATE 2
I’m sorry if my answers don’t always describe everything 100% but because I’m still a noob at programming I am bound to miss some info. I think this would be the answer to your question: because I use BlueJ, when I have a class in compiler (I believe displayed in a class diagram) I just right-click it and choose “new PackOfCrisps(String newFlavour)” and then I get the box in which I can type in crisps flavour but when I press ‘Ok’ I get error message.

I tried to play with other options to see if I can get any more info but with no success.
Note: This was a comment above, but I am “elevating” it to an answer.
Based on your Update 2 and the screenshots above here is my guess.
The text box that BlueJ pops up probably allows you to type in a variable name or a string. If you want it to be a string then type in “BBQ” with the quotes.