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

    Default Proper if Statements: Help

    Would i need to use else if statements for this
    Java Code:
    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";
    	}
    When i try just to use regualr if statements the last 2 statements become "unreachable". Could this be solved by using if else?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by socboy6579 View Post
    Java Code:
    		if (myCars.size() < 25);
    Remove the trailing semicolon(s) (also on the other lines with if-clauses).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

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

    Default

    thank you i figured it out.
    Java Code:
    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";
    	}
    Thank you. and one more question if you wuld be able to answer this
    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);
    		
    		System.out.println(myCars.size());
    	}
    	
    	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";
    		
    		} 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).getAmtGallonsGas();
    		return sum;
    	}
    }
    How would i call the averageMiles, GarageHolds, and the numGallons methods in the main method..

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by socboy6579 View Post
    How would i call the averageMiles, GarageHolds, and the numGallons methods in the main method..
    System.out.println(averageMiles(myCars))?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Is this the proper way to save an object?
    By ryuzog in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 02:32 AM
  2. while and proper conditions for if
    By Saletra in forum New To Java
    Replies: 11
    Last Post: 08-25-2010, 10:37 AM
  3. proper use of IllegalArgumentException
    By vendetta in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 08:43 PM
  4. pls expalin me with any proper example
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-05-2009, 11:35 AM
  5. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 10:44 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
  •