I am creating a game in Java and need to create a list of all of the rooms in it.
I have a 'Rooms' class that has the code for the room, i.e. room name, items in the room etc.
In the Room class I want to have a static ArrayList that has all of the room objects in the whole game in there. This is needed for a method I am working on.
I have created an ArrayList field:
private static ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();
In the initialisation of each instance of Rooms, I wish to add that instance to the listOfRooms ArrayList.
I assume you start with listOfRooms.add(), but what would you actually put in the parameter to add the current object to the list of Rooms objects?
You'd add this to the list; this being a reference to the current object (a Room in this case) being worked on:
listOfRooms.add(this);
Note though, having listOfRooms as a static member of the class is a bad idea. With this setup, you can only ever have one list, and anyone can alter the list however they want since it's public.
It would likely be much better to create something like a Hotel class and make it a member of that:
class Hotel {
ArrayList<Rooms> listOfRooms = new ArrayList<Rooms>();
}
Now you can at least have multiple hotels if necessary with separate lists of rooms, and to modify the rooms, code would at least require a reference to the hotel.
Adding to what Carcigenicate said, it's always better to have more abstraction in object oriented programming.
For example, Hotel object has a Floor ArrayList in it. Each Floor object contains ArrayList of Room and so on.
Related
for my exams in Java in a couple of weeks i go trough any old exams i get my fingers on. But there is one task i can't get my head arround. I need a solution for this.
You should creat a abstract Class called Plant(just with Attributes for height and weight).
There are three Sub Classes (Tomato, Bellpeper and Onions)
Then you need a Class called Greenhouse, in this Greenhouse is room for Excactly 30 Plants in total. It dosent matter what kind of Plants.
If you want to store more then 30 Plants then it should drop a Exception.
i know i need:
a Final attribute for the 30 plants.
Array.Add to get the Objects in my Array.
and a Method that calculated 30 - the length of my Array List.
but i dont know how to use this. There a lot of Tutorials for stuff like this in the same class but not from another class.
I hope someone can help me.
Greetings from Germany
You just need a method in your Greenhouse class. for example
public void addPlant(Plant p) throws Exception //Placeholder exception
Where you take the Plant as an argument, check if you have room in the Greenhouse arraylist, and either add it, or throw the exception. It doesn't matter which class creates the Plant, it will be passed to the Greenhouse class via this method.
I was hoping someone could tell me if I'm even going about this the right way, or if what I'm doing is even possible.
My end goal is to create a program where you can create cabins, create campers, and assign them to a cabin.
What I thought of was allowing the user to create a new ArrayList by having a method called that does such. I would create a Camper class where it has the variables such as name, age, gender, etc...
Then I would write a method that looked something like this....
public static void userCreatesList(){
ArrayList<Camper> cabin = new ArrayList<Camper>();
}
The problem is, the method works when I add items to the list and have it print out as each time it prints a different list when calling the method, however, what I cannot figure out how to do is to call a previous list again as every list here will have the same variable or object name.
So, if I created say three cabin lists all together by calling this method three times, how would I assign a person to a particular cabin if they all end up having the same variable name?
It has to be a program where the user can create the list and I didn't have to declare a number of lists with a different variable name, because each camp may have a different number of cabins, and they have to be able to make their own list?
If what I'm doing is not possible and I need to be using another type of object besides a list to do this, please just tell me and I'll research how to use that object.
I've searched all over how to create a new group, a new set, etc.. and I cannot find anything relevant to what I'm trying to do that can explain this.
I want them to be able to give that group a name, such as "Red Cabin", "Blue Cabin", "Cabin 1", "Cabin 12", etc....
For this I would recommend to use Map, with group name as a key and ArrayList as value.
private static Map<String, ArrayList<Camper>> cabinMap = new HashMap<String, ArrayList<Camper>>();
public static void userCreatesList(String groupName){
ArrayList<Camper> cabin = new ArrayList<Camper>();
cabinMap.put(groupName, cabin);
}
Later you can access to the certain group from this map by name and add members to it.
I'm trying to make a game similar to Risk where each territory on the map is an object of a class I made, Territory. In order to create the game map I would like each Territory object to be linked to the other territories that would be adjacent on the game board. Is there a way in Java to create some kind of data structure that can store all the objects and with links to their "neighbors"? Or is there a way to have objects store references to other objects like you could with pointers in C?
You can either have the neighbours of every instance of Territory stored in the instance like this:
public class Territory{
private ArrayList<Territory> neighbours;
}
or map all territorys to their neighbours like this:
HashMap<Territory , List<Territory>> neighbours;
I got an assignment in which I have to make a phone book using a link list.
In this list I got to be able to add an entry. The entry must have a person first name,last name and phone number.
I have to be able to delete the first,last, and phone number of the entries.
How can I do this I am think of creating a class named entry with string for first,last,and phone number.
And when the user decides to create a new Record put a new Object entry with these fields. Into the link list.
The problem is how can I create a new Object and name it when the user want to put it on the list.
I cannot keep using the same name for an object over and over or can I?
You can't.
Suppose you have a class, Person, with properties as you suggested. directory is an object of type LinkedList<Person>. The correct way to do what you are trying to do is to make Person immutable. Every time you want to add a Person, you write statements such as directory.add(new Person("George", "Washington", "1776")) (the constructor initializes final Strings.
You must make new objects because LinkedList only stores references to objects, not copies of them.
Really, though, you can instead of Person use an associative array, for example mapping enum types for properties to strings.
Make a list of some kind (array, ArrayList, whatever floats your boat) and store it there using new Record(...).
Making a class for keeping the data is a good way to solve the problem.
I'm programming in java for a class project.
In my project I have airplanes, airports and passengers.
The passenger destination airport is randomly created, but then I have to add it to a List with passengers for that destination.
As long as the airports are read from a file thus they can vary, how can I create Lists according to these airports?
What I want to do is something like:
List<Passenger> passengersToJFK = new ArrayList<Passenger>();
.
.
.
if(passenger.destination == "JFK"){
passengersToJFK.add(passenger);
}
The problem is that as I've said, the number and name of airports can vary, so how can I do a general expression that creates Lists according to the Airports File and then adds passengers to those Lists according to the passenger destination airport?
I can get the number of Airports read from the file and create the same number of Lists, but then how do I give different names to this Lists?
Thanks in advance
You can keep a registry of the associations between a destination or airport and a list of passengers with a Map, in a particular class that centers this passengers management.
Map<String,List<Passenger>> flights = new HashMap<String,List<Passenger>>();
Then, whenever you want to add a new destination you put a new empty list and
public void addDestination(String newDestination) {
flights.put(newDestination, new ArrayList<Passenger>());
}
When you want to add a passenger, you obtain the passenger list based on the destination represented by a String.
public void addPassengerToDestination(String destination, Passenger passenger) {
if(flights.containsKey(destination))
flights.get(destination).add(passenger);
}
I suggest you dig a little deeper into some particular multi-purpose Java classes, such as Lists, Maps and Sets.
I would probably create a Map of airports with airport name as the key and a List of passengers as the value.
e.g.
Map<String, List<String>> airports = new HashMap<String, List<String>>();
airports.put("JFK", passengersToJFK);
You sound like you're thinking too much in terms of primitives, Strings, and collections and not enough in terms of objects.
Java's an object-oriented language; start thinking about Objects and encapsulation.
You've got a good start with your Passenger class. Keep going with Airport.
Do you add Passengers to Airport? Nope, I think they belong to a Flight.
Do a little thinking about your problem before you write more code.
You shouldn't focus on giving the actual variables of list objects unique names, but instead, create a map from String (destination id) to List (passengers heading to that destination), and add lists on the fly to that map, linking each new list to its relevant destination. Update the lists in that map as needed.
The best way to do it is to create objects for all three.
You might have an Airport object that looks like this:
class Airport{
String name;
List Airplane airplanes;
}
then you would have an airplane that looked like this:
class Airplane{
String name; // ?? or bodyType? or whatever else you need
List Passenger passengers;
}
In this way you compose your objects from each other in a way that ends up being much easier to understand and deal with.
Note that I'm leaving off methods, like Airport probably has a method like "addAirplane" to add another airplane, and the airplane object has an addPassenger method...