---
title: Computer Programming II Lab 3 v1.0
---
<h1 style='border: none'><center>Computer Programming II Lab 3</center></h1>
<h2 style='border: none'><center>Introduction to Object-Oriented Programming</br>Classes and Objects</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Authors: Usama R. Al Zayan<span style="float:right">2023/02/24</span></h6>
<h6>Parts of this Lab were adapted from work done by Mohammed Nafiz ALMadhoun</h6>
---
## Lab 3: Time and Plan
| Tasks | Timing |
| -------- | -------- |
| Quiz 1 | 10 min |
| Task 1 | 60 min |
## Lab Task
This Task shall guide you through the important concepts in inheritance.

<p>In this Task, a subclass called <code>Cylinder</code> is derived from the superclass <code>Circle</code> as shown in the class diagram (where an an arrow pointing up from the subclass to its superclass). Study how the subclass <code>Cylinder</code> invokes the superclass' constructors (via <code>super()</code> and <code>super(radius)</code>) and inherits the variables and methods from the superclass <code>Circle</code>.</p>
<p> Make sure that you keep "<code>Circle.class</code>" in the same directory.</p>
<p>Write a test program (says <code>TestCylinder</code>) to test the <code>Cylinder</code> format the output as the following:
```
Cylinder: radius=1.0 height=1.0 base area=12.566370614359172 volume=3.141592653589793
Cylinder: radius=10.0 height=1.0 base area=691.1503837897545 volume=314.1592653589793
Cylinder: radius=2.0 height=10.0 base area=150.79644737231007 volume=125.66370614359172
```
<p><span class="line-heading">Method Overriding and "Super":</span> The subclass <code>Cylinder</code> inherits <code>getArea()</code> method from its superclass Circle. Try <em>overriding</em> the <code>getArea()</code> method in the subclass <code>Cylinder</code> to compute the surface area (=2π×radius×height + 2×base-area) of the cylinder instead of base area. That is, if <code>getArea()</code> is called by a <code>Circle</code> instance, it returns the area. If <code>getArea()</code> is called by a <code>Cylinder</code> instance, it returns the surface area of the cylinder.</p>
<p>If you override the <code>getArea()</code> in the subclass <code>Cylinder</code>, the <code>getVolume()</code> no longer works. This is because the <code>getVolume()</code> uses the <em>overridden</em> <code>getArea()</code> method found in the same class. (Java runtime will search the superclass only if it cannot locate the method in this class). Fix the <code>getVolume()</code>.</p>
<p>Hints: After overridding the <code>getArea()</code> in subclass <code>Cylinder</code>, you can choose to invoke the <code>getArea()</code> of the superclass <code>Circle</code> by calling <code>super.getArea()</code>.</p>
<p>TRY:</p>
<p>Provide a <code>toString()</code> method to the <code>Cylinder</code> class, which overrides the <code>toString()</code> inherited from the superclass <code>Circle</code>, e.g.,</p>
<pre class="color-example">@Override
public String toString() { <span class="color-comment">// in Cylinder class</span>
return "Cylinder: subclass of " + super.toString() <span class="color-comment">// use Circle's toString()</span>
+ " height=" + height;
}</pre>
<p>Try out the <code>toString()</code> method in <code>TestCylinder</code>.</p>
<p>Note: <code>@Override</code> is known as <em>annotation</em> (introduced in JDK 1.5), which asks compiler to check whether there is such a method in the superclass to be overridden. This helps greatly if you misspell the name of the <code>toString()</code>. If <code>@Override</code> is not used and <code>toString()</code> is misspelled as <code>ToString()</code>, it will be treated as a new method in the subclass, instead of overriding the superclass. If <code>@Override</code> is used, the compiler will signal an error. <code>@Override</code> annotation is optional, but certainly nice to have.</p>
###### tags: `Computer Programming II` `Lab` `IUG` `Computer Engineering`
<center>End Of Lab 3</center>