This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/6/25

why am I getting this error, please explain

1
2
danpost danpost

2016/6/27

#
I would think that the fields on lines 9 through 21 would need to be declared outside the method (but still within the class) for them to be utilizable in the other methods. Where is the 'start' method being called from?
divinity divinity

2016/6/27

#
hi danpost it work but still have one or two errors
if(intList.contain(item))the item saying it is not initialized and the contain saying it cannot find symbol 

total + intValue(); this one here is saying the same as above. total(not initialzed) and intValue(cannot find symbol
divinity divinity

2016/6/27

#
the start method was something that I initiated to see how it will work.
danpost danpost

2016/6/27

#
The assignment of 'item' on line 62 only lasts for that case (case 1). That is because it assigns a local variable declared on that line; it does not assign the field declared at line 21. To assign the field declared on line 21, remove 'Integer' from the beginning of line 62. This still will not be a valid assignment for the other cases, however. You probably need to repeat the assignment within the code for cases where needed (such as case 2). If 'intValue' is a field (not a method), which it appears to be (as per line 17), then you should not have parenthesis after it. However, I do not believe that the value of the 'intValue' field was set to anything after being declared (its value remains zero as when first initialized).
danpost danpost

2016/6/27

#
To tell you the truth, I do not think that most of the fields declared between lines 15 through 21 are needed. I think you can just make them local to the methods they are used in or removed entirely: * the 'pushForward' boolean field is not used anywhere and can be removed * the 'userOption' int field is actually not used anywhere (will provide explanation if requested) * the 'intValue' int field is troublesome as getting the appropriate value for any item is not clear * the 'total' int field is one that will be necessary (keep this one); but questionable as far as type -- probably int okay (remove 'double' at beginning of line 89) * the 'input' int field is actually not used anywhere (same reason as 'userOption') * the 'integer' int field is not used at all (even by name) * the 'item' Integer reference field is probably not needed as it can be locally declared when needed in each case
divinity divinity

2016/6/27

#
okay thanks u
divinity divinity

2016/7/9

#
hi danpost i am doing another program even i havent fixed this last one. I have decided just for the time being, layed it down and i need help with this next one. this is a parking ticket. the error is the same error as before " cant find this symbol" but this time it is in the main, i have figured out the errors in the other classes and have since kinda fixed them but it is the one in the main part of the program. when i clicked the yellow and red buld at the side it is telling me to create a class but why do i have to if it is the main. all of the other class is done. here is the code.
public class ParkingSimulator 
{
    public static void main(String[] args)
    {
        Scanner userinput = new Scanner(System.in);
             
        //a green car was park for at least 125 minutes
        ParkingTicketSimulator parkingTicketSimulator = new ParkingTicketSimulator(); - the error is right here. the constructor ah believe this is 
        ParkedCar car = parkingTicketSimulator.new ParkedCar("Toyota", 2015, "Green", "ACD321",125);
        
        //60 minutes was purchased
        ParkingMeter meter  = parkingTicketSimulator.new ParkingMeter(60); 
        
        
        //officer Dibbles is on duty
        PoliceOfficer officer = parkingTicketSimulator.new PolicerOfficer("Sgt Robyn Dibbles", 6909);
        
        ParkingTicket ticket = officer.patrol(car, meter);
        
        //did the officer did his job by giving a ticket
        if(ticket != null)
        {
            System.out.println(ticket);
        }
        else
        {
            System.out.println("No crime was committed");
        }
    }
}
danpost danpost

2016/7/9

#
The 'main' is not a constructor. It is a set of code that is initially run when the program is started. You apparently do not have a constructor for the'ParkingTicketSimulator class which defaults to an empty constructor. This is not a problem, however. The problem is that you are referencing the object created from this class to try to create other type objects. Just remove 'parkingTicketSimulator.' from lines 9, 12 and 16.
divinity divinity

2016/7/9

#
can I ask a stupid question? can one make a constructor in the main and how do i make it
danpost danpost

2016/7/9

#
divinity wrote...
can I ask a stupid question? can one make a constructor in the main and how do i make it
The 'main' can be placed into any class; but, you are only allowed to have one 'main' in any project. All classes are subclasses of the Object class (except the Object class itself, of course) and have constructors, whether explicitly given or not. So, the answer is you can have a constructor along with a main in a class (the constructor would not be in the 'main' code-block, of course). To me, doing so would seem a bit strange, however, as the name of the class the main is in would usually be the project name, not the name of an object within the project.
You need to login to post a reply.
1
2