📄 autoshow.java
字号:
import java.util.ArrayList;
import java.util.Iterator;
public class Autoshow {
//These are the instance variables
private ArrayList<Car> cars;
//Here is the get method
public ArrayList<Car> getCars() { return cars; }
//Here is the constructor
public Autoshow() {
cars = new ArrayList<Car>();
}
//This method returns a string representing the car
public String toString() {
return ("Autoshow with " + cars.size() + " cars");
}
/***************** 2.1 ********************/
//This method adds a car to the Autoshow
public void addCar(Car aCar) {
cars.add(aCar);
}
//This method returns the number of cars in the auto show
public int carCount() {
return cars.size();
}
/***************** 2.2 ********************/
//This method returns a new ArrayList of cars with the given make
public ArrayList<Car> carsWithMake(String aMake) {
ArrayList<Car> result = new ArrayList<Car>();
Iterator carsIterator = cars.iterator();
while(carsIterator.hasNext()){
Car aCar = (Car) carsIterator.next();
if(aCar.getMake().equals(aMake))
result.add(aCar);
}
/*for (Car aCar: cars) {
if (aCar.getMake().equals(aMake))
result.add(aCar);
}*/
return result;
}
/***************** 2.3 ********************/
//This method returns a new arrayList of the different car makes (no duplicates)
public ArrayList<String> differentMakes() {
ArrayList<String> result = new ArrayList<String>();
for (Car aCar: cars) {
if (!result.contains(aCar.getMake()))
result.add(aCar.getMake());
}
return result;
}
/***************** 2.4 ********************/
//This method returns the car with the maximum topSpeed
public Car fastestCar() {
if (cars.isEmpty())
return null;
Car maxCar = cars.get(0);
for (Car aCar: cars){
if (aCar.getTopSpeed() > maxCar.getTopSpeed())
maxCar = aCar;
}
return maxCar;
}
/***************** 2.5 ********************/
//This method returns the color that is most common
public String mostCommonColor() {
String maxColor = null;
int maximum = 0;
for (Car searchCar: cars) {
String searchColor = searchCar.getColor();
int count = 0;
for (Car aCar: cars) {
if (aCar.getColor().equals(searchColor))
count++;
}
if (count > maximum) {
maximum = count;
maxColor = searchColor;
}
}
return maxColor;
}
/***************** 2.6 ********************/
//This method prints out the cars in order of their top speed
public void printBySpeed() {
//Copy the cars into a new ArrayList
ArrayList<Car> carsCopy = new ArrayList<Car>(cars);
//Now repeatedly find the car with maximum speed
while (!carsCopy.isEmpty()) {
Car maxSpeedCar = carsCopy.get(0);
for (Car aCar: carsCopy) {
if (aCar.getTopSpeed() > maxSpeedCar.getTopSpeed())
maxSpeedCar = aCar;
}
System.out.println(maxSpeedCar);
carsCopy.remove(maxSpeedCar);
}
}
/***************** 2.7 ********************/
//This method prints out the cars in order of their make
public void printByMake() {
//Copy the cars into a new ArrayList
ArrayList<Car> carsCopy = new ArrayList<Car>(cars);
//Now repeatedly find the car with least alphabetical order
while (!carsCopy.isEmpty()) {
Car minMakeCar = carsCopy.get(0);
for (Car aCar: carsCopy) {
if (aCar.getMake().compareTo(minMakeCar.getMake()) < 0)
minMakeCar = aCar;
}
System.out.println(minMakeCar);
carsCopy.remove(minMakeCar);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -