OOP Exercise1(Encapsulation)
Cars
Implement a class Car, which contains the fields (5 points):
- make, e.g. Ford, Subaru, Toyota ...
- model, e.g., Escape, Outback, Camry ...
- year
- MPG miles per gallon
- milesDriven, the total number of miles ever driven in this car.
- fuelCapacity in gallons, i.e., the size in gallons of the fuel tank.
- fuelRemaining, which represents the amount of fuel remaining in the gas tank.
Implement at least the following methods within the Car class (5 points each):
- a constructor, which initializes each of the fields
- fillTank(double g), which adds up to g gallons of gas to the fuel tank, but not more than the car's fuel capacity.
- drive(double m), which simulates driving m miles in the car, adding to the total number of miles driven, and reducing the amount of gas in the car according to this car's average MPG.
- toString( ), which returns a String representation of the car.
- getFuelRemaining( ), which returns the amount of fuel left in the tank.
For example, we should be able to do something like the following:
Car oldJunker = new Car("Ford", "Pinto", 1972, 17.5, 132480, 12, 8); // creates a new Car object
oldJunker.drive(5); // drives the Car 5 miles
oldJunker.fillTank(1); // put in a gallon of gas
System.out.println(oldJunker.getFuelRemaining()); // prints the amount of fuel left
System.out.println(oldJunker); // prints the attributes of the car to the screen
Write a short driver program to test your Car class with a short array of Cars (5 points).
========================================中文
类:Cars
属性:
make(厂商),如:福特, 斯巴鲁,桑塔纳...
year(生产年份)
KMPG (每升油跑多少公里)
milesDriven(里程数,即这辆车一共开了多少公里),
fuelCapacity (油箱容量,单位是升)
fuelRemaining(油量剩余)
方法:
1.构造,用于初始化所有属性
2.加油: fillTank(double g)
g是加油的量,单位升,这个油量需要累加的剩余油量中,但加完油后如果剩余油量大于油箱容量,则剩余油量设置为油量容量
3.开车:drive(double km)
km是开了多少公里,该路程要累加到里程数中
同时,剩余油量会要减少(怎么减,自己思考)
4.toString( )方法
5.所有属性的get、set方法

浙公网安备 33010602011771号