• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing Arrays and Editing Objects Within Them From Other Class'  RSS feed

 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok basically I have my main Method which is where I have initialized an array of objects. I want to access this array, the objects within and the actual methods within the object from within another class. How would I go about doing this???

I keep on coming up with errors.

Here is some of my code to help you...

public static void main(String[]args)

{


//Declare Variables
int menuChoice = 2;
boolean quit = false;

// Init carArry and set variables
Car [] carArray;
carArray = new Car [10];



//Possition In Array Manufact Model E.Size Reg Millage MPG Status
carArray[0] = new Car("Vauxhall", "Corsa", 1.3, "GP 03 CBT", 8123, 35, 'Y');
carArray[1] = new Car("Vauxhall", "Agila", 1.2, "AB 03 FTR", 10045, 38, 'S');
carArray[2] = new Car("Vauxhall", "Agila", 1.0, "FT 01 YUT", 21488, 40, 'N');
carArray[3] = new Car("Vauxhall", "Astra", 1.6, "RT 51 DRE", 15032, 32, 'S');
carArray[4] = new Car("Vauxhall", "Astra", 2.2, "DR 51 KLP", 19933, 25, 'Y');
carArray[5] = new Car("Ford", "Fiesta", 1.0, "GP 53 REW", 4987, 40, 'Y');
carArray[6] = new Car("Ford", "Fiesta", 1.3, "SR 01 BHG", 1997, 35, 'N');
carArray[7] = new Car("Ford", "Mondeo", 1.6, "WE 02 GTY", 17342, 33, 'N');
carArray[8] = new Car("Ford", "Mondeo", 2.0, "QW 52 EWQ", 12345, 25, 'Y');
carArray[9] = new Car("Ford", "Orion", 1.8, "SD 01 TYR", 24459, 28, 'N');
}

Also here is one method from my Car object..

// Read the millage attribute
public double getMillage()

{
return millage;
}


How would I go about accessing this method from another class???

Thanks alot...
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry couldn't understood the question.

If you are asking about accessing any method of a class then you can do that by creating the object of that class and use "." to call the method of that class if it is public from outside that class.

Car car = new Car();
car.getMillage();

Similarly you can call the method of any object in that array you can call as following:

car[index].getMillage();

Is this you are trying to do? If no then properly describe your question.
[ May 14, 2004: Message edited by: Ali Gohar ]
 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class hireCar

{
public static void start()

{
System.out.println("This is the hire car option");
System.out.println ("");
System.out.println (carArray[3].getMillage());
}
}

I have created the following class to try and access the array. I run my main method, and then half way through it runs this method but when I try and compile it keeps on coming up with the following error.

C:\Documents and Settings\Mr Ben\Desktop\Java 2\hireCar.java:9: cannot resolve symbol
symbol : variable carArray
location: class hireCar
System.out.println (carArray[3].getMillage());
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class hireCar

{
public static void start()

{
System.out.println(" This is the hire car option");
System.out.println ("");
System.out.println (carArray[3].getMillage());
}
}


Where is the definition of carArray in this class. I can't see anywhere in this class you declared the carArray.

You have declared array in any other class and you are trying to access it in this class its not possible.

You can pass the Object of Car in this method and then you can use that.

class hireCar

{
public static void start(Car car)

{
System.out.println(" This is the hire car option");
System.out.println ("");
System.out.println (car.getMillage());
}
}

now you can call in that main method HireCar.start(carArray[3]);

If this is not the solution to your problem then describe the whole problem what you want to develop.
 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I want to create a program for a hire car company. At the moment the company has a fleet of 10 cars so the best way to store them would be as an array of objects.

The user has to be able to hire and return the cars, and add new cars to the current fleet of cars.

The main meny I have has 3 options..

#Hire Car
#Return Car
#Add New Car

I've just finished setting up the initial array of 10 'car' objects.

Each menu option loads up a new class where all the code for that particular option sits. So for the hireCar class I will have to be able to access the array of cars, search for cars that are currently available for hire and then select a car and edit an attribute in the car object to set it to 'hired'

The return class one shows a list of all the cars currently hired out and then the user selects a car to be returned which then updates the objects attributes such as millage and hire status. The return class then runs a method in the car object to work out the cost of the hire period based on new millage and how much fuel was used.

finally the Add Car menu option launches the addCar class which I hope will enable the user to add a new car to the array. Although I think I have to copy the array to a temp array which is bigger and then delete the previous array and remake the carArray based on the new details in the temp array.

I thought I could create the array in the Mainmethod and then access and update the objects via all the classes run by the main method.

Meh any help would be appreciated.
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Ben,

Make a class which will hold your cars, add cars in that and get the desired car from that collection. Use java.util.Arraylist to hold the list of cars instead of that array of Car objects it will solve the problem.

Class CarOperations{
public Arraylist carList = new carList();
public CarContainer(){
Car car = new Car(values as many);
carList.add(car);
//add other cars
}
public void addCar(Car aCar){
carList.add(aCar);
}
public void getCar(int index){
return (Car)carList.get(index);
}
public void getTotalNoOfCars(){
return carList.size();
}
}

You can also change this class add different methods like search the cars which are hired or something like that in this only class.
If you find anyother problem then let me know.
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Ben,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We'd like something other than a title for the displayed first name.

Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!