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

Tutorial 2: On Objects & Classes, Inheritance and Polymorphism

The document discusses two tutorials on object-oriented programming concepts in Java. The first tutorial describes how to create a Stock class with fields to store stock name, symbol, previous and current prices, and a method to calculate percentage change. A test class is then created to instantiate Stock objects and call methods. The second tutorial describes how to create a multimedia item database application using inheritance. An Item class is created as the parent with common fields. Child classes CD and DVD are created by extending Item and adding unique fields. Getters, setters and toString methods are included. A main test class instantiates objects to test the classes.

Uploaded by

Lucas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Tutorial 2: On Objects & Classes, Inheritance and Polymorphism

The document discusses two tutorials on object-oriented programming concepts in Java. The first tutorial describes how to create a Stock class with fields to store stock name, symbol, previous and current prices, and a method to calculate percentage change. A test class is then created to instantiate Stock objects and call methods. The second tutorial describes how to create a multimedia item database application using inheritance. An Item class is created as the parent with common fields. Child classes CD and DVD are created by extending Item and adding unique fields. Getters, setters and toString methods are included. A main test class instantiates objects to test the classes.

Uploaded by

Lucas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

TUTORIAL 2: ON OBJECTS & CLASSES,

INHERITANCE AND POLYMORPHISM

28 March 2017
Lectured by Jerelyn Pillay and Alicia Nair

1. Develop a java application. Your application should have two classes, a Stock class and a
test/main class. Design a class named Stock that contains:
A string data field named symbol for the stocks symbol.
A string data field named name for the stocks name.
A double data field named previousClosingPrice that stores the stock price for the
previous day.
A double data field named currentPrice that stores the stock price for the current
time.
A constructor that creates a stock with the specified symbol and name.
A method named getChangePercent() that returns the percentage changed from
previousClosingPrice to currentPrice.

Create a test/main class to create your Stock objects and invoke your methods.

2. Develop an application to manage a database of multimedia items. The user has many
multimedia items that include audio CDs, DVDs and PC games. The application/project
must allow the user to :
Store his multimedia items
Search through the database of stored items to find particular items.
Print out items
1. Create a project called DOME, within that create a package called dome.
2. Write a class called Item. The Item class represents all that is common between various
multimedia items. It should have the following attributes :

- A String name to store the name of the item


- A String comment to store a comment about the item
- A double value to store the items cost

3. It should have the following methods :


- Getters and setter methods for all the attributes
- A toString method
4. Now create two additional classes to represent the multimedia items, CDs and DVDs, we
store.
(Note: the use of inheritance)
5. Write a CD class that extends the Item class. The CD class has a String artist field and an
int playTime field.
6. Write a DVD class that extends the Item class. The DVD class has a String director (the
director of the movie) and an int runningTime. Provide the correct constructors and
override the toString() method.
7. Write a main class/ test class and create objects to test your classes

You might also like