Docsity

Problems for Assignment 3 - Principles of Programming Languages | CS 181, Assignments of Computer Science

Toggle sidebar
bg1
CS181, Programming Languages
Assignment 3
(due: 11-3-2000)
For both problems submit only the source files (.java) using the WWW-
Turnin. Remember to include your name in the files!
Problem one:
Define a class hierarchy of shapes. Try to extract all common properties
of shapes into an abstract class called Shape. Specific shapes should be
subclasses of that class. Make your hierarchy tree at least three levels deep.
For example polygons have special properties, while circles and ellipsis have
different ones. Define at least two interfaces that may apply to shapes.
An example would be the Drawable interface, for objects which should im-
plement a draw method, or maybe the Selectable interface for objects that
can be selected when the mouse pointer lies inside them. Remember that
an interface may apply to any kind of an object and not only a shape. It is
more general than a class (for example Drawable could be implemented by
a string, to draw itself on the screen.)
Problem two:
Extending your solution to problem one, define the interface Printable
with a method ’public String toString()’. Make the Shape class implement
this interface (just the Shape class, not all subclasses!) Every shape now
should implement this method and return a string with useful information
about itself. Create an application that initializes an array of Printable
shapes, like ’Printable[] shapes = {new Circle(0, 10), new Rectangle(1, 3,
10, 20)}’. Using a for loop on the array, call the ’toString’ method of each
element (’shapes[i].toString()’) and print what you get on standard output.
Check the output!
1

Partial preview of the text

Download Problems for Assignment 3 - Principles of Programming Languages | CS 181 and more Assignments Computer Science in PDF only on Docsity!

CS181, Programming Languages

Assignment 3

(due: 11-3-2000)

For both problems submit only the source files (.java) using the WWW- Turnin. Remember to include your name in the files!

Problem one: Define a class hierarchy of shapes. Try to extract all common properties of shapes into an abstract class called Shape. Specific shapes should be subclasses of that class. Make your hierarchy tree at least three levels deep. For example polygons have special properties, while circles and ellipsis have different ones. Define at least two interfaces that may apply to shapes. An example would be the Drawable interface, for objects which should im- plement a draw method, or maybe the Selectable interface for objects that can be selected when the mouse pointer lies inside them. Remember that an interface may apply to any kind of an object and not only a shape. It is more general than a class (for example Drawable could be implemented by a string, to draw itself on the screen.)

Problem two: Extending your solution to problem one, define the interface Printable with a method ’public String toString()’. Make the Shape class implement this interface (just the Shape class, not all subclasses!) Every shape now should implement this method and return a string with useful information about itself. Create an application that initializes an array of Printable shapes, like ’Printable[] shapes = {new Circle(0, 10), new Rectangle(1, 3, 10, 20)}’. Using a for loop on the array, call the ’toString’ method of each element (’shapes[i].toString()’) and print what you get on standard output. Check the output!