Once you master getters, setters, constructors, and toString() , you’ll be ready for more advanced topics like inheritance, polymorphism, and encapsulation in larger projects. Got stuck? Double‑check your spelling, semicolons, and that your file is named Car.java . You’ve got this. 🚗
public int getYear() { return year; }
// Getter for make public String getMake() { return make; } // Getter for model public String getModel() { return model; }
// Constructor public Car(String carMake, String carModel, int carYear) { make = carMake; model = carModel; year = carYear; } These return the current values of the instance variables. 5.6.7 Car Class Codehs
// Getter for year public int getYear() { return year; } These allow you to change the values after the object is created.
public class Car { private String make; private String model; private int year; // Constructor public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; }
public String getModel() { return model; } You’ve got this
public void setYear(int year) { this.year = year; }
public String toString() { return year + " " + make + " " + model; } } Here’s the complete class (without a main method – CodeHS usually provides a separate tester file).
// Setters public void setMake(String make) { this.make = make; } public class Car { private String make; private
// Setter for year public void setYear(int newYear) { year = newYear; } This makes it easy to print a readable representation of the car.
// Getters public String getMake() { return make; }
Once you master getters, setters, constructors, and toString() , you’ll be ready for more advanced topics like inheritance, polymorphism, and encapsulation in larger projects. Got stuck? Double‑check your spelling, semicolons, and that your file is named Car.java . You’ve got this. 🚗
public int getYear() { return year; }
// Getter for make public String getMake() { return make; } // Getter for model public String getModel() { return model; }
// Constructor public Car(String carMake, String carModel, int carYear) { make = carMake; model = carModel; year = carYear; } These return the current values of the instance variables.
// Getter for year public int getYear() { return year; } These allow you to change the values after the object is created.
public class Car { private String make; private String model; private int year; // Constructor public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; }
public String getModel() { return model; }
public void setYear(int year) { this.year = year; }
public String toString() { return year + " " + make + " " + model; } } Here’s the complete class (without a main method – CodeHS usually provides a separate tester file).
// Setters public void setMake(String make) { this.make = make; }
// Setter for year public void setYear(int newYear) { year = newYear; } This makes it easy to print a readable representation of the car.
// Getters public String getMake() { return make; }