I have an assignment to write a program to project your annual fuel usage based on three fill ups of a car. I have to use two separate classes. This is my first class, titled AnnualFuelUse.
public class AnnualFuelUse
{
private static int endMiles, startMiles,fillUp, days,distance;
private double gallonsUsed, pricePerGallon,MPG,cost;
//constructor
AnnualFuelUse(int fu, int days, int sm, int em, double gu, double price)
{
fillUp = fu;
days = days;
startMiles = sm;
endMiles = em;
gallonsUsed = gu;
pricePerGallon = price;
distance = 0;
MPG = 0.0;
cost = 0.0;
}
//calculates distance
public void calcDistance ()
{
distance = endMiles - startMiles;
}
public int getDistance(){
return distance;
}
//calculates miles per gallon
public void calcMPG()
{
MPG = distance /gallonsUsed;
}
public double returnMPG(){
return MPG;
}
//calculates total cost
public void totalCost(){
cost= gallonsUsed * pricePerGallon;
}
public double getCost(){
return cost;
}
This is my second class, titled AnnualFuelUseTester. (sorry for the long names, but it's required)
public class AnnualFuelUseTester
{
public static void main(String[]args)
{
int fillUp1,fillUp2,fillUp3,days1,days2,days3,startMiles1,startMiles2,startMiles3;
int endMiles1,endMiles2,endMiles3,distance1,distance2,distance3;
double gallons1,gallons2,gallons3,MPG1,MPG2,MPG3,price1,price2,price3;
double cost1,cost2,cost3;
AnnualFuelUseTester[]fuel = {new AnnualFuelUseTester(1,1,45023,45231,10.00,2.95),
new AnnualFuelUseTester(2,4,45231,45480,11.70,2.99),
new AnnualFuelUseTester(3,8,45480,45659,9.30,3.01)};
for (int index = 0; index<fuel.length;index++)
{
fuel[index].calcDistance();
fuel[index].calcMPG();
fuel[index].totalCost();
}
System.out.println(" Fill Up Days Start Miles End Miles Distance Gallons Used MPG Price Cost ");;
for(int index = 0; index < fuel.length; index++)
{
System.out.printf("%5i %5i %5i %5i %5i %5.2d %5.2d %5.2d %5.2d %n",
fillUp,days,startMiles,endMiles,fuel[index].getDistance(),gallonsUsed,fuel[index].getMPG(),price,fuel[index].getCost());
}
}
}
The problem I have so far is that when I run it, there is an error saying that AnnualFuelUseTester in class AnnualFuelUseTester cannot be applied to given types; required: no arguments; found: int,int,int,int,double,double; reason: actual and formal argument lists differ in length.
I think this is saying something about the length or types not matching up correctly, but as far as I see, it seems to be fine. I'm not sure how to fix this, and would appreciate some help.
I don't know if the code will run correctly even if I fix that, but I can't get any further right now, so I'm trying to go one step at a time. If you see any glaring errors, please point them out, so I can figure out how to fix it.
I edited AnnualFuelUseTester to this:
public class AnnualFuelUseTester
{
public static void main(String[]args)
{
int fillUp1 = 0 ,fillUp2 = 0,fillUp3 = 0,days1 = 0,days2= 0,days3= 0,startMiles1= 0,startMiles2= 0,startMiles3= 0;
int endMiles1= 0,endMiles2= 0,endMiles3= 0,distance1= 0,distance2= 0,distance3= 0;
double gallons1= 0,gallons2= 0,gallons3= 0,MPG1= 0,MPG2= 0,MPG3= 0,price1= 0,price2= 0,price3= 0;
double cost1= 0,cost2= 0,cost3= 0;
AnnualFuelUse[]fuel = {new AnnualFuelUse(1,1,45023,45231,10.00,2.95),
new AnnualFuelUse(2,4,45231,45480,11.70,2.99),
new AnnualFuelUse(3,8,45480,45659,9.30,3.01)};
for (int index = 0; index<fuel.length;index++)
{
fuel[index].calcDistance();
fuel[index].calcMPG();
fuel[index].totalCost();
}
System.out.println(" Fill Up Days Start Miles End Miles Distance Gallons Used MPG Price Cost ");;
for(int index = 0; index < fuel.length; index++)
{
System.out.printf("%5d %5d %5d %5d %5d %5.2d %5.2d %5.2d %5.2d \n",
fillUp1,days1,startMiles1,endMiles1,fuel[index].getDistance(),gallons1,fuel[index].returnMPG(),price1,fuel[index].getCost());
}
}
}
The problem I mentioned earlier is fixed, but now it has an error on line 31 - System.out.printf("%5d %5d %5d %5d %5d %5.2d %5.2d %5.2d %5.2d \n", It compiles fine, but when I try to run it there is an error saying "java.util.IllegalFormatPrecisionException: null(in java.util.Formatter$FormatSpecifier). What does this mean?