Lab 9 Inheritance BESE-11

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Department of Computing

CS 212: Object Oriented Programming

Class: BESE-11
Lab 09: Inheritance

Date: 5th May, 2021

Instructor: Ms. Hania Aslam

CS 212: Object Oriented Programming Page 1


Learning Objectives

The learning objective of this lab is to understand and practice the concept of inheritance, a very
powerful feature of OOP which helps in code reusability.

Activity #1.

What is the output of running the class ActivityOne? Explain the output.

Hint. Remember only super-class data members (instance variables and instance methods) are
inherited by the sub-class, and constructors are not included.

class A
{
public A()
{
System.out.println( "A's no-arg constructor is invoked");
}
}

class B extends A {}

public class ActivityOne


{
public static void main( String[] args)
{
B b = new B();
}
}
Activity #2.

The following Java program compiles correctly. Show its output, and explain what is happening
when an object of Class B is created?

class A
{
public A()
{
System.out.println( "A's constructor is invoked");
}
}

class B extends A
{

CS 212: Object Oriented Programming Page 2


public B(int t)
{
System.out.println( "B's constructor is invoked");
}
}

public class ActivityTwo


{
public static void main( String[] args)
{
B b = new B(3);
}
}

Activity #3.

The program fails to compile. Identify the problem and propose a correction? Also explain the
reason.Hint. If a constructor does not explicitly call a super constructor as its first statement, a
call to super() is automatically added.

class A
{
public A( int x) {}
}

class B extends A
{
public B() {}
}

public class ActivityThree


{
public static void main( String[] args)
{
B b = new B();
}
}

Activity #4.

CS 212: Object Oriented Programming Page 3


Identify and correct the problems in the following program so that it compiles and runs fine.

class Circle
{
private double radius;

public Circle( double radius)


{
radius = radius;
}

public double getRadius()


{
return radius;
}

public double getArea()


{
return radius * radius * Math.PI;
}
}

class B extends Circle


{
private double length;

B( double radius, double length)


{
Circle( radius);
length = length;
}

/** Override getArea() */


public double getArea()
{
return getArea() * length;
}
}

public class ActivityFour


{

CS 212: Object Oriented Programming Page 4


public static void main( String[] args)
{
B b = new B( 5, 10);
System.out.println( "Area = " + b.getArea());
}
}

Task #1:

The following UML class diagram illustrates an inheritance relationship, wherein the classes
Circle and Rectangle have been extended from the class GeometricObject.

You’re required to implement the classes GeometricObject and Rectangle.

The Rectangle class contains:


CS 212: Object Oriented Programming Page 5
 Two double data fields named width and height that specify the width and height of
the rectangle. The default values are 1.0 for both width and height.
 A no-arg constructor that creates a default rectangle.
 A constructor that creates a rectangle with the specified width and height.
 A method named getArea() that returns the area of this rectangle.
 A method named getPerimeter() that returns the perimeter.
 A method named toString() that returns a string description for the rectangle.

The toString() method shall be implemented as follows:

return "Rectangle: width = " + width + " height = " + height;

Write a test program that prompts the user to enter width and height of the rectangle, a color,
and a Boolean value to indicate whether the rectangle is filled. The program should create a
Rectangle object and set the color and filled properties using the input. The program should
display the area, perimeter, color, and true or false to indicate whether it is filled or not.

Hint:
In Java, getting the current date is as simple as instantiating the Date object from the Java package java.util:
java.util.Date date=new java.util.Date();
System.out.println(date);
// You may need to use Date class while implementing the getDateCreated() method of GeometricObject class.

Task #2:

Design a class named Person and its two subclasses named Student and Employee. Make
Faculty and Staff subclasses of Employee.

A person has a name, address, phone number, and email address. A student has a class status
(freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an
office, salary, and date hired. A faculty member has office hours and a rank. A staff member has
a title. Override the toString() method in each class to display the class name and the person’s
name.

Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and
invokes their toString() methods.

CS 212: Object Oriented Programming Page 6


Hand in

Hand in the source code from this lab at the appropriate location on the LMS system. You should
hand in a single file named Lab_9_<Your CMS_ID. Your_NAME >.docx (without angle
brackets) that contains ONLY the following.

1) All completed java source code representing the work accomplished for this lab: The
code snippets should contain author information in the comments at the top.

To Receive Credit

1. Comment your program heavily. Intelligent comments and a clean, readable formatting
of your code account for 20% of your grade.

2. The lab time is not intended as free time for working on your programming/other
assignments.

CS 212: Object Oriented Programming Page 7

You might also like