0% found this document useful (0 votes)
143 views2 pages

Exercises 4

The document describes exercises to create classes and demonstrate inheritance and polymorphism in Java. Exercise 1 involves creating a Car class with attributes like speed and gears, along with methods to drive and display attributes. A SportCar class inherits from Car and adds an airbag type attribute, overriding the display method. Exercise 2 defines a Payment class with an amount attribute and methods to get/set amount. CashPayment and CreditCardPayment inherit from Payment, with CashPayment redefining the paymentDetails method and CreditCardPayment adding credit card attributes and redefining paymentDetails. Exercise 3 involves creating classes based on a UML diagram. Exercise 4 demonstrates polymorphism by making different animal subclasses like Dog and Cat shout using a parent

Uploaded by

crazz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views2 pages

Exercises 4

The document describes exercises to create classes and demonstrate inheritance and polymorphism in Java. Exercise 1 involves creating a Car class with attributes like speed and gears, along with methods to drive and display attributes. A SportCar class inherits from Car and adds an airbag type attribute, overriding the display method. Exercise 2 defines a Payment class with an amount attribute and methods to get/set amount. CashPayment and CreditCardPayment inherit from Payment, with CashPayment redefining the paymentDetails method and CreditCardPayment adding credit card attributes and redefining paymentDetails. Exercise 3 involves creating classes based on a UML diagram. Exercise 4 demonstrates polymorphism by making different animal subclasses like Dog and Cat shout using a parent

Uploaded by

crazz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercises

-----------1 Create a class Car which contains members speed,


noOfGear. The class has a method drive() which is
responsible to provide starting speed and noOfGears to a
Car. Implement display() method which will display all
attributes of Car class.
The class SportCar is derived from the class Car which
adds new features AirBallonType. When this method is
invoked, initial speed and gear status must be displayed
on console. Override the display method which display all
attribute of the SportCar. Make use of super class display()
method.
2 Define a class named Payment that contains a member
variable of type double that stores the amount of the
payment and appropriate accessor and mutator methods.
Also create a method named paymentDetails that outputs
an English sentence to describe the amount of the
payment.
Next, define a class named CashPayment that is derived
from Payment. This class should redefine the
paymentDetails method to indicate that the payment is in
cash. Include appropriate constructor(s).
Define a class named CreditCardPayment that is derived
from Payment. This class should contain member variables
for the name on the card, expiration date, and credit card
number. Include appropriate constructor(s). Finally,
redefine the paymentDetails method to include all credit
card information in the printout.
Create a main method that creates at least two
CashPayment and two
CreditCardPayment objects with different values and calls
paymentDetails for each.

Create classes as shown in the UML diagram given above


and test your classes
4 There is an animal class which has the common
characteristics of all animals. Dog, Horse, Cat are
animals(sub-class). Each can shout, but each shout is
different. Use polymorphism to create objects of same and
using an animal variable, make each of the animals shout.
5 Write a program to calculate the number of objects
created at a given point using user defined class

You might also like