Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program: Create a class named Car that contains data fields for the manufac

ID: 3819407 • Letter: J

Question

Java Program:

Create a class named Car that contains data fields for the manufacturer, model, and manufacture year, e.g. Ford, Escape, 2015. Include get and set methods for these fields. The class also includes a display() method that displays all the car data. Next, create a subclass named RaceCar, which contains an additional field that holds the number of races in which the car has competed and additional methods to get and set the new field. Override the parent class display() method to include RaceCar information. Next create a subclass named PrivateCar which contains an additional field that holds the owners name and additional methods to get and set the new field. Override the parent class display() method to include PrivateCar information. Write an application that demonstrates using objects of each class. Save the files as Car.java, RaceCar.java, PrivateCar.java, and DemoCar.java. At the beginning of the program add comments including program description.

Explanation / Answer

/*

                Car.java

                Car describes about manfacturer, model and year

*/

public class Car {

                String manfacturer,model;

                int year;

                //Setters and Getters

                public String getManfacturer() {

                                return manfacturer;

                }

                public void setManfacturer(String manfacturer) {

                                this.manfacturer = manfacturer;

                }

                public String getModel() {

                                return model;

                }

                public void setModel(String model) {

                                this.model = model;

                }

                public int getYear() {

                                return year;

                }

                public void setYear(int year) {

                                this.year = year;

                }

                //Method display is used to print information

                public void display(){

                                System.out.println("Manfacturer:"+manfacturer+" "+"Model is:"+model+" "+"Year:"+year);

                }

}

/*

    RaceCar.java

    RaceCar is child class of car contains noOfRaces

*/

public class RaceCar extends Car{

                int noOfRaces;

                //Setters and getters of number of races

                public int getNoOfRaces() {

                                return noOfRaces;

                }

                public void setNoOfRaces(int noOfRaces) {

                                this.noOfRaces = noOfRaces;

                }

               

                //Overriding method of display() method

                public void display(){

                                System.out.println("********Race Car Information******");

                                System.out.println("No of Races:"+noOfRaces);

                                super.display(); // Calling super class Car display method

                }

}

/*

                PrivateCar.java

                PrivateCar is child class of Car

*/

public class PrivateCar extends Car{

                String ownerName;

                //Setters and Getters of ownerName

                public String getOwnerName() {

                                return ownerName;

                }

                public void setOwnerName(String ownerName) {

                                this.ownerName = ownerName;

                }

               

                //Overriding method of display() method

                public void display(){

                                System.out.println("*****Private Car Information******");

                                System.out.println("Owners Name:"+ownerName);

                                super.display(); // Calling super class Car display method

                }

}

/*

                DemoCar.java

                DemoCar is used to instantiate RaceCar and PrivateCar

*/

public class DemoCar {

                public static void main(String[] args) {

                               

                                // RaceCar instance

                                RaceCar raceCar = new RaceCar();

                                //Setters to set the data

                                raceCar.setManfacturer("Maruti");

                                raceCar.setModel("Swift Dzire");

                                raceCar.setYear(2012);

                                raceCar.setNoOfRaces(15);

                                raceCar.display();

                               

                               

                                // PrivateCar instance

                                PrivateCar privateCar = new PrivateCar();

                                //Setters to set the data

                                privateCar.setManfacturer("Ford");

                                privateCar.setModel("Fiesta");

                                privateCar.setYear(2010);

                                privateCar.setOwnerName("Madhu Babu");

                                privateCar.display();

                }

}

OUTPUT:

********Race Car Information******

No of Races:15

Manfacturer:Maruti

Model is:Swift Dzire

Year:2012

*****Private Car Information******

Owners Name:Madhu Babu

Manfacturer:Ford

Model is:Fiesta

Year:2010

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote