Advanced Java - Lec #3 ~ Practical Work Examples.pptx
1. 1
In the name of Allah
Ghalib University
Computer Science Faculty
General Department
Advanced Java
Lecture Title
Lecture #3 Practical Work
Examples
Subject: Advanced Java
Instructor: Saddiqi
Date: Saturday, July 5, 2025
2. List of Contents
• At First Some Questions
• Classes for Objects
• Constructor
• Default value of data fields
• Visibility Modifiers.
• Class and Object
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 2
3. At First Some Questions
• Describe the relationship between an object and its defining class.
• How do you define a class?
• How do you create an object?
• Which operator is used to access a data field or invoke a method from
an object?
• What is an anonymous object?
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 3
4. Classes for Objects
Example #1
• Here is a program that defines the Circle class and uses it to create
objects.
• At first look at the UML diagram:
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 4
6. Cont.
Example #2
• As another example, consider television sets.
• Each TV is an object with states (current channel, current volume
level, power on or off) and behaviors (change channels, adjust
volume, turn on/off).
• You can use a class to model TV sets.
• The UML diagram for the class is shown in Figure 9.6.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 6
10. Constructor
Example #3
• What is wrong in the
following code?
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 10
11. Default value of data fields
Example #4
• What is the output of
the following code?
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 11
12. Cont.
Example #5
• What is wrong with each of the following programs?
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 12
13. Visibility Modifiers.
Example #6
• In this code, radius is private
in the Circle class,
• and myCircle is an object of the
Circle class.
• Does the highlighted code cause
any problems? If so, explain
why.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 13
14. Class and Object
Example #7
• (The Rectangle class) design a class named Rectangle to represent a
rectangle.
• The class contains:
1. Two double data fields named width and height that specify the width and height of the
rectangle. The default values are 1 for both width and height.
2. A no-arg constructor that creates a default rectangle.
3. A constructor that creates a rectangle with the specified width and height.
4. A method named getArea() that returns the area of this rectangle.
5. A method named getPerimeter() that returns the perimeter.
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 15
15. Summary
• At First Some Questions
• Classes for Objects
• Constructor
• Default value of data fields
• Visibility Modifiers.
• Class and Object
Saturday, July 5, 2025 Instructor: Saddiqi- Ghalib University 16