I am confused and stuck on how to find the max and minimum of my code in the array. I have to use Integer.MAX_Value and Integer.MIN_Value.
public class AnnualFuelUseTester
{
public static void main(String [] args)
{
int sMiles1, eMiles1, dist;
double price , gallons, mpg, GPM;
int counter = 0;
int total;
AnnualFuelUse[] annual = {new AnnualFuelUse(12700,12751,51,2.8),
new AnnualFuelUse(12802,12908,106,5.8),
new AnnualFuelUse(12908,13014,106,5.8)};
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int index = 0; index < annual.length; index++)
{
annual[index].calcDistance();
annual[index].calcMPG();
annual[index].calcTotal();
}
System.out.println(
"Fill up Start Miles End Miles Distance Gallons Used MPG Price Cost");
for(int index = 0; index < annual.length; index++)
{
System.out.printf(" %d %6d %5d %5d %.2f %.2f %.2f %.2f\n",
index + 1,
annual[index].getStart(),annual[index].getEnd(),
annual[index].getDist(), annual[index].getGal(),
annual[index].getMPG(), annual[index].getPrice(),
annual[index].getTotal());
}
System.out.println("Minimum: " + max);
}
}
And here my other class used together
public class AnnualFuelUse
{
private int sMiles1, eMiles1,dist;
private double price, gallons, mpg, total, GPM;
AnnualFuelUse(int s1, int e1, int distance, double gals)
{
sMiles1 = s1;
eMiles1 = e1;
dist = distance;
price = 1.83;
gallons = gals;
}
public void calcDistance()
{
dist = eMiles1 - sMiles1;
}
public double getPrice()
{
return price;
}
public void calcMPG()
{
mpg = dist / gallons;
}
public int getStart()
{
return sMiles1;
}
public int getEnd()
{
return eMiles1;
}
public int getDist()
{
return dist;
}
public double getMPG()
{
return mpg;
}
public double getGal()
{
return gallons;
}
public void calcTotal()
{
total = 1.83 * gallons;
}
public double getTotal()
{
return total;
}
}
I am asking for a brief run over on how to find the minimum and maximum is arrays. Especially specific numbers that are the same (eg 51,106 is my distances)
I'm afraid I have to guess what value you would like to minimize or maximize. So be it the distance, because you want to use an Integer value.
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int index = 0; index < annual.length; index++)
{
annual[index].calcDistance();
if (annual[index].getDist() < min) annual[index].getDist() = min;
if (annual[index].getDist() > max) annual[index].getDist() = max;
annual[index].calcMPG();
annual[index].calcTotal();
....
}
If you use a double value, be aware that Double.MIN_VALUE is the mnallest value a double could have. You would need -Double.MAX_VALUE instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.