java - 1D array frequency counter for other methods -


i wanted call method prompt user enter miles driven, gallons used, calculate miles per gallon, display how many miles per gallon type of car got on trip. wanted method passes “1” add frequency counter each type of car later on. (if car honda, add “1” arrayname[1], if car toyota, add “1” arrayname[2], etc.).

     int[] mpglist = new int[5]; // 5 because there 4 more car types      mpglist[0] =       do{         prompt = double.parsedouble(joptionpane.showinputdialog(null, "enter"             + "\n"             + "1 honda"));          if (prompt == 1)         {                  forhonda();           }; 

......

 public static void forhonda(){     double miles, gallons, mpg;      miles = double.parsedouble(joptionpane.showinputdialog(null,"enter miles driven "));         if (miles <= -1){             joptionpane.showmessagedialog(null,"input negative"                     + "\n"                     + "try again");         miles = double.parsedouble(joptionpane.showinputdialog(null,"enter miles driven "));          }     gallons = double.parsedouble(joptionpane.showinputdialog(null,"enter gallons used "));         if (gallons <= -1){             joptionpane.showmessagedialog(null,"input negative"                     + "\n"                     + "try again");         gallons = double.parsedouble(joptionpane.showinputdialog(null,"enter gallons used "));          }     mpg = (miles/gallons);     if (gallons == 0){         joptionpane.showmessagedialog(null, "division zero"                 + "\n"                 + "try again");     miles = double.parsedouble(joptionpane.showinputdialog(null,"enter miles driven "));     gallons = double.parsedouble(joptionpane.showinputdialog(null,"enter gallons used "));     mpg = (miles/gallons);     }     joptionpane.showmessagedialog(null,string.format("mpg honda: %.0f"             + "\n", mpg)); 

......

    public static void counter(int x[]){     for(int counter = 0; counter< x.length; counter++)         x[counter]+=1; } 

this kind of idea going for, got stuck @ how utilize array freq counter

i'm not sure why want use arrays , primitive data types, let's assume not requirement (you're writing java code after all). here's how go solving problem of keeping track of fuel consumption of multiple car types.

so have pre-defined list of car types need displayed , somehow accessed integer number. let's create enum that:

public enum cartype {      honda(1, "honda"),      toyota(2, "toyota"),      alfa(3, "alfa romeo")     // ...     ;      private int id = 0;     private string displayname;      public static cartype forid(int id) {         (cartype type : cartype.values()) {             if (type.id == id) {                 return type;             }         }         throw new illegalargumentexception("no car type number " + id);     }      private cartype(int id, string displayname) {         this.id = id;         this.displayname = displayname;     }      public string getdisplayname() {         return displayname;     }      public int getid() {         return id;     }  } 

you want keep track of fuel consumption, possibly total number of miles driven, total distance traveled, number of trips , mpg:

public class consumption {      private double miles = 0;     private double gallons = 0;     private double mpg = 0;     private int numberoftrips = 0;      public void addtrip(double miles, double gallons) throws illegalargumentexception {         if (miles > 0 && gallons > 0) {             this.miles += miles;             this.gallons += gallons;             numberoftrips++;             mpg = this.miles / this.gallons;         } else {             throw new illegalargumentexception("both miles , gallons have greater zero");         }     }      public double getmiles() {         return miles;     }      public double getgallons() {         return gallons;     }      public double getmpg() {         return mpg;     }      public int getnumberoftrips() {         return numberoftrips;     }  } 

you don't have declare throw illegalargumentexception since runtimeexception, it's nice caller know can happen , can add javadoc block describe, under circumstances does.

you want able keep track of fuel consumption of multiple vehicle types:

import java.util.hashmap;  public class consumptionmanager {     private hashmap<cartype, consumption> data = new hashmap<>();      public consumption addtripdata(cartype type, double miles, double gallons) throws illegalargumentexception {         if (type == null) {             throw new illegalargumentexception("car type cannot null");         }         consumption consumption = data.get(type);         if (consumption == null) {             consumption = new consumption();             data.put(type, consumption);         }         consumption.addtrip(miles, gallons);          return consumption;     }      public consumption getconsumption(cartype type) throws illegalargumentexception {         if (type == null) {             throw new illegalargumentexception("car type cannot null");         }         return data.get(type);     }  } 

now can dynamically build ui using cartype enum this:

    (cartype type : cartype.values()) {         // build ui, e.g. on console like:         system.out.println(string.format("%d) %s", type.getid(), type.getdisplayname()));     } 

then after collecting type's id, miles , gallons used on trip add , possibly display current state:

    // create instance of consumptionmanager somewhere, possibly in start-up code:      // consumptionmanager mgr=new consumptionmanager();     try {         consumption consumption=mgr.addtripdata(cartype.forid(id), miles, gallons);         // display mpg/number of trips/etc, e.g. on console         system.out.println(string.format("average range after %d trips: %f", consumption.getnumberoftrips(),consumption.getmpg()));     } catch (exception e) {         // display error user, e.g. on console         system.out.println(e.getmessage());     } 

all have in order add car type add cartype enum , you're done. don't have magic numbers number of types support, respective ids, etc on code, in places need know them.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -