Results 1 to 3 of 3
Thread: OutPut Error :/
- 12-10-2010, 05:59 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
OutPut Error :/
When i run my code:
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.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; } }
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.
- 12-10-2010, 06:32 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
the name of the parameters are MilesDrove and AmtGallonsGas !!!!public Garage(int MilesDrove, int AmtGallonsGas)
{
this.milesDriven=milesDriven;
so write:
this.milesDriven=MilesDrove;
this.GallonsGas=AmtGallonsGas;
in your constructor! And remove the static modifiers ! (and read the namingconventions :) )
- 12-12-2010, 09:03 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 81
- Rep Power
- 0
Similar Threads
-
Two Class Program but No Output or Error
By H0AX in forum New To JavaReplies: 2Last Post: 12-09-2010, 06:27 AM -
how to get the resultset output into an output file
By renu in forum New To JavaReplies: 0Last Post: 09-30-2010, 08:16 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks