Unit 5 Code Examples
Java Code Examples for Unit 5
import java.util.ArrayList;
public class CarClass{
//Constant
public final String vehicleType = "Car";
//Instance Variables
private String model;
private int milesRun;
private float velocity;
// Static Variables
public static ArrayList<CarClass> carList = new ArrayList<>();
//Constructor
public CarClass(String model, int milesRun, float velocity){
this.model = model;
this.milesRun = milesRun;
this.velocity = velocity;
}
//Methods
public void printModel(){
System.out.println(getModel());
}
public void printMilesRun(){
System.out.println(getMilesRun());
}
public void printVelocity(){
System.out.println(getVelocity());
}
//Getters
public String getModel(){
return model;
}
public int getMilesRun(){
return milesRun;
}
public float getVelocity(){
return velocity;
}
//Setters
public void setModel(String newModel){
model = newModel;
}
public void setMilesRun(int newMilesRun){
milesRun = newMilesRun;
}
public void setVelocity(float newVelocity){
velocity = newVelocity;
}
//Static Methods
public static void manufactureCar(String model, int milesRun, float velocity){
CarClass newCar = new CarClass(model, milesRun, velocity);
carList.add(newCar);
}
public static void printCarDetails(){
for (int carIndex = 0; carIndex < carList.size(); carIndex++){
System.out.println("---------------------------------");
carList.get(carIndex).printModel();
}
System.out.println("---------------------------------");
}
//main method
public static void main(String[] args){
manufactureCar("Tesla Model X", 10, 100);
manufactureCar("Prius", 200, 70);
manufactureCar("Toyota Minivan", 500, 65);
CarClass.printCarDetails();
}
}
CarClass.main(null);