Results 1 to 3 of 3

Thread: OutPut Error :/

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

    Default OutPut Error :/

    When i run 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 MilesDrove)
    	{
    		return milesDriven;
    	}
    	
    	public int getAmtGallonsGas(int AmtGallonsGas)
    	{
    		return GallonsGas;
    	}
    	
    	public static void main(String[] args)
    	{
    		Garage auto1 = new Garage(5000, 511);
    		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);
    		
    		System.out.println(averageMiles(myCars));
    		System.out.println(GarageHolds(myCars));
    		System.out.println(numGallons(myCars));
    	}
    	
    	public static int averageMiles(ArrayList<Garage> myCars)
    	{
    		int sum= 0;
    		for(int i=0; i < myCars.size(); i++)
    			sum+= myCars.get(i).milesDriven;
    		
    		return sum/myCars.size();
    	}
    	
    	public static String GarageHolds(ArrayList<Garage> myCars)
    	{
    		if (myCars.size() < 25){
    		return "below minimum";
    		
    		} else if(myCars.size() >= 25 && myCars.size() <100){
    		return "normal load";
    		
    		} else 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).GallonsGas;;
    		}
    		return sum;
    		
    	}
    	
    }
    my desired out that i need to get is the average of the miles in the array list, the correct return statement from the if statements, and the sum of all the gallons used in all cars in the array list.

    When i run my program my output is
    0
    below minimum
    0


    The second method output is correct, but i am just wondering why my output for the average miles method and the total gallons method is 0. Any reasons that are easy to see because i cant find them.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    public Garage(int MilesDrove, int AmtGallonsGas)
    {
    this.milesDriven=milesDriven;
    the name of the parameters are MilesDrove and AmtGallonsGas !!!!

    so write:

    this.milesDriven=MilesDrove;
    this.GallonsGas=AmtGallonsGas;

    in your constructor! And remove the static modifiers ! (and read the namingconventions :) )

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

Similar Threads

  1. Two Class Program but No Output or Error
    By H0AX in forum New To Java
    Replies: 2
    Last Post: 12-09-2010, 06:27 AM
  2. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 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
  •