Results 1 to 15 of 15

Thread: ArrayList Help

  1. #1
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default 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++);
    	}
    	
    }

  2. #2
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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?

  3. #3
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    it says to write a Garage class with one instance variable: an arraylist of autos which contains the gas and the miles

  4. #4
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by socboy6579 View Post
    it says to write a Garage class with one instance variable: an arraylist of autos which contains the gas and the miles
    Are you sure that doesnt mean that you have one ArrayList in your Garage class that contains car objects, which have instance variables miles and gas? It only makes sense that a garage holds cars. It makes no sense for a garage to have miles.

  5. #5
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    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?

  6. #6
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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.

  7. #7
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    ArrayList<Integer> car = new ArrayList<Integer>();
    could i use that for starters

  8. #8
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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 Cars
    Last edited by Bertstar; 12-09-2010 at 04:29 AM.

  9. #9
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    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>();
    	
    }

  10. #10
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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.

  11. #11
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    could you show me that? im just very confused

  12. #12
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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.

  13. #13
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default

    we are given a source code file to build off of.
    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." );
          }
        }
    }
    we are to create those various methods above^^

    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?

  14. #14
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Please do not make multiple threads for this same problem...

    Other thread

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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

  1. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  2. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  3. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  4. Regarding arrayList
    By kishan in forum Advanced Java
    Replies: 7
    Last Post: 08-07-2009, 12:48 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •