Advertisement
Guest User

error with calculating annual for MPG average?

a guest
Jan 3rd, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. /**
  2.  * This class instantiates car objects with ten private instance variables.
  3.  * It contains eight mutator methods with return values for mi/gal, distance (mi), gal/mi,
  4.  * total cost; minimum/maximum, total, and annual values for these variables within a car object.
  5.  *
  6.  * @author A. Mackey
  7.  * @version 01/02/14
  8.  */
  9. public class AnnualFuelUse
  10. {
  11.     //private instance variables
  12.     private double gallonsUsed, pricePerGallon, startingMiles, endingMiles;
  13.  
  14.     //constructor with two parameters
  15.     AnnualFuelUse(int FillUpNumber, double eMiles, double sMiles, double galsUsed, double pricePerGal)
  16.     {
  17.         startingMiles = sMiles;
  18.         endingMiles = eMiles;
  19.         gallonsUsed = galsUsed;
  20.         pricePerGallon = pricePerGal;
  21.     }
  22.  
  23.     //getter method to return the value of distance traveled by the object on one trip
  24.     public double getDistance()
  25.     {
  26.         return endingMiles - startingMiles;
  27.     }
  28.  
  29.     //getter method to return the value of the miles per gallon of the object on one trip
  30.     public double getMPG()
  31.     {
  32.         return getDistance() / gallonsUsed;
  33.     }
  34.  
  35.     //getter method to return the value of the gallons per mile of the object on one trip
  36.     public double getGPM()
  37.     {
  38.         return gallonsUsed / getDistance();
  39.     }
  40.  
  41.     //getter method to return the value of the total gasoline purchases on one trip
  42.     public double getTotalCost()
  43.     {
  44.         return pricePerGallon * gallonsUsed;
  45.     }
  46.  
  47.     //mutator method which calculates the minimum value of a given array
  48.     public static double min(double minMaxVariable[])
  49.     {
  50.         Double minVariable = Double.MAX_VALUE;
  51.         for(int i = 0; i < minMaxVariable.length; i++)
  52.         {
  53.             if (minMaxVariable[i] < minVariable)
  54.             {
  55.                 minVariable = minMaxVariable[i];
  56.             }
  57.         }
  58.         return minVariable;
  59.     }
  60.  
  61.     //mutator method which calculates the minimum value of a given array
  62.     public static double max(double minMaxVariable[])
  63.     {
  64.         Double maxVariable = Double.MIN_VALUE;
  65.         for(int i = 0; i < minMaxVariable.length; i++)
  66.         {
  67.             if(minMaxVariable[i] > maxVariable)
  68.             {
  69.                 maxVariable = minMaxVariable[i];
  70.             }
  71.         }
  72.         return maxVariable;
  73.     }
  74.  
  75.     //mutator method which calculates the total value of a given array
  76.     public static double totalOf(double totalVariable[])
  77.     {
  78.         double totalValue = 0;
  79.         for(double newValue : totalVariable)
  80.         {
  81.             totalValue += newValue;
  82.         }
  83.         return totalValue;
  84.     }
  85.    
  86.     //mutator method which calculates the annual value of a given array
  87.     public static double annualProjection(double days[], double annualVariable)
  88.     {
  89.         double totalValue = 0;
  90.         for(double newValue : days)
  91.         {
  92.             totalValue += newValue;
  93.         }
  94.         return 365.25 / (totalValue - 1) * annualVariable;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement