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

    Default adding objects into arrayLists

    Hi, i am having trouble adding car objects with a model name, gas miles, and miles drove to my ArrayList.

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Garage {
    
    	private static String model;
    	private static int milesDriven;
    	private static int GallonsGas;
    	
    	
    	public Garage(String ModelCar, int MilesDrove, int AmtGallonsGas)
    	
    	{
    		this.model=model;
    		this.milesDriven=milesDriven;
    		this.GallonsGas=GallonsGas;
    	}
    	
    	public String getModelCar()
    	{
    		String returnModelCar= model;
    		return returnModelCar;
    	}
    	
    	public int getMilesDrove()
    	{
    		int returnMilesDrove= milesDriven;
    		return returnMilesDrove;
    	}
    	
    	public int getAmtGallonsGas()
    	{
    		int returnAmtGallonsGas= GallonsGas;
    		return returnAmtGallonsGas;
    	}
    	
    	public static void auto(String model, int milesDriven, int gallonsGas) 
    	// TODO Auto-generated method stub
    	{
    		ArrayList<List> auto = new ArrayList<List>();
    		auto(model, milesDriven, GallonsGas);
    		auto.add("Saab", 5552, 37414);
    	}
    	
    }
    It wont let me use the .add method? For this i dont know why. If i am mixing sometihng up or doing something wrong please tell me.

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

    Default

    I told you how to do this yesterday in your other thread......

    I told you to make 2 classes a Car class and a Garage class. The Garage class should ONLY contain an ArrayList<Car> as its instance variable. The Car class should have the instance variables for the model of the car, the miles driven, and the gallons of gas.
    Last edited by Bertstar; 12-10-2010 at 01:38 AM.

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

    Default

    I know but am i anywhere close? is my array list format correct?

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

    Default

    Quote Originally Posted by socboy6579 View Post
    I know but am i anywhere close? is my array list format correct?
    Currently, you essentially have a class named Garage that acts as a Car. So please take my advice this time and create TWO classes a Garage class and a Car class. Your Garage class should have the ArrayList that contains all of the Cars in the Garage.

    And to answer your question no your ArrayList is not correct.... the stuff in the <> is the type that your ArrayList will contain. You currently have <List>. Do you want your ArrayList to hold List objects?

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

    Default

    just integers for the gas and the miles.. but i need the car object

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

    Default

    Yes, you need auto objects. In your other thread you say you are given the auto class, so you need to have your ArrayList hold autos since each auto has data about the gas and miles.

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

    Default

    should the car class have the variables such as gas and miles?

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

    Default

    Yes, but aren't your provided with the "auto" class as you provided in your other thread?

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    socboy, listen to Bertstar, go through your problem again and re-write your garage class, this time taking into account where those milesDriven and gasGallons variables will be initialised hint: your Car class methods maybe?

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

    Default

    yes i am given the auto class the one above^

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

    Default

    so far i changed it to this not that much but still:
    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Garage {
    
    	
    	
    	
    	public Garage(int MilesDrove, int AmtGallonsGas)
    	
    	{
    		
    	}
    	public class Car{
    		private int milesDriven;
    		private int GallonsGas;
    		
    	public Car(int MilesDrove, int GallonsGas)
    	{
    		this.milesDriven=milesDriven;
    		this.GallonsGas=GallonsGas;
    	}
    	
    	public int getMilesDrove()
    	{
    		int returnMilesDrove= milesDriven;
    		return returnMilesDrove;
    	}
    	
    	public int getAmtGallonsGas()
    	{
    		int returnAmtGallonsGas= GallonsGas;
    		return returnAmtGallonsGas;
    	}
    	
    	
    	
    }
    }

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    post two classes, 1. your car class and 2. your garage class (your garage class will hold car objects, in an arraylist)

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

    Default

    I dont have anything so far in my garage class. i only have code in my car class
    Java Code:
    public class Car{
    		private int milesDriven;
    		private int GallonsGas;
    		
    	public Car(int MilesDrove, int GallonsGas)
    	{
    		this.milesDriven=milesDriven;
    		this.GallonsGas=GallonsGas;
    	}
    	
    	public int getMilesDrove()
    	{
    		int returnMilesDrove= milesDriven;
    		return returnMilesDrove;
    	}
    	
    	public int getAmtGallonsGas()
    	{
    		int returnAmtGallonsGas= GallonsGas;
    		return returnAmtGallonsGas;
    	}
    	
    	
    	
    }

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    ok:
    A skeleton Garage class:
    Java Code:
    public class Garage{
    
    private ArrayList<Car> carList = new ArrayList<Car>();
    
    public Garage(Car newCar){
    
     carList.add(newCar);
    
    }
    
    public void addCarToGarage(Car newCar){
        carList.add(newCar);
    
    }
    
    
    }
    this should give you an overview
    Last edited by al_Marshy_1981; 12-10-2010 at 02:34 AM. Reason: void

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

    Default

    lol thanks for assisting me in this one al_Marshy

    Please listen to al_Marshy he pretty much just wrote the whole class for you.

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

    Default

    and i would put milesDriven and GallonsGas inside the garage class aswell? and thank you

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

    Default

    Does it make sense for a Garage to have how many miles a Garage has driven? Would a Garage have how much gas it has left in its gas tank?

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

    Default

    my stupidity. so my car class would have what i already have. correct?

  19. #19
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by socboy6579 View Post
    and i would put milesDriven and GallonsGas inside the garage class aswell? and thank you
    like Bertstar is saying why would you want these in the garage class when they are available to your car class?

    quick skeleton:
    from you garage class:
    Java Code:
    public int getMilesDriven(Car thisCar){
    Car carFound;
    // find thisCar in arraylist using appropiate arraylist methods and set carFound equal to it 
    return carFound.getMilesDrove();
    
    }

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

    Default

    i have my code
    Java Code:
    import java.util.ArrayList;
    
    
    public class Garage {
    
    	private static int milesDriven;
    	private static int GallonsGas;
    	
    	
    	public Garage(int MilesDrove, int AmtGallonsGas)
    	
    	{
    		
    		this.milesDriven=milesDriven;
    		this.GallonsGas=GallonsGas;
    	}
    	
    	public int getMilesDrove()
    	{
    		int returnMilesDrove= milesDriven;
    		return returnMilesDrove;
    	}
    	
    	public int getAmtGallonsGas()
    	{
    		int returnAmtGallonsGas= GallonsGas;
    		return returnAmtGallonsGas;
    	}
    	
    	public static void main(String[] args)
    	{
    		Garage auto1 = new Garage(5000, 2679);
    		Garage auto2 = new Garage(4325, 1931);
    		Garage auto3 = new Garage(2452, 978);
    		Garage auto4 = new Garage(6742, 2979);
    		Garage auto5 = new Garage(10000, 4503);
    		
    		ArrayList<Garage> myCars = new ArrayList<Garage>();
    		myCars.add(auto1);
    		myCars.add(auto2);
    		myCars.add(auto3);
    		myCars.add(auto4);
    		myCars.add(auto5);
    		
    		
    	}
    	
    	public static int averageMiles(ArrayList<Garage> myCars)
    	{
    		int sum= 0;
    		for(int i=0; i < myCars.size(); i++)
    		sum+= myCars.get(i).getMilesDrove();
    		return sum/myCars.size();
    		
    	}
    	public static String GarageHolds(ArrayList<Garage> myCars)
    	{
    		if (myCars.size() < 25);
    		return "below minimum";
    		
    		if (myCars.size() >= 25 && myCars.size() < 100);
    		return "normal load";
    
    		if (myCars.size() >= 100);
    		return "full";
    		
    	}
    	
    	public static int numGallons(ArrayList<Garage> myCars)
    	{
    		int sum=0;
    		for(int i=0; i < myCars.size(); i++)
    			sum+= myCars.get(i).getAmtGallonsGas();
    		return sum;
    	}
    	
    }
    My only question is why the last 2 if statements are unreachable? i dont know why i cant get them towork

Similar Threads

  1. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 AM
  2. Method for adding and drawing several objects
    By Pillow in forum New To Java
    Replies: 2
    Last Post: 09-08-2010, 11:24 PM
  3. Replies: 1
    Last Post: 05-04-2010, 11:00 AM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM

Posting Permissions

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