我用Java编写这两个文件,我的老师说它们工作正常,但我需要减少代码中私有实例变量的数量。你怎么会修这个呢?
public class CarV7 {
private String myCarType1, myCarType2, myCarType3;
private int mySmiles1, mySmiles2, mySmiles3;
private int myEmiles1, myEmiles2, myEmiles3;
private double myGals1, myGals2, myGals3;
private double myPricePerGallon1, myPricePerGallon2, myPricePerGallon3;
CarV7(String carType, int eMiles, int sMiles, double gals, double pricePerGallon) {
myCarType1 = carType;
myCarType2 = carType;
myCarType3 = carType;
mySmiles1 = sMiles;
mySmiles2 = sMiles;
mySmiles3 = sMiles;
myEmiles1 = eMiles;
myEmiles2 = eMiles;
myEmiles3 = eMiles;
myGals1 = gals;
myGals2 = gals;
myGals3 = gals;
myPricePerGallon1 = pricePerGallon;
myPricePerGallon2 = pricePerGallon;
myPricePerGallon3 = pricePerGallon;
}
public int calcDistance(int myEmiles, int sMiles) {
return myEmiles - sMiles;
}
public double calcMPG(int distance, double gals) {
return distance / gals;
}
public double calcGPM(double myGals, int distance) {
return myGals / distance;
}
public double totalCost(double pricePerGallon, double gals) {
return gals * pricePerGallon;
}
}
public class CarV7Tester {
public static void main(String[] args) {
String carType1, carType2, carType3;
int sMiles1, sMiles2, sMiles3;
int eMiles1, eMiles2, eMiles3;
double gals1, gals2, gals3;
double pricePerGallon1, pricePerGallon2, pricePerGallon3;
int distance1, distance2, distance3;
double cost1, cost2, cost3;
double mpg1, mpg2, mpg3;
double gpm1, gpm2, gpm3;
carType1 = "06 Saturn View";
carType2 = "03 Toyota Camr";
carType3 = "10 Nissan Leaf";
sMiles1 = 5678;
sMiles2 = 5678;
sMiles3 = 5678;
eMiles1 = 6015;
eMiles2 = 6015;
eMiles3 = 6015;
gals1 = 15.8;
gals2 = 17.6;
gals3 = 14.9;
pricePerGallon1 = 2.67;
pricePerGallon2 = 2.8;
pricePerGallon3 = 2.58;
CarV7 car1 = new CarV7(carType1, eMiles1, sMiles1, gals1, pricePerGallon1);
CarV7 car2 = new CarV7(carType2, eMiles2, sMiles2, gals2, pricePerGallon2);
CarV7 car3 = new CarV7(carType3, eMiles3, sMiles3, gals3, pricePerGallon3);
distance1 = car1.calcDistance(eMiles1, sMiles1);
distance2 = car2.calcDistance(eMiles2, sMiles2);
distance3 = car3.calcDistance(eMiles3, sMiles3);
cost1 = car1.totalCost(pricePerGallon1, gals1);
cost2 = car2.totalCost(pricePerGallon2, gals2);
cost3 = car3.totalCost(pricePerGallon3, gals3);
mpg1 = car1.calcMPG(distance1, gals1);
mpg2 = car2.calcMPG(distance2, gals2);
mpg3 = car3.calcMPG(distance3, gals3);
gpm1 = car1.calcGPM(gals1, distance1);
gpm2 = car2.calcGPM(gals2, distance2);
gpm3 = car3.calcGPM(gals3, distance3);
System.out.printf("%40s", "Gas Mileage Calculations");
System.out.println();
System.out.printf("%-13s %12s %10s %10s %10s %8s %6s %10s %10s", "Type of Car", "Start Miles", "End Miles", "Distance", "Gallons", "Price", "Cost", "Miles/Gal", "Gal/Mile");
System.out.println();
System.out.println("=================================================================================================");
System.out.printf("%-14s %9d %10d %10d %9.1f %8.1f %8.1f %10.3f %9.3f\n", carType1, sMiles1, eMiles1, distance1, gals1, pricePerGallon1, cost1, mpg1, gpm1);
System.out.printf("%-14s %9d %10d %10d %9.1f %8.1f %8.1f %10.3f %9.3f\n", carType2, sMiles2, eMiles2, distance2, gals2, pricePerGallon2, cost2, mpg2, gpm2);
System.out.printf("%-14s %9d %10d %10d %9.1f %8.1f %8.1f %10.3f %9.3f\n", carType3, sMiles3, eMiles3, distance3, gals3, pricePerGallon3, cost3, mpg3, gpm3);
}
}
答案 0 :(得分:0)
如果查看CarV7文件,您将拥有大量未在类中使用的实例变量。看起来你没有使用你在构造函数中设置的任何东西。
答案 1 :(得分:0)
将私有实例变量的声明更改为:
private String myCarType1;
private int mySmiles1;
private int myEmiles1;
private double myGals1;
private double myPricePerGallon1;
...因为根本没有使用其他变量。
然后按如下方式更改构造函数:
CarV7 (String carType, int eMiles, int sMiles, double gals, double pricePerGallon)
{
myCarType1 = carType;
mySmiles1 = sMiles;
myEmiles1 = eMiles;
myGals1 = gals;
myPricePerGallon1 = pricePerGallon;
}
不要在主方法中更改局部变量的名称。
Finally run the code... it should work.
答案 2 :(得分:0)
为什么你需要实例变量?似乎没有一个成员方法使用实例变量。因此,这些方法可以定义为静态,您甚至不需要首先实例化该类。
答案 3 :(得分:0)
我给你的CarV7类一些实例变量。由于变量是在构造函数中设置的,因此这些方法不需要任何参数。
package com.ggl.car;
public class CarV7 {
private int eMiles;
private int sMiles;
private double gallons;
private double pricePerGallon;
private String carType;
public CarV7(String carType, int sMiles, int eMiles,
double gallons, double pricePerGallon) {
this.carType = carType;
this.sMiles = sMiles;
this.eMiles = eMiles;
this.gallons = gallons;
this.pricePerGallon = pricePerGallon;
}
public int calcDistance() {
return eMiles - sMiles;
}
public double calcMPG() {
return (double) calcDistance() / gallons;
}
public double calcGPM() {
return gallons / calcDistance();
}
public double totalCost() {
return gallons * pricePerGallon;
}
public int geteMiles() {
return eMiles;
}
public int getsMiles() {
return sMiles;
}
public double getGallons() {
return gallons;
}
public double getPricePerGallon() {
return pricePerGallon;
}
public String getCarType() {
return carType;
}
}
我的测试人员课程没有太大变化。我在main方法的开头摆脱了变量类型,并设置变量类型,因为我使用了每个变量。
package com.ggl.car;
public class CarV7Tester {
public static void main(String[] args) {
String carType1 = "06 Saturn View";
String carType2 = "03 Toyota Camr";
String carType3 = "10 Nissan Leaf";
int sMiles1 = 5678;
int sMiles2 = 5678;
int sMiles3 = 5678;
int eMiles1 = 6015;
int eMiles2 = 6015;
int eMiles3 = 6015;
double gals1 = 15.8;
double gals2 = 17.6;
double gals3 = 14.9;
double pricePerGallon1 = 2.67;
double pricePerGallon2 = 2.8;
double pricePerGallon3 = 2.58;
CarV7 car1 = new CarV7(carType1, sMiles1, eMiles1, gals1, pricePerGallon1);
CarV7 car2 = new CarV7(carType2, sMiles2, eMiles2, gals2, pricePerGallon2);
CarV7 car3 = new CarV7(carType3, sMiles3, eMiles3, gals3, pricePerGallon3);
int distance1 = car1.calcDistance();
int distance2 = car2.calcDistance();
int distance3 = car3.calcDistance();
double cost1 = car1.totalCost();
double cost2 = car2.totalCost();
double cost3 = car3.totalCost();
double mpg1 = car1.calcMPG();
double mpg2 = car2.calcMPG();
double mpg3 = car3.calcMPG();
double gpm1 = car1.calcGPM();
double gpm2 = car2.calcGPM();
double gpm3 = car3.calcGPM();
System.out.printf("%40s", "Gas Mileage Calculations");
System.out.println();
System.out.printf("%-13s %12s %10s %10s %10s %8s %6s %10s %10s", "Type of Car", "Start Miles", "End Miles", "Distance", "Gallons", "Price", "Cost", "Miles/Gal", "Gal/Mile");
System.out.println();
System.out.println("=================================================================================================");
System.out.printf("%-14s %9d %10d %10d %9.1f %8.2f %8.2f %10.3f %9.3f\n", car1.getCarType(), sMiles1, eMiles1, distance1, gals1, pricePerGallon1, cost1, mpg1, gpm1);
System.out.printf("%-14s %9d %10d %10d %9.1f %8.2f %8.2f %10.3f %9.3f\n", car2.getCarType(), sMiles2, eMiles2, distance2, gals2, pricePerGallon2, cost2, mpg2, gpm2);
System.out.printf("%-14s %9d %10d %10d %9.1f %8.2f %8.2f %10.3f %9.3f\n", car3.getCarType(), sMiles3, eMiles3, distance3, gals3, pricePerGallon3, cost3, mpg3, gpm3);
}
}