Results 1 to 15 of 15
Thread: ArrayList Help
- 12-09-2010, 03:42 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
ArrayList Help
I am just asking for a little help if possible from the forums. In my program i am asked:
for a method returning the average number of miles of all cars in garage
a method returning"full" if the garage has 100 cars of more, "below minimum" if the garage has fewer than 25 cars, and "normal load" if the garage has between 25 and 100 cars in it
for a method returning the total number of gallons of gas used by all cars in garage
I know that i have to create an arraylist as a garage to hold cars with gallons of gas and miles. i have the miles and gas sorted out, but i am just stuck on how to put and relate the gallons of gas and the miles in the arrayList. If i could get any pointers, that would be appreciated. Here is my code
Java Code:import java.util.ArrayList; //allows usage of ArrayLists public class Garage { private int NumMiles; private int Gas; public Garage (int Miles, int GalsGas) { NumMiles = Miles; Gas = GalsGas; } public int getMiles() { int returnMiles = NumMiles; return returnMiles; } public int getGalsGas() { int returnGalsGas = Gas; return returnGalsGas; } ArrayList<Integer> garage = new ArrayList<Integer>(); { for(int i =0; i <garage.size(); i++); } }
- 12-09-2010, 04:05 AM #2
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
Why wouldn't the Garage class simply contain all of the cars? why does it contain miles and gas? Shouldn't miles and gas be instance variables in a car class?
- 12-09-2010, 04:12 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
it says to write a Garage class with one instance variable: an arraylist of autos which contains the gas and the miles
- 12-09-2010, 04:16 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
- 12-09-2010, 04:17 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Yes i need to have an arrraylist in my garage class containing car objects which instance varaibles are miles and gas. am i even clear on the right path?
- 12-09-2010, 04:22 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
you are kind of on the right path since your Garage class contains the data for a Car. So you need to create a Garage class that has an instance variable that is an ArrayList of cars.
- 12-09-2010, 04:24 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
ArrayList<Integer> car = new ArrayList<Integer>();
could i use that for starters
- 12-09-2010, 04:26 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
No, you need to essentially rename your Garage class to Car and then create a Garage class that has an variable like this
ArrayList<Car> allCars = new ArrayList<Car>();
the ArrayList you are trying to create would contain Integers not CarsLast edited by Bertstar; 12-09-2010 at 04:29 AM.
- 12-09-2010, 04:40 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
is this at all correct
Java Code:import java.util.ArrayList; //allows usage of ArrayLists public class Car { private int NumMiles; private int Gas; public Car (int Miles, int GalsGas) { NumMiles = Miles; Gas = GalsGas; } public int getMiles() { int returnMiles = NumMiles; return returnMiles; } public int getGalsGas() { int returnGalsGas = Gas; return returnGalsGas; } ArrayList<Car> allCars = new ArrayList<Car>(); }
- 12-09-2010, 04:52 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
You dont need the ArrayList of Cars in the Car class. You should create a Garage class that has an ArrayList of Cars as its instance variable.
- 12-09-2010, 05:02 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
could you show me that? im just very confused
- 12-09-2010, 05:10 AM #12
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
I am not going to write the class for you, but here is what you need to do.
Create a new class called Garage that has one instance variable which is an ArrayList of Cars. You then need to add methods that will allow you to add Cars to your Garage. You will also probably need a method to remove Cars from your Garage. Keep in mind that the Cars in your Garage are held in your instance variable, which like I stated previously is an ArrayList of Cars. So, the implementation of your Garage's addCar and removeCar methods should be pretty simple.
- 12-09-2010, 11:29 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
we are given a source code file to build off of.
we are to create those various methods above^^Java Code:/* Auto class, Version 3 Anderson, Franceschi */ public class Auto { // instance variables private String model; // model of auto private int milesDriven; // number of miles driven private double gallonsOfGas; // number of gallons of gas // Default constructor: // initializes model to "unknown"; // milesDriven is auto-initialized to 0 // and gallonsOfGas to 0.0 public Auto( ) { model = "unknown"; } // Overloaded constructor: // allows client to set beginning values for // model, milesDriven, and gallonsOfGas. public Auto( String startModel, int startMilesDriven, double startGallonsOfGas ) { model = startModel; setMilesDriven( startMilesDriven ); setGallonsOfGas( startGallonsOfGas ); } // Accessor method: // returns current value of model public String getModel( ) { return model; } // Accessor method: // returns current value of milesDriven public int getMilesDriven( ) { return milesDriven; } // Accessor method: // returns current value of gallonsOfGas public double getGallonsOfGas( ) { return gallonsOfGas; } // Mutator method: // allows client to set model public void setModel( String newModel ) { model = newModel; } // Mutator method: // allows client to set value of milesDriven; // prints an error message if new value is less than 0 public void setMilesDriven( int newMilesDriven ) { if ( newMilesDriven >= 0 ) milesDriven = newMilesDriven; else { System.err.println( "Miles driven cannot be negative." ); System.err.println( "Value not changed." ); } } // Mutator method: // allows client to set value of gallonsOfGas; // prints an error message if new value is less than 0.0 public void setGallonsOfGas( double newGallonsOfGas ) { if ( newGallonsOfGas >= 0.0 ) gallonsOfGas = newGallonsOfGas; else { System.err.println( "Gallons of gas cannot be negative." ); System.err.println( "Value not changed." ); } } }
If i were to create an ArrayList could if start off as
ArrayList<Integer> autos = new ArrayList();
Any pointers on how to add gas and miles to the arraylist?
- 12-10-2010, 01:51 AM #14
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
Please do not make multiple threads for this same problem...
Other thread
- 12-10-2010, 01:53 AM #15
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You don't want ArrayList<Integer> in your Garage class, you want ArrayList<Auto>.
I have to say this is a very poorly-designed exercise. It makes no sense to have a setModel() method -- is somebody going to change a Corolla into a Hummer2? setMilesDriven() makes some sense, as it can at least reflect a reading at some point in time -- but merely checking that it's a positive value is stupid. It should make sure the value is the same or higher than the previous reading, or else somebody is committing fraud. And setGallonsOfGas() makes no sense, as you can only add gasoline to a car's tank -- you can't just arbitrarily set a new value for how much it has used. But that's just me ranting.
You want an ArrayList<Auto>, and then your methods will all iterate through your list and sum up totals and compute averages. Basic stuff that's probably well covered in your text.
-Gary-
Similar Threads
-
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
A private static ArrayList hold an object and static getter ArrayList
By Louis in forum New To JavaReplies: 2Last Post: 11-16-2010, 05:51 PM -
Creating an ArrayList from an ArrayList
By Klahking in forum New To JavaReplies: 17Last Post: 09-09-2010, 03:34 PM -
Regarding arrayList
By kishan in forum Advanced JavaReplies: 7Last Post: 08-07-2009, 12:48 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks