You are on page 1of 2

Computer Programming

05/12/2014
Lab 12
Problem 1: Design and implement a very simple superclass
Pet
with a method
makeNoise()
and two subclasses
Dog
and
Cat
that override this method. Prompt the user to
select the type of pet he or she prefers, create a proper object of
Dog
or
Cat
and invoke the
makeNoise()
method through the polymorphism and dynamic binding mechanism. A
partial client code looks like:
public static void main(String[] args) {
Scanner inp = new Scanner(Sstem!in)"
Sstem!out!println(#$%ic% tpe o& pet do ou pre&er'#)"
Sstem!out!print(#(nter d &or dogs and c &or cats) #)"
Pet p"
** to be &illed
**
p!makeNoise()"
inp!close()"
+
Two sample runs are given below:
Which type of pet do you prefer?
Enter d for dogs and c for cats: d
Woof! Woof!
Which type of pet do you prefer?
Enter d for dogs and c for cats: c
Meow! Meow
Problem 2: Download
,ab-./Problem.!0ip
from the course website. n!ip the file and
you will find three java files
1eometric2b3ect!3ava4 5riangle!3ava4
Circle!3ava
. Add these files into the
src
folder in your project. "efresh #right click$ in
%clipse and you should see the files listed in the
src
in the
de&ault package
in %clipse.
&e now have a superclass
1eometric2b3ect
and two subclasses
Circle
and
5riangle
.
se an
6rra,ist
to store randomly generated
Circle
or
5riangle
objects. &hile
generating these objects, assign radius with a random number between ' and ( for
Circle

(
Computer Programming
05/12/2014
object and three sides with a random number between ( and ) for
5riangle
object. &hile
adding the objects into the
6rra,ist
, you should reject those with area less than the
e*isting elements in the
6rra,ist
and only add those with area greater than the e*isting
elements. +top the addition when you have five elements in the
6rra,ist
. ,elow is a
sample run:
An ArrayList with five Circle or Triangle obects
Triangle: side! " !#$!%&'%%'!%(%!%$% side$ " !#()*!$'+$*'($'()( side, "
!#&('!,+%,&))(&*,* created on -un May !! !!:++:(, C-T $'!+
color: white and filled: false
Area: '#&(++(&%%$%,+$$'!
Circle: radius " '#(),)$!*$*+''(,)% created on -un May !! !!:++:(, C-T $'!+
color: white and filled: false
Area: '#&&*&%%'!(+!'')(+
Circle: radius " '#)$%)''((*!%(&$'% created on -un May !! !!:++:(, C-T $'!+
color: white and filled: false
Area: !#$+!,)+*!$&(%)*(%
Triangle: side! " !#)*)$%+!,+)%++%+, side$ " !#%&&(,&!)*$+&+,)$ side, "
!#)(%&+'$**&((+)$* created on -un May !! !!:++:(, C-T $'!+
color: white and filled: false
Area: !#,'!%!$!$(%+)+!)
Circle: radius " '#%*!+*&+,,**!%),$ created on -un May !! !!:++:(, C-T $'!+
color: white and filled: false
Area: $#,%(&)(+%&**'(%(
)

You might also like