Question
public class Car{ String model; // Attribute to store the model of a car int age; // Attribute to store the age of a car
public class Car{ String model; // Attribute to store the model of a car int age; // Attribute to store the age of a car Car() { // Constructor }
Car(String s, int i) { // Constructor model = s; age = i; }
public String getModel() { // Method to get the model of car return model; }
public void setModel(String model) { this.model = model; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public boolean equals(Car c) { // to compare if two cars are equal if ((this.age == c.age) && (this.model.equals(model))) return true; else return false; }
public void printDetails() { // Printing the details of car System.out.println("Model:" + model); System.out.println("Age:" + age); }
public void run() // Method to call when you want the car to run { System.out.println("The car is running"); }
public static void main(String args[]) { Car c = new Car("Maruti", 10); // Creating an instance of the car class c.printDetails(); // Printing car details c.setModel("BMW"); // Changing model of the car System.out.println("After applying changes:"); // Output to the screen c.printDetails(); // Printing car details } }
Using the java class above, add encapsulation to it to include making all attributes private, adding constructor, and adding get and set methods. The main method should create an instance of the class and demonstrate correct functionality of all the methods. Submit your program as an attached .java file and post a screen shot to show that you have been able to successfully run that program.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started