java - How to use integers inside of an array as parameters for a method. (Tester) -


i calculate integer , assign array using method inside loop, next method in loop needs previous integer calculated parameter declared double fixed problem now need print result , have same problem, put in method parameters when printing because variable wiped after every loop in first loop.

this main method class :

public class annualfueltester {     public static void main(string args[]) {         //declaration of variables         int endmiles, startmiles;         double gallonsused, pricepergallon;          //initialization of array of objects         annualfueluse[] fillups = {                 new annualfueluse(45023, 45231, 10.00, 2.95),                 new annualfueluse(45231, 45480, 11.70, 2.99),                 new annualfueluse(45480, 45659, 9.30, 3.03),                 new annualfueluse(45659, 45961, 14.90, 3.05)         };          //call methods         (int index = 0; index < fillups.length; index++) {             double distance = fillups[index].calcdistance();             fillups[index].calcmpg(distance);              fillups[index].getstartmiles();             fillups[index].getendmiles();             fillups[index].getgallons();             fillups[index].totalcost(distance);         }          //print results         system.out.printf(" %15s %15s %15s %15s %15s %15s %15s %15s %15s", "fill up", "days", "start miles", "end miles", "distance", "gallons", "miles/gal", "gallons/miles", "price", "total cost\n");          (int index = 0; index < fillups.length; index++) {             system.out.printf("%15i %15i %15s %15s %15d %15d %15d %15d %15d", index, index, fillups[index].getstartmiles(), fillups[index].getendmiles(), fillups[index].calcdistance(), fillups[index].getgallons(), fillups[index].calcmpg(distance), fillups[index].totalcost(distance), "\n");         }     } }   

this class methods:

public class annualfueluse {     //private instance variables     private int myendmiles, mystartmiles;     private double mygallonsused, mypricepergallon;      annualfueluse(int sm, int em, double gu, double ppg) {          myendmiles = em;         mystartmiles = sm;         mygallonsused = gu;         mypricepergallon = ppg;     }      //distance driven     public double calcdistance() {         return myendmiles - mystartmiles;     }      //calculate miles per gallon     public double calcmpg(double distance) {         return distance / mygallonsused;     }      //calculate gallons per mile     public double calcgpm(double distance) {         return (distance / mygallonsused) / 100;     }      //calculate total cost     public double totalcost(double distance) {         return mypricepergallon * distance;     }      //getter start miles     public int getstartmiles() {         return mystartmiles;     }      //getter end miles     public int getendmiles() {         return myendmiles;     }      //getter gallons used     public double getgallons() {         return mygallonsused;     }      //getter price per gallon     public double getpricepergallon() {         return mypricepergallon;     } } 

the instructions program are

  1. if have not yet created 8.08 annual fuel use project in mod08 assignments folder, please now.
  2. be sure save copy of these instructions in mod08 documents folder.
  3. print copy notebook.
  4. read instructions before attempt assignment.
  5. create 2 classes called annualfuelusetester , annualfueluse in newly created project folder.
  6. use fill data have been collecting car (or family car) , calculate total distance, gallons used, , cost of gas.
  7. determine minimum , maximum values distance, miles per gallon , price. (recall integer class constancies min_value , max_value. double class has class constants of same name.)
  8. calculate annual projections distance, gallons used, miles per gallon, , cost based on data collected.

  9. each fill should considered object , program design should based on array of objects. use demo program in lesson model how create , process array of objects.

if expecting value calculated in .calcdistance() method, have method .calcdistance() return value (the 1 require). store in variable or directly pass second called function .clacmpg()

as returning value .calcdistance() can like

int dist = fillups[index].calcdistance();

and can use dist value in other method calls make, like

fillups[index].calcmpg(dist);

you can use 1 variable overwritten every time loop runs.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -