Sale!

 Assignment 3 Polymorphism

$30.00

Category:

Description

5/5 - (3 votes)

Object-Oriented Programming Languages
Question 1 (30 points).  Polymorphism is one cornerstone of Object-Oriented Programming. This question has the following setting of a polymorphism problem:

Class Animal:

Attributes:

Leg, an integer, private      à The number of legs

 

Behaviors:

Animal(), public   à default constructor sets Leg=4

talk(), public   à Takes no input parameter, prints out a string

“Animal can’t talk !”

 

End Class Animal

 

Class Cow:

 

Attributes:

An object of type Cow has all the attributes of an

object of type Animal

 

Behaviors:

An object of type Cow has all the behaviors of an object of

type Animal

 

Additionally, Cow overrides the talk( ) method:

talk(), public   à Takes no input parameter, prints out a string

“Moo !”

 

End Class Cow

Class Pig:

 

Attributes:

An object of type Pig has all the attributes of an

object of type Animal

 

Behaviors:

An object of type Pig has all the behaviors of an object of

type Animal

 

Additionally, Pig overrides the talk( ) method:

talk(), public   à Takes no input parameter, prints out a string

“Grunt !”

 

End Class Pig

 

Class Snake:

 

Attributes:

An object of type Snake has all the attributes of an

object of type Animal

 

Behaviors:

An object of type Snake has all the behaviors of an object of

type Animal

 

 

End Class Snake

 

 

In a main( ) routine,  do the following things:

·           Create a one-dimension array of type Animal with four elements

·           Create an instance of each class type, i.e., Animal, Cow, Pig and Snake

·           Assign each instance to an element of the array we just created

·           Do a loop over each array element, and call the talk( ) method of that array element to demonstrate how the polymorphism works.

 

This question consists of four following sub-questions:

 

(a)  Implement the above polymorphism problem in C++, and test the results in you main( ) routine;
(b)  Implement the above polymorphism problem in Java, and test the results in you main( ) routine;
(c)  Implement the above polymorphism problem in C#, and test the results in you Main( ) routine;
(d)  Summarize what are your findings about the polymorphism of C++, Java, and C#. What are the differences between them ?
 

Question 2 (30 points)
Inheritance is another corner stone of Object-Oriented Programming. Multiple inheritance or its equivalent counterpart frequently appears in commercial codes. This question has the following setting of a multiple inheritance problem:
Class Pianist:

 

Attributes:

name, a string, private      à The name of pianist

 

Behaviors:

Pianist(string x), public   à default constructor sets name=x

pianoPlay(), public   à Takes no input parameter, prints out a string “Play a piano !”

 

End Class Pianist

 

 

Class Violinist:

 

Attributes:

Not available

 

Behaviors:

violinPlay(), public   à Takes no input parameter, prints out a string “Play a violin !”

 

End Class Violinist

 

Class Musician:

 

Attributes:

An object of type Musician has all the attributes of an

object of type Pianist

 

Behaviors:

An object of type Musician has all the behaviors of an object of

type Pianist and type Violinist

 

Additionally, Musician overrides the talk( ) method:

sing(), public   à Takes no input parameter, prints out a string

“Sing a song !”

 

End Class Musician

 

In a main( ) routine,  do the following things:

·           Create an instance of type Musician

·           Perform the sing( ) action of this instance

·           Perform pianoPlay( ) action of this instance

·           Perform violinPlay( ) action of this instance.

 

This question consists of four following sub-questions:

 

(a)  Implement the above inheritance problem in C++, and test the results in you main( ) routine;
(b)  Implement the above inheritance problem in Java, and test the results in you main( ) routine;
(c)  Implement the above inheritance problem in C#, and test the results in you Main( ) routine;
(d)  Summarize what are your findings about the multiple inheritance of C++, Java, and C#. What are the similarity and difference between them ?
Hint:  for sub-questions (b) and (c), you may consider using interface.

 

Question 3 (30 points)

Information hiding is a unique feature of Object-Oriented Programming. Sometimes, we may want to break the regular access rule via friend, package, internal, or inner class. This question has the following setting of an information hiding problem:
Class Bank:

 

Attributes:

name, a string, private          à The name of bank

securityInfo, a string, protected    à The security information

of bank

 

Behaviors:

display(), public   à Takes no input parameter, prints out a string “This is a bank !”

 

End Class Bank

 

Class Manager:

 

Attributes:

id, an integer, private          à The id number of manager

 

Behaviors:

display(), public   à Takes no input parameter, prints out a string “I am a manager !”

securityAccess(Bank x), public   à Takes an input parameter of type Bank, prints out a string: “Security Information is : ” + x.securityInfo

 

End Class Manager

 

In a main( ) routine,  do the following things:

·           Create an instance of type Bank

·           Create an instance of type Manager

·           Call securityAccess via the instance of Manager and Bank

 

This question consists of four following sub-questions:

 

(a)  Implement the above problem in C++ via friend class, and test the results in you main( ) routine;
(b)  Implement the above problem in Java via the same package, and test the results in you main( ) routine;
(c)  Implement the above problem in C# via internal, and test the results in you Main( ) routine;
(d)    Summarize what are your findings about breaking the conventional access rule. What are the similarity and difference between friend, package, and internal ?
 

 

Submission of Your Work:

 

(1)  The Word document should contain the following information