Results 1 to 4 of 4
Thread: Proper if Statements: Help
- 12-10-2010, 01:15 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Proper if Statements: Help
Would i need to use else if statements for this
When i try just to use regualr if statements the last 2 statements become "unreachable". Could this be solved by using if else?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"; }
- 12-10-2010, 01:26 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 12-10-2010, 01:33 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
thank you i figured it out.
Thank you. and one more question if you wuld be able to answer thisJava 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"; }
How would i call the averageMiles, GarageHolds, and the numGallons methods in the main method..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; } }
- 12-10-2010, 01:48 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Is this the proper way to save an object?
By ryuzog in forum New To JavaReplies: 6Last Post: 10-10-2010, 02:32 AM -
while and proper conditions for if
By Saletra in forum New To JavaReplies: 11Last Post: 08-25-2010, 10:37 AM -
proper use of IllegalArgumentException
By vendetta in forum New To JavaReplies: 1Last Post: 01-16-2010, 07:43 PM -
pls expalin me with any proper example
By javastuden in forum New To JavaReplies: 1Last Post: 11-05-2009, 10:35 AM -
Need help. Method won't returning proper value..
By zlwilly in forum New To JavaReplies: 2Last Post: 12-02-2008, 09:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks