/*
A progress that uses the Vehicle class.
Call this file VehicleDemo.java
This program creates two Vehicle objects.
Add range to Vehicle
*/
class Vehicle
{
int passengers; //number of passengers
int fuelCup; //fule capacity in gallons
int mpg; //fule consumption in miles per gallon
//Display the range.
void range()
{
System.out.println("Range is " + fuelCap * mpg);
}
}
//This class declares an object of type Vehicle.
class TwoVehicles
{
public static void main(String[] args)
{
Vehicle minivan = new minivan();
Vehicle sportscar = new sportscar();
int range1, range2;
//assign value to fields in minivan
minivan.passengers = 7;
minivan.fuelCup = 16;
minivan.mpg = 21;
//assign value to files in sportscar
sportscar.passenger = 2;
sportscar.fuelCup = 14;
sportscar.mpg = 12;
//compute the range assuming a full tank of gas
range1 = minivan.fuelCup * minivan.mpg;
range2 = sportscar.fuelCup * sportscar.mpg;
System.out.println("Minivan can carry " + minivan.passengers + "with a range of "+".");
minivan.range();
System.out.println("Sportscar can carry " + sportscar.passengers + "with a range of "+"." );
sportscar.range();
}
}
运行如下:
E:\jae>javac TwoVehicles.java
TwoVehicles.java:16: 错误: 找不到符号
System.out.println("Range is " + fuelCap * mpg);
^
符号: 变量 fuelCap
位置: 类 Vehicle
TwoVehicles.java:26: 错误: 找不到符号
Vehicle minivan = new minivan();
^
符号: 类 minivan
位置: 类 TwoVehicles
TwoVehicles.java:27: 错误: 找不到符号
Vehicle sportscar = new sportscar();
^
符号: 类 sportscar
位置: 类 TwoVehicles
TwoVehicles.java:36: 错误: 找不到符号
sportscar.passenger = 2;
^
符号: 变量 passenger
位置: 类型为Vehicle的变量 sportscar
4 个错误
A progress that uses the Vehicle class.
Call this file VehicleDemo.java
This program creates two Vehicle objects.
Add range to Vehicle
*/
class Vehicle
{
int passengers; //number of passengers
int fuelCup; //fule capacity in gallons
int mpg; //fule consumption in miles per gallon
//Display the range.
void range()
{
System.out.println("Range is " + fuelCap * mpg);
}
}
//This class declares an object of type Vehicle.
class TwoVehicles
{
public static void main(String[] args)
{
Vehicle minivan = new minivan();
Vehicle sportscar = new sportscar();
int range1, range2;
//assign value to fields in minivan
minivan.passengers = 7;
minivan.fuelCup = 16;
minivan.mpg = 21;
//assign value to files in sportscar
sportscar.passenger = 2;
sportscar.fuelCup = 14;
sportscar.mpg = 12;
//compute the range assuming a full tank of gas
range1 = minivan.fuelCup * minivan.mpg;
range2 = sportscar.fuelCup * sportscar.mpg;
System.out.println("Minivan can carry " + minivan.passengers + "with a range of "+".");
minivan.range();
System.out.println("Sportscar can carry " + sportscar.passengers + "with a range of "+"." );
sportscar.range();
}
}
运行如下:
E:\jae>javac TwoVehicles.java
TwoVehicles.java:16: 错误: 找不到符号
System.out.println("Range is " + fuelCap * mpg);
^
符号: 变量 fuelCap
位置: 类 Vehicle
TwoVehicles.java:26: 错误: 找不到符号
Vehicle minivan = new minivan();
^
符号: 类 minivan
位置: 类 TwoVehicles
TwoVehicles.java:27: 错误: 找不到符号
Vehicle sportscar = new sportscar();
^
符号: 类 sportscar
位置: 类 TwoVehicles
TwoVehicles.java:36: 错误: 找不到符号
sportscar.passenger = 2;
^
符号: 变量 passenger
位置: 类型为Vehicle的变量 sportscar
4 个错误









