Professional Documents
Culture Documents
Programming Language 2
Computer Science
Department
2023
Lab Objectives:
In this lab, a student will practice
• Reading part of a UML class diagram • Implementing is-a relationship.
• Creating a class. • Overriding methods.
• Declaring instance variables. • Writing a test application to
• Declaring a constructor. demonstrate the capabilities of another
• Declaring accessors and mutators classes.
methods
Lab Exercise:
Given the following UML implement all the classes in the same package with all needed instance variables,
accessor and mutators methods and constructors; then write a test application to demonstrate the classes
capabilities.
<<Shape>>
-name:String
+<<Constructor>> (Full Arguments)
+<<all setter>>
+<all getter>>
+ CalculateArea(): double
<<Circle>>
-radius:double
-
+<<Constructor>> (Full Arguments)
+<<all setter>>
+<all getter>>
+ CalculateArea(): double
+
Consider the following points:
Link the class Circle with class Shape by is_a relationship and add any needed suitable methods.
In Shape Class:
• CalculateArea() return zero.
In Class Circle:
• Use constructor call chaining to refer to the superclass using the keyword super.
• Override the method CalculateArea() to calculate area for each class, use @Override
annotation.
In main:
• create object Shape1 of type Shape,
• create object C1 of type Circle.
• Then call method CalculateArea() for the two objects Shape1 and C1 and print the
values.
Hint:
• Area of a circle: 𝜋 ∗ 𝑟𝑎𝑑𝑖𝑢𝑠 2
Assignment:
Write a Java program that demonstrate Inheritance, “is-A” relationship. Your program must
logically represent the relationship. DO NOT use the examples from the lecture
In your program, design and implement 3 classes ( one superclass and two subclasses ) in the
same package with all needed instance variables, accessor and mutators methods and
constructors; then write a test application to demonstrate the classes capabilities.
Consider the following points: