# Thread: Calculating min and max in an array list

1. Member
Join Date
Dec 2009
Posts
22
Rep Power
0

## Calculating min and max in an array list

I have everything done except finding maximum and minimum values. In this (tester) file, i can't figure out where to do that maximum and minimum for,if loop. Its bolded where it is right now but let me know where it should be. Thank you very much!

Java Code:
/**
* This program uses classes and objects between different java files.
*
*
* Jamison Hyman
* 1/15/10
*/

public class AnnualFuelUseTester {

public static void main(String[] args)

{

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

AnnualFuelUse car1 = new AnnualFuelUse(1, 81823, 82243, 15.3, 2.61);	// car object

AnnualFuelUse[] AnnualFuelUse = {new AnnualFuelUse(1, 81823, 82243, 15.3, 2.61),
new AnnualFuelUse(6, 82243, 82713, 16.0, 2.55),
new AnnualFuelUse(13, 82713, 83341, 14.7, 2.64),
new AnnualFuelUse(19, 83341, 83920, 15.1, 2.60)};

for(int index = 0; index < AnnualFuelUse.length; index++)
{
AnnualFuelUse[index].getmyStartMiles();
AnnualFuelUse[index].getmyEndMiles();
AnnualFuelUse[index].getmyGallons();
AnnualFuelUse[index].getmyDistance();
AnnualFuelUse[index].getmyMpg();
AnnualFuelUse[index].getmypricePerG();
AnnualFuelUse[index].getmyTotalPrice();
AnnualFuelUse[index].getmyGallonsPerMile();
AnnualFuelUse[index].getmyDay();

[B]            if (AnnualFuelUse[index].getmyDistance < min){
min = AnnualFuelUse[index].getmyDistance;[/B]
}
}

System.out.printf("%55s\n\n","Gas Mileage Calculations");
System.out.printf("%s%8s%13s%12s%12s%15s%10s%13s%11s\n","Fill Up","Days","Start Miles","End Miles","Distance","Gallons Used","MPG","Price","Cost");

int count = 1;

for(int index = 0; index < AnnualFuelUse.length; index++)
{
System.out.printf("%s%12s%13s%13s%12.3s%13.5s%15.5s%12.4s%11.5s\n",count,
AnnualFuelUse[index].getmyDay(),AnnualFuelUse[index].getmyStartMiles(),
AnnualFuelUse[index].getmyEndMiles(), AnnualFuelUse[index].getmyDistance(),
AnnualFuelUse[index].getmyGallons(), AnnualFuelUse[index].getmyMpg(),
AnnualFuelUse[index].getmypricePerG(),AnnualFuelUse[index].getmyTotalPrice());

count++;
}
}

}

and the other file

Java Code:
/**
* This program calculates and displays gas mileage calculations
* using methods and Tester files.
*
*
* Jamison Hyman
* 1/7/10
*/

public class AnnualFuelUse {

private double myStartMiles1;
private double myEndMiles1;
private double myGallons1;
private String myMake1;
private double myDistance1;			// variable names to be assigned later
private double myMpg1;
private double mypricePerG1;
private double myTotalPrice1;
private double myGallonsPerMile1;
private int myDay;

AnnualFuelUse(){	// default
}

public AnnualFuelUse(int day ,double startMiles, double endMiles, double gallons,double pricePerG){

myStartMiles1 = startMiles;
myEndMiles1 = endMiles;
myGallons1 = gallons;
myDistance1 = myEndMiles1 - myStartMiles1;		// variables assigned
myMpg1 = myDistance1/myGallons1;
mypricePerG1 = pricePerG;
myTotalPrice1 = pricePerG * myGallons1;
myGallonsPerMile1 = myGallons1/myDistance1;
myDay = day;
}

public double getmyStartMiles(){
return myStartMiles1;
}

public double getmyEndMiles(){
return myEndMiles1;
}

public double getmyGallons(){
return myGallons1;
}

public double getmyDistance(){
return myDistance1;
}										// methods to return the values for printing later

public double getmyMpg(){
return myMpg1;
}

public double getmypricePerG(){
return mypricePerG1;
}

public double getmyTotalPrice(){
return myTotalPrice1;
}

public double getmyGallonsPerMile(){
return myGallonsPerMile1;
}

public int getmyDay(){
return myDay;
}
}

2. Senior Member
Join Date
Jun 2008
Posts
2,568
Rep Power
15
Sort it and take the first and last values.

3. Member
Join Date
Dec 2009
Posts
22
Rep Power
0
Thank you I figured it out. I was lacking parentheses.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•