Java 将数组数据从另一个类中的另一个方法输入到另一个类中的方法中
这些类旨在协同工作以产生如下输出: 我有一个逻辑错误,不允许我为显示和/或声明为0的变量生成输出和数据。我拼命地工作了几个小时,试图修正这个错误,但毫无效果。如果有人能告诉我如何解决代码中的问题以产生我想要的输出,我将不胜感激 这是一节课Java 将数组数据从另一个类中的另一个方法输入到另一个类中的方法中,java,arrays,methods,logic,Java,Arrays,Methods,Logic,这些类旨在协同工作以产生如下输出: 我有一个逻辑错误,不允许我为显示和/或声明为0的变量生成输出和数据。我拼命地工作了几个小时,试图修正这个错误,但毫无效果。如果有人能告诉我如何解决代码中的问题以产生我想要的输出,我将不胜感激 这是一节课 /** * This class instantiates car objects with ten private instance variables. * It contains eight mutator methods to calculate
/**
* This class instantiates car objects with ten private instance variables.
* It contains eight mutator methods to calculate mi/gal, distance (mi), gal/mi,
* total cost; minimum/maximum, total, and annual values for these variables within a car object.
* There are getter methods for each private instance variable.
*
* @author A. Mackey
* @version 01/02/14
*/
public class AnnualFuelUse
{
//private instance variables
private double gallonsUsed, pricePerGallon, totalCost,
minSpeed, maxSpeed,
minVariable, maxVariable, minMaxVariable,
galPerMi, miPerGal,
startingMiles, endingMiles, distance,
totalVariable[], totalValue;
//constructor with two parameters
AnnualFuelUse(int FillUpNumber, double eMiles, double sMiles, double galsUsed, double pricePerGal)
{
startingMiles = sMiles;
endingMiles = eMiles;
gallonsUsed = galsUsed;
pricePerGallon = pricePerGal;
}
//mutator method which calculates the distance drive
public void calcDistance()
{
distance = endingMiles - startingMiles;
}
//getter method to return the value of distance traveled by the object on one trip
public double getDistance()
{
return distance;
}
//mutator method which calculates gallons used per mile driven
public void calcMPG(double dist, double gallons)
{
miPerGal = dist / gallons;
}
//getter method to return the value of the miles per gallon of the object on one trip
public double getMPG()
{
return miPerGal;
}
//mutator method which calculates miles driven per gallons used
public void calcGPM(double gallons, double dist)
{
galPerMi = gallons / dist;
}
//getter method to return the value of the gallons per mile of the object on one trip
public double getGPM()
{
return galPerMi;
}
//mutator method which calculates total cost of filling up
public void totalCost()
{
totalCost = pricePerGallon * gallonsUsed;
}
//getter method to return the value of the total gasoline purchases on one trip
public double getTotalCost()
{
return totalCost;
}
//mutator method which calculates the minimum value of a given array
public void min(double minMaxVariable[])
{
minVariable = Double.MAX_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) < minVariable)
{
minVariable = minMaxVariable[i];
}
}
}
//getter method to return the minimum value of a given array
public double getMin()
{
return minVariable;
}
//mutator method which calculates the minimum value of a given array
public void max(double minMaxVariable[])
{
maxVariable = Double.MIN_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) > maxVariable)
{
maxVariable = minMaxVariable[i];
}
}
}
//getter method to return the minimum value of a given array
public double getMax()
{
return maxVariable;
}
//mutator method which calculates the total value of a given array
public void totalOf(double totalVariable[])
{
totalValue = 0;
for(double newValue : totalVariable)
{
totalValue += newValue;
}
}
//getter method to return the total value of a given array
public double getTotalOf()
{
return totalValue;
}
}
/**
*这个类用十个私有实例变量实例化汽车对象。
*它包含八种计算mi/gal、距离(mi)、gal/mi、,
*总成本;汽车对象中这些变量的最小值/最大值、总值和年值。
*每个私有实例变量都有getter方法。
*
*@作者A.麦基
*@version 01/02/14
*/
公共类年费
{
//私有实例变量
私人双加仑,每加仑价格,总成本,
minSpeed,maxSpeed,
minVariable,maxVariable,minMaxVariable,
加尔佩米,米佩加尔,
开始英里,结束英里,距离,
totalVariable[],totalValue;
//具有两个参数的构造函数
年费(整数填充数,双倍埃米尔,双倍微笑,双倍高利贷,双倍价格)
{
开始英里=微笑;
endingMiles=艾米尔斯;
gallussed=gallussed;
价格每加仑=价格每加仑;
}
//计算行驶距离的mutator方法
公共空隙距离()
{
距离=结束英里-开始英里;
}
//getter方法返回对象在一次行程中行驶的距离值
公共双getDistance()
{
返回距离;
}
//计算每英里行驶所用加仑数的mutator方法
公共空间容量(双倍距离,双倍加仑)
{
miPerGal=距离/加仑;
}
//getter方法返回一次行程中对象每加仑英里数的值
公共双getMPG()
{
返回miPerGal;
}
//计算所用每加仑行驶里程的mutator方法
公共空间计算CGPM(双加仑,双距离)
{
galPerMi=加仑/距离;
}
//getter方法返回一次行程中对象每英里加仑数的值
公共双getGPM()
{
返回galPerMi;
}
//计算填充总成本的mutator方法
公共成本()
{
总成本=每加仑价格*加仑消耗量;
}
//getter方法返回一次行程中汽油购买总量的值
公共双getTotalCost()
{
返回总成本;
}
//计算给定数组的最小值的mutator方法
公共void最小值(双minMaxVariable[])
{
minVariable=Double.MAX_值;
对于(int i=0;imaxVariable)
{
maxVariable=minMaxVariable[i];
}
}
}
//getter方法返回给定数组的最小值
公共双getMax()
{
返回maxVariable;
}
//计算给定数组总值的mutator方法
public void totalOf(double totalVariable[]
{
totalValue=0;
for(双新值:totalVariable)
{
totalValue+=新值;
}
}
//getter方法返回给定数组的总值
公共双getTotalOf()
{
返回总值;
}
}
及其测试类
/**
* This class tests the AnnualFuelUse class.
*
* An array of car objects is created to hold some of the private instance variables.
*
* A for loop is used to call the variables' methods on each object.
* A second for loop is used to print an output header identifying the variables for the instance
* A third for loop is used to print the values of the instance variables for each object.
*
* @author A. Mackey
* @version 01/02/14
*/
public class AnnualFuelUseTester
{
//main method
public static void main(String[] args)
{
//declaring and initialising variables
String header = "Annual Gas Mileage Calculations",
headerLine = "===============================";
String[] headerInfo = { "Fill Up" , " Days" , " Start Miles" , " End Miles" , "Distance" , "Gallons" , " Price" , "Cost" , " Miles/Gal" , "Gal/Mile" };
int[] fillUpNumber = {1, 2, 3, 4},
daysSinceFill = {1, 4, 8, 13};
double[] startMiles = {24963, 25437, 25937, 26221},
endMiles = { 25437, 25937, 26221, 26794},
gallonsUsed = { 21.4, 21.2, 12.2, 23.7},
milesPerGallon = new double[startMiles.length],
gallonsPerMile = new double[startMiles.length],
pricePerGallon = {3.52, 3.45, 3.39, 3.41},
totalCost = new double[startMiles.length],
distance = new double[startMiles.length];
int minDistance,
maxDistance,
totalDistance,
annualDistance;
double minMPG, minCost,
maxMPG, maxCost,
totalGallonsUsed, totalCostSum,
annualGallonsUsed, annualCost, annualMPG;
//initialization of array of objects
AnnualFuelUse[] car = {new AnnualFuelUse(fillUpNumber[0], endMiles[0], startMiles[0], gallonsUsed[0], pricePerGallon[0]),
new AnnualFuelUse(fillUpNumber[1], endMiles[1], startMiles[1], gallonsUsed[1], pricePerGallon[1]),
new AnnualFuelUse(fillUpNumber[2], endMiles[2], startMiles[2], gallonsUsed[2], pricePerGallon[2]),
new AnnualFuelUse(fillUpNumber[3], endMiles[3], startMiles[3], gallonsUsed[3], pricePerGallon[3])};
//call methods
for(int index = 0; index < car.length; index++)
{
distance[index] = car[index].getDistance();
milesPerGallon[index] = car[index].getMPG();
gallonsPerMile[index] = car[index].getGPM();
totalCost[index] = car[index].getTotalCost();
}
//I desire to invoke the methods which relate to these particular variables in order to define them
minDistance = 0;
minMPG = 0;
minCost = 0;
maxDistance = 0;
maxMPG = 0;
maxCost = 0;
totalDistance = 0;
totalGallonsUsed = 0;
totalCostSum = 0;
annualDistance = 0;
annualGallonsUsed = 0;
annualCost = 0;
annualMPG = 0;
//print results
System.out.printf("%74s%n", header);
System.out.printf("%74s%n", headerLine);
for(String info : headerInfo)
{
System.out.print(info + "\t");
}
System.out.println("\n=========================================================================================================================");
for(int index = 0; index < car.length; index++)
{
System.out.printf("%4d%10d%14.0f%14.0f%12.0f%16.1f%11.2f%12.2f%12.2f%13.3f%n", fillUpNumber[index], daysSinceFill[index], startMiles[index], endMiles[index], distance[index], gallonsUsed[index], pricePerGallon[index], totalCost[index], milesPerGallon[index], gallonsPerMile[index]);
}
System.out.println("=========================================================================================================================\n");
System.out.printf("Minimum:%46d%27.2f%24.2f%n", minDistance, minMPG, minCost);
System.out.printf("Maximum:%46d%27.2f%24.2f%n%n", maxDistance, maxMPG, maxCost);
System.out.printf("Totals:%47d%16.1f%35.2f%n", totalDistance, totalGallonsUsed, totalCostSum);
System.out.printf("Annual Projection:%36d%16.1f%11.2f%24.2f%n", annualDistance, annualMPG, annualGallonsUsed, annualCost);
}
}
/**
*这个类测试AnnualFuelUse类。
*
*将创建一个car对象数组来保存一些私有实例变量。
*
*for循环用于在每个对象上调用变量的方法。
*第二个for循环用于打印识别实例变量的输出头
*第三个for循环用于打印每个对象的实例变量值。
*
*@作者A.麦基
*@version 01/02/14
*/
公共级年费表
{
//主要方法
公共静态void main(字符串[]args)
{
//声明和初始化变量
字符串标题=“年度汽油里程计算”,
headerLine=“=================================================”;
字符串[]headerInfo={“加油”、“天数”、“开始里程”、“结束里程”、“距离”、“加仑”、“价格”、“成本”、“英里/加仑”、“加仑/英里”};
int[]fillUpNumber={1,2,3,4},
daysSinceFill={1,4,8,13};
双[]起始文件={24963,25437,25937,26221},
endMiles={25437,25937,26221,26794},
gallonsessed={21.4,21.2,12.2,23.7},
milesPerGallon=新的双精度[起始文件长度],
加仑许可证=新的双[起始里程长度],
价格每加仑={3.52,3.45,3.39,3.41},
totalCost=新的双精度[startMiles.length],
距离=新的双精度[startMiles.length];
心灵距离,
最大距离,
总距离,
年间隔;
双倍最低价格,最低成本,
maxMPG,maxCost,
总消耗,总成本,
阿努阿尔加隆
//call methods
for(int index = 0; index < car.length; index++)
{
car[index].calcDistance();
distance[index] = car[index].getDistance();
milesPerGallon[index] = car[index].getMPG();
gallonsPerMile[index] = car[index].getGPM();
totalCost[index] = car[index].getTotalCost();
}
public class AnnualFuelUse
{
//private instance variables
private double gallonsUsed, pricePerGallon, startingMiles, endingMiles;
//constructor with two parameters
AnnualFuelUse(int FillUpNumber, double eMiles, double sMiles, double galsUsed, double pricePerGal)
{
startingMiles = sMiles;
endingMiles = eMiles;
gallonsUsed = galsUsed;
pricePerGallon = pricePerGal;
}
//getter method to return the value of distance traveled by the object on one trip
public double getDistance()
{
return endingMiles - startingMiles;
}
//getter method to return the value of the miles per gallon of the object on one trip
public double getMPG()
{
return getDistance() / gallonsUsed;
}
//getter method to return the value of the gallons per mile of the object on one trip
public double getGPM()
{
return gallonsUsed / getDistance();
}
//getter method to return the value of the total gasoline purchases on one trip
public double getTotalCost()
{
return pricePerGallon * gallonsUsed;
}
//mutator method which calculates the minimum value of a given array
public static double min(double minMaxVariable[])
{
double minVariable = Double.MAX_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) < minVariable)
{
minVariable = minMaxVariable[i];
}
}
return minVariable;
}
//mutator method which calculates the minimum value of a given array
public static double max(double minMaxVariable[])
{
double maxVariable = Double.MIN_VALUE;
for(int i = 0; i < minMaxVariable.length; i ++)
{
if((minMaxVariable[i]) > maxVariable)
{
maxVariable = minMaxVariable[i];
}
}
return maxVariable;
}
//mutator method which calculates the total value of a given array
public static double totalOf(double totalVariable[])
{
double totalValue = 0;
for(double newValue : totalVariable)
{
totalValue += newValue;
}
return totalValue;
}
}
minDistance = (int)AnnualFuelUse.min(distance);
minMPG = AnnualFuelUse.min(milesPerGallon);
minCost = AnnualFuelUse.min(totalCost);
maxDistance = (int)AnnualFuelUse.max(distance);
maxMPG = AnnualFuelUse.max(milesPerGallon);
maxCost = AnnualFuelUse.max(totalCost);
totalDistance = (int)AnnualFuelUse.totalOf(distance);
totalGallonsUsed = AnnualFuelUse.totalOf(gallonsUsed);
totalCostSum = AnnualFuelUse.totalOf(totalCost);