SlideShare a Scribd company logo
CSC139 Chapter 10 Lab Assignment (2)
Polymorphism
Objectives
In this lab assignment, students will learn:
- How to design and define interface
- How to implement methods declared in interface
- How to implement polymorphism
Goals
In this lab assignment, students will demonstrate the abilities
to:
- Design and define interface
- Implement methods declared in interface
- Implement polymorphism
Grading
- Define interface (30 pts)
- Define base / derived classes (40 pts)
- Define tester program to implement polymorphism (30 pts)
In this lab you will create an interface inheritance hierarchy and
perform polymorphism in a tester program. The following is the
simplified UML for the hierarchy:
1. Create a Console Application Project called “InterfaceApp”.
2. Rename the “Module1.vb” as “IShapeTester.vb”
3. Add a class definition, modify it to an Interface named
“IShape” which has two members:
a. Function ToString() : String
b. Function CalculateArea() :Double
4. Add a class definition named “TwoDShape”, which is an
abstract base class, the following members should be included:
a. xValue: Integer ---- instance variable
b. yValue: Integer ---- instance variable
c. Sub New(Integer, Integer) ---- constructor
d. Property X():Integer ---- get and set xValue
e. Property Y():Integer ---- get and set yValue
f. Function CalculateArea():Double ---- “MustOverride”
function and implements IShape’s CalculateArea() function
g. Function ToString() ---- Overrides ToString() function in
Object and implements IShape’s method, display x and y
coordinate on the console window (use the following definition)
Public Overrides Function ToString() As String Implements
IShape.ToString
Return ("X-coordinate: " & vbTab & xValue & vbCrLf & "Y-
coordinate: " & vbTab & yValue)
End Function
5. Add a class definition named “Circle”, which inherits from
“TwoDShape” class and includes the following members:
a. radiusValue: Double --- instance variable, radius of a circle
b. Sub New(Integer, Integer, Double) --- constructor
c. Property Radius():Double --- get and set radiusValue
d. Function CalculateArea():Double ---- override and
implement the abstract method in base class Shape, calculate
area of a circle using:
radiusValue * radiusValue * 3.14
e. Function ToString() ----- use
Convert.ToString(Me.GetType()) to display the type of shape,
then display X and Y coordinates by calling base class’s
ToString() function, finally, display radius value on console
window (use the following definition)
Public Overrides Function ToString() As String
Return (Convert.ToString(Me.GetType()) & vbCrLf &
MyBase.ToString() & vbCrLf & "Radius: " & vbTab &
String.Format("{0:N2}", radiusValue))
End Function
6. Add a class definition named “Square”, which inherit from
“TwoDShape” class and include the following members:
a. sideValue: Double --- instance variable, side of a square
b. Sub New(Integer, Integer, Double) --- constructor
c. Property Side():Double --- get and set sideValue
d. Function CalculateArea():Double ---- override and
implement the abstract method in base class Shape, calculate
area of a square using:
sideValue * sideValue
e. Function ToString() ----- use
Convert.ToString(Me.GetType()) to display the type of shape,
then display X and Y coordinates by calling base class’s
ToString() function, finally, display side value on console
window (use the following definition)
Public Overrides Function ToString() As String
Return (Convert.ToString(Me.GetType()) & vbCrLf &
MyBase.ToString() & vbCrLf & "Side: " & vbTab &
String.Format("{0:N2}", sideValue))
End Function
7. Perform polymorphism in the Module “IShapeTester.vb”:
a. Declare 4 objects:
s1: a circle object ( center at (2, 4) and radius = 3.0)
s2: a square object (center at (1, 5) and side =
4.0)
s3: a circle object (center at(3, 6) and radius =
3.5)
s4: a square object (center at(6, 3) and side = 6.0)
b. Put these 4 objects into an IShape array
c. Use a “For” loop to traverse the array and let each object do
the following:
i. Call ToString() and let it display shape type, x and y
coordinates, radius or side value
ii. Call CalculateArea() and let it calculate area of a shape,
display the area on console window.
8. Here is the sample output window, your program should be
performed similarly like this:
<<Interface>>
IShape
+ Function ToString() As String
+ Function CalculateArea(): Double
TwoDShape
- xValue: Integer
- yValue: Integer
+ Sub New(Integer, Integer)
+ Property X():Integer
+ Property Y():Integer
+ Function CalculateArea():Double
+ Function ToString():Strinbg
Square
sideValue: Double
+ Sub New(Integer, Integer,Double)
+ Property Side():Double
+ Function CalculateArea():Double
+ Function ToString():Strinbg
Circle
radiusValue: Double
+ Sub New(Integer, Integer,Double)
+ Property Radius():Double
+ Function CalculateArea():Double
+ Function ToString():Strinbg
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx

More Related Content

DOCX
Programasedo
PDF
Lab_910.pdf
DOCX
Comp 220 ilab 7 of 7
DOCX
Comp 220 ilab 7 of 7
DOCX
Comp 220 ilab 7 of 7
DOCX
Comp 220 ilab 7 of 7
PDF
Software Development - Thinking Object's Way
PPT
Csphtp1 10
Programasedo
Lab_910.pdf
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
Software Development - Thinking Object's Way
Csphtp1 10

Similar to CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx (20)

PPT
3433 Ch10 Ppt
PPT
Csphtp1 09
PPT
3433 Ch09 Ppt
PDF
Assignment for Factory Method Design Pattern in C# [ANSWERS]
DOCX
Ecet 370 week 1 lab
PDF
Refactoring
PDF
Object oriented programming
PDF
Class program and uml in c++
PDF
Visual Basic(Vb) practical
PDF
SOLID Principles
PDF
C++ L11-Polymorphism
PDF
Ec oop f90
PPTX
Polymorphism
PDF
Clean Code Tutorial maXbox starter24
PPT
1207028 634528828886611250
DOCX
Write a new version of the area calculation program that makes use of.docx
ODP
Expression problem
PDF
JRuby: Apples and Oranges
PDF
Please follow the cod eand comments for description CODE #incl.pdf
PPT
Visual basic intoduction
3433 Ch10 Ppt
Csphtp1 09
3433 Ch09 Ppt
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Ecet 370 week 1 lab
Refactoring
Object oriented programming
Class program and uml in c++
Visual Basic(Vb) practical
SOLID Principles
C++ L11-Polymorphism
Ec oop f90
Polymorphism
Clean Code Tutorial maXbox starter24
1207028 634528828886611250
Write a new version of the area calculation program that makes use of.docx
Expression problem
JRuby: Apples and Oranges
Please follow the cod eand comments for description CODE #incl.pdf
Visual basic intoduction
Ad

More from faithxdunce63732 (20)

DOCX
Assignment DetailsScenario You are member of a prisoner revie.docx
DOCX
Assignment DetailsScenario You are an investigator for Child .docx
DOCX
Assignment DetailsScenario You are a new patrol officer in a .docx
DOCX
Assignment DetailsScenario Generally, we have considered sexual.docx
DOCX
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
DOCX
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
DOCX
Assignment DetailsPart IRespond to the following.docx
DOCX
Assignment DetailsPlease discuss the following in your main post.docx
DOCX
Assignment DetailsPennsylvania was the leader in sentencing and .docx
DOCX
Assignment DetailsPart IRespond to the followingReview .docx
DOCX
Assignment DetailsPart IRespond to the following questio.docx
DOCX
Assignment DetailsPart IRespond to the following questions.docx
DOCX
Assignment DetailsOne thing that unites all humans—despite cultu.docx
DOCX
Assignment DetailsMN551Develop cooperative relationships with.docx
DOCX
Assignment DetailsInfluence ProcessesYou have been encourag.docx
DOCX
Assignment DetailsIn this assignment, you will identify and .docx
DOCX
Assignment DetailsFinancial statements are the primary means of .docx
DOCX
Assignment DetailsIn this assignment, you will identify a pr.docx
DOCX
Assignment DetailsHealth information technology (health IT) .docx
DOCX
Assignment DetailsDiscuss the followingWhat were some of .docx
Assignment DetailsScenario You are member of a prisoner revie.docx
Assignment DetailsScenario You are an investigator for Child .docx
Assignment DetailsScenario You are a new patrol officer in a .docx
Assignment DetailsScenario Generally, we have considered sexual.docx
Assignment DetailsPower’s on, Power’s Off!How convenient is.docx
Assignment DetailsIn 1908, playwright Israel Zangwill referred to .docx
Assignment DetailsPart IRespond to the following.docx
Assignment DetailsPlease discuss the following in your main post.docx
Assignment DetailsPennsylvania was the leader in sentencing and .docx
Assignment DetailsPart IRespond to the followingReview .docx
Assignment DetailsPart IRespond to the following questio.docx
Assignment DetailsPart IRespond to the following questions.docx
Assignment DetailsOne thing that unites all humans—despite cultu.docx
Assignment DetailsMN551Develop cooperative relationships with.docx
Assignment DetailsInfluence ProcessesYou have been encourag.docx
Assignment DetailsIn this assignment, you will identify and .docx
Assignment DetailsFinancial statements are the primary means of .docx
Assignment DetailsIn this assignment, you will identify a pr.docx
Assignment DetailsHealth information technology (health IT) .docx
Assignment DetailsDiscuss the followingWhat were some of .docx
Ad

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PDF
Complications of Minimal Access Surgery at WLH
PPTX
master seminar digital applications in india
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
TR - Agricultural Crops Production NC III.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
Complications of Minimal Access Surgery at WLH
master seminar digital applications in india
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Microbial disease of the cardiovascular and lymphatic systems
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx

  • 1. CSC139 Chapter 10 Lab Assignment (2) Polymorphism Objectives In this lab assignment, students will learn: - How to design and define interface - How to implement methods declared in interface - How to implement polymorphism Goals In this lab assignment, students will demonstrate the abilities to: - Design and define interface - Implement methods declared in interface - Implement polymorphism Grading - Define interface (30 pts) - Define base / derived classes (40 pts) - Define tester program to implement polymorphism (30 pts) In this lab you will create an interface inheritance hierarchy and perform polymorphism in a tester program. The following is the simplified UML for the hierarchy: 1. Create a Console Application Project called “InterfaceApp”.
  • 2. 2. Rename the “Module1.vb” as “IShapeTester.vb” 3. Add a class definition, modify it to an Interface named “IShape” which has two members: a. Function ToString() : String b. Function CalculateArea() :Double 4. Add a class definition named “TwoDShape”, which is an abstract base class, the following members should be included: a. xValue: Integer ---- instance variable b. yValue: Integer ---- instance variable c. Sub New(Integer, Integer) ---- constructor d. Property X():Integer ---- get and set xValue e. Property Y():Integer ---- get and set yValue f. Function CalculateArea():Double ---- “MustOverride” function and implements IShape’s CalculateArea() function g. Function ToString() ---- Overrides ToString() function in Object and implements IShape’s method, display x and y coordinate on the console window (use the following definition) Public Overrides Function ToString() As String Implements IShape.ToString Return ("X-coordinate: " & vbTab & xValue & vbCrLf & "Y- coordinate: " & vbTab & yValue) End Function 5. Add a class definition named “Circle”, which inherits from “TwoDShape” class and includes the following members: a. radiusValue: Double --- instance variable, radius of a circle b. Sub New(Integer, Integer, Double) --- constructor c. Property Radius():Double --- get and set radiusValue
  • 3. d. Function CalculateArea():Double ---- override and implement the abstract method in base class Shape, calculate area of a circle using: radiusValue * radiusValue * 3.14 e. Function ToString() ----- use Convert.ToString(Me.GetType()) to display the type of shape, then display X and Y coordinates by calling base class’s ToString() function, finally, display radius value on console window (use the following definition) Public Overrides Function ToString() As String Return (Convert.ToString(Me.GetType()) & vbCrLf & MyBase.ToString() & vbCrLf & "Radius: " & vbTab & String.Format("{0:N2}", radiusValue)) End Function 6. Add a class definition named “Square”, which inherit from “TwoDShape” class and include the following members: a. sideValue: Double --- instance variable, side of a square b. Sub New(Integer, Integer, Double) --- constructor c. Property Side():Double --- get and set sideValue d. Function CalculateArea():Double ---- override and implement the abstract method in base class Shape, calculate area of a square using: sideValue * sideValue e. Function ToString() ----- use Convert.ToString(Me.GetType()) to display the type of shape, then display X and Y coordinates by calling base class’s ToString() function, finally, display side value on console
  • 4. window (use the following definition) Public Overrides Function ToString() As String Return (Convert.ToString(Me.GetType()) & vbCrLf & MyBase.ToString() & vbCrLf & "Side: " & vbTab & String.Format("{0:N2}", sideValue)) End Function 7. Perform polymorphism in the Module “IShapeTester.vb”: a. Declare 4 objects: s1: a circle object ( center at (2, 4) and radius = 3.0) s2: a square object (center at (1, 5) and side = 4.0) s3: a circle object (center at(3, 6) and radius = 3.5) s4: a square object (center at(6, 3) and side = 6.0) b. Put these 4 objects into an IShape array c. Use a “For” loop to traverse the array and let each object do the following: i. Call ToString() and let it display shape type, x and y coordinates, radius or side value ii. Call CalculateArea() and let it calculate area of a shape, display the area on console window. 8. Here is the sample output window, your program should be performed similarly like this: <<Interface>> IShape + Function ToString() As String
  • 5. + Function CalculateArea(): Double TwoDShape - xValue: Integer - yValue: Integer + Sub New(Integer, Integer) + Property X():Integer + Property Y():Integer + Function CalculateArea():Double + Function ToString():Strinbg Square sideValue: Double
  • 6. + Sub New(Integer, Integer,Double) + Property Side():Double + Function CalculateArea():Double + Function ToString():Strinbg Circle radiusValue: Double + Sub New(Integer, Integer,Double) + Property Radius():Double + Function CalculateArea():Double + Function ToString():Strinbg