[an error occurred while processing this directive] [an error occurred while processing this directive] Simple Java Exercises
Rice University
COMP 200
Elements of Computer Science
Fall 2005
Simple Java Exercises

To gain more familiarity with Java syntax and semantic, let's do the following exercises.

1. Write a class called RectArea to compute the area of a rectangle.  The class should have a method called area

NEW TERMINOLOGY: The name of a method, its parameters, the types of its parameters and the return value is called the signature of a method.  This is similar to what is called 'contract' in Scheme.

2. Write a class called CircleAra to compute the area of a circle.  The class should have a method called area.  What should its signature be?  Use the interactions pane to create two CircleAra instances (objects) to compute the area of a few circles of various sizes.

3. Write a class called ShapeArea to compute the area of common geometric shapes such as rectangles and circles.  The class should have two methods: one called rectArea to compute the area of a rectangle, the other called circleArea to compute the area of a circle.  By now, you should be able to determine what the signature of each of the methods should be.

Design Dilemma

Now suppose we are asked to compute the area of a right triangle.  What should we do?  Do we write another class called RightTriangleArea or do we add another method to the ShapeArea class?  What are the pros and cons?  This is a software engineering issue.  Note that from the point of view of mathematics, this is a non-issue.



© Dung X. Nguyen

Last revised 10/21/2005 10:02 AM

Maintained by the professor; see contact information on the course home page