当我编译 AnnualFuelUseTester 时,我收到一条错误消息,提示“找不到符号 - 方法 findMaxDistance(AnnualFuelUse[])”,而实际上我确实创建了一个 findMaxDistance 静态方法。代码分为两个类,如下所示
public class AnnualFuelUseTester
{
// main method
public static void main(String[ ] args) // main method
{
//create 4 new AnnualFuelUseTester objects
AnnualFuelUse[] car1 = {new AnnualFuelUse(1, 10876, 11665, 29.00, 1.99), // object data for the first fill up
new AnnualFuelUse(13, 11665, 12399, 27.60, 1.97), // object data for the second fill up
new AnnualFuelUse(28, 12399, 13155, 26.99, 2.05), // object data for the thrid fill up
new AnnualFuelUse(40, 13155, 13946, 29.47, 2.03)}; // object data for the fourth fill up
//call methods
for(int index = 0; index < car1.length; index++) // calls the follwing methods for each car1 object
{
car1[index].calcDistance(); // call method to calculate the distance
car1[index].calcMPG(); // call method to calculate the MPG
car1[index].totalCost(); // call method to calculate the total cost
}
// print the data
System.out.printf("%7s%7s%14s%12s%11s%15s%7s%8s%8s\n",
"Fillup", "Days", "Start Miles", "End Miles", "Distance", "Gallons Used", "MPG", "Price", "Cost"); // print the headers
for(int index = 0; index < car1.length; index++)
{
System.out.printf(" %-4s%6d%12d%13d%11d%9d%14.2f%10.1f%9.2f%9.2f\n",
car1[index].getDays(), car1[index].getStartMiles(), car1[index].getEndMiles(), car1[index].getDistance(),
car1[index].getGallonsUsed(), car1[index].getMilesPerGallon(), car1[index].getPricePerGallon(), car1[index].getTotalPrice()); // prints and formates car data
}
System.out.printf("%8s%36d%24.1f%9.2f\n", "Minimum", findMinDistance(car1), findMinMPG(car1), findMinPrice(car1)); // prints and formats the mimimun data
System.out.printf("%8s%36d%24.1f%9.2f\n", "Maximun", findMaxDistance(car1), findMaxMPG(car1), findMaxPrice(car1)); // prints and formats the mimimun data
} //end of main method
}
和
public class AnnualFuelUse
{
// variable declaration and initialization
private int myDays, myStartMiles, myEndMiles, myDistance;
private double myGallonsUsed, myPricePerGallon, myMilesPerGallon, myTotalPrice;
// default constructor
AnnualFuelUse()
{
}
// constructor that accepts all car descriptors
AnnualFuelUse(int days1, int startMiles1, int endMiles1, double gallonsUsed1, double pricePerGallon1)
{
myDays = days1; // creates a parameter to use for car type
myEndMiles = endMiles1; // creates a parameter to use for end miles
myStartMiles = startMiles1; // creates a parameter to use for start miles
myGallonsUsed = gallonsUsed1; // creates a parameter to use for galons used
myPricePerGallon = pricePerGallon1; // creates a parameter to use for price per gallon
myDistance = 0; // creates a parameter to use for distance
myMilesPerGallon = 0.0; // creates a parameter to use for mpg
myTotalPrice = 0.0; // creates a parameter to use for total price
}
// calculate the Distance driven
public void calcDistance() // method returns a interger distance
{
myDistance = myEndMiles - myStartMiles; // calculates and returns the distance to the main method
}
// calculate the miles per gallon
public void calcMPG() // method returns a double MPG and accepts a interger distance
{
myMilesPerGallon = (double)myDistance / myGallonsUsed; // calculates and returns the mpg to the main method
}
// calcualtes the total cost
public void totalCost() // method that returns a double cost
{
myTotalPrice = myGallonsUsed * myPricePerGallon; // calculates the price per gallons and returns it to the main method
}
// method to return myDays
public int getDays()
{
return myDays; // returns myDays to the main method
}
// method to return myEndMiles
public int getEndMiles()
{
return myEndMiles; // returns myEndMiles to the main method
}
// method to return myStartMiles
public int getStartMiles()
{
return myStartMiles; // returns myStartMiles to the main method
}
// method to return myGallonsUsed
public double getGallonsUsed()
{
return myGallonsUsed; // returns myGallonsUsed to the main method
}
// method to return myPricePerGallon
public double getPricePerGallon()
{
return myPricePerGallon; // returns myPricePerGallon to the main method
}
// method to return myDistance
public int getDistance()
{
return myDistance; // returns myDistance to the main method
}
// method to return myMilesPerGallon
public double getMilesPerGallon()
{
return myMilesPerGallon; // returns myMilesPerGallon to the main method
}
// method to return myTotalPrice
public double getTotalPrice()
{
return myTotalPrice; // returns myTotalPrice to the main method
}
// calculate the min distance
public static int findMinDistance(AnnualFuelUse[] car1)
{
int min = Integer.MAX_VALUE; // set min at a really big number
for(int index = 0; index < car1.length; index++)
{
if(min > car1[index].getDistance())
{
min = car1[index].getDistance();
}
}
return min;
}
public static int findMaxDistance(AnnualFuelUse[] car1)
{
int max = Integer.MIN_VALUE; // set min at a really small number
for(int index = 0; index < car1.length; index++)
{
if(max < car1[index].getDistance())
{
max = car1[index].getDistance();
}
}
return max;
}
public static double findMinMPG(AnnualFuelUse[] car1)
{
double min = Double.MAX_VALUE; // set min at a really big number
for(int index = 0; index < car1.length; index++)
{
if(min > car1[index].getDistance())
{
min = car1[index].getDistance();
}
}
return min;
}
public static double findMaxMPG(AnnualFuelUse[] car1)
{
double max = Double.MIN_VALUE; // set min at a really small number
for(int index = 0; index < car1.length; index++)
{
if(max < car1[index].getDistance())
{
max = car1[index].getDistance();
}
}
return max;
}
public static double findMinPrice(AnnualFuelUse[] car1)
{
double min = Double.MAX_VALUE; // set min at a really big number
for(int index = 0; index < car1.length; index++)
{
if(min > car1[index].getDistance())
{
min = car1[index].getDistance();
}
}
return min;
}
public static double findMaxPrice(AnnualFuelUse[] car1)
{
double max = Double.MIN_VALUE; // set min at a really small number
for(int index = 0; index < car1.length; index++)
{
if(max < car1[index].getDistance())
{
max = car1[index].getDistance();
}
}
return max;
}
}