0% found this document useful (0 votes)
7 views

Exercises3-Java-Inheritance and Polymorphism

The document describes creating classes for different types of vehicles including Car, Truck, Ford, and Sedan. It then creates objects of each class type and displays their sale prices. It also asks the user to enter two numbers and displays the sum, product, difference, and quotient.

Uploaded by

tivip.003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Exercises3-Java-Inheritance and Polymorphism

The document describes creating classes for different types of vehicles including Car, Truck, Ford, and Sedan. It then creates objects of each class type and displays their sale prices. It also asks the user to enter two numbers and displays the sum, product, difference, and quotient.

Uploaded by

tivip.003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex 1. a. Create a super class called Car.

The Car class has the following fields and


methods:

 int speed;
 double regularPrice;
 string color;
 double getSalePrice();

b. Create a sub class of Car class and name it as Truck. The Truck class has the following
fields and methods:

 int weight;
 double getSalePrice();//Ifweight>2000,10%discount.Otherwise,20%discount.
c. Create a subclass of Car class and name it as Ford. The Ford class has the following
fields and methods:

 int year;
 int manufacturerDiscount;
 double getSalePrice(); //From the sale price computed from Car class , subtract the
manufacturerDiscount.
d. Create a subclass of Car class and name it as Sedan. The Sedan class has the following
fields and methods:

 int length;
 double getSalePrice();//Iflength>20feet,5%discount,Otherwise,10%discount.

e. Create MyOwnAutoShop class which contains the main() method. Perform the
following within the main() method:

 Create an instance of Sedan class and initialize all the fields with appropriate
values. Use super(...) method in the constructor for initializing the fields of the
superclass.
 Create two instances of the Ford class and initialize all the fields with appropriate
values. Use super(...) method in the constructor for initializing the fields of the
super class.
 Create an instance of Car class and initialize all the fields with appropriate values.
 Display the sale prices of all instance.

Ex 2. Write an applet that asks the user to enter two floating ‐point numbers, obtains the
two numbers from the user and draws their sum, product (multiplication), difference and
quotient (division). Use the techniques shown in example.

You might also like