0

so I had this java code below which the compiler said couldn't find symbol for my car class so had to create an empty class like this:

public class TrafficQueue {
private Car [] carArray;
private int numberOfcarsInQueue;


public TrafficQueue(int numberOfcarsInQueue){
carArray = new Car[numberOfcarsInQueue];


}
private class Car {} ; // define an empty Car class


public void add(Car car){
carArray[numberOfcarsInQueue] = car;
numberOfcarsInQueue ++;

}

public static void main(String[] args) {
        // TODO code application logic here


         TrafficQueue queueLane1 = new TrafficQueue(10);
        queueLane1.add("Red Chevrolet");   

i try to run and i get an error using queuelane1.add("Red Chevrolet")

from my previous knowledge of creating object arrays this is done as:

private Car [] carArray;

then initialised in the constructor as:

carArray = new Car[numberOfcarsInQueue];

but however this does not work until i create a new empty class. my question why do i have to create a new class?

and from my code how i do i add a new car object?

blueGOLD
  • 105
  • 3
  • 12
  • If you don't create a `Car` class, what type do you think the compiler should make your array? – azurefrog Oct 28 '16 at 18:48
  • `queueLane1.add("Red Chevrolet");` is adding a `String`, not a `Car`... – OneCricketeer Oct 28 '16 at 18:49
  • well been reading some text and none of them seem to create but somehow their code works. check this link pls http://www-scf.usc.edu/~csci455/curr/bigJava/ch16/section_3_4/CircularArrayQueue.java – blueGOLD Oct 28 '16 at 18:49
  • i have just read through a previous similar post which explains why this error occurs but unfortunately does not explain much further using a class... – blueGOLD Oct 28 '16 at 19:09

0 Answers0