CSCE 155

Homework Assignment 2:  OO Design

Assigned:  September 13, 2004

Due: Start of Class, September 27, 2004

 

Note: This assignment is to be completed individually - collaboration is strictly prohibited.

Points:  100 points

 

Problem Description

 

In the last homework assignment, you created a simple interactive guessing game using a single class. For this assignment, you are to design and implement three classes using an object-oriented approach. The main idea of object-oriented (OO) development is to design the system using one or more classes such that each class in the system is a well-defined module. In other words, each class has a well-defined set of functionality implemented through its own data members and methods that operate on its data. The advantages of OO design are 1) It decomposes the bit problem into smaller problems that are easier to solve; 2) The system is easier to maintain and debug; and 3) It increases reusability of classes. In an OO system, objects interact with one another through message passing to create a system that can support complex behaviors and extensive functionality.

 

For Assignment 2, complete the following steps:

1.      Download the archive (assignment2.tar) that contains the files you will need to complete this assignment.

2.      After you download this tar file, unpack it as you did in assignment 1.  Then you should find six new files:  Circle.class, Rectangle.class, IsoscelesTriangle.class, TestCircle.java, TestRectangle.java, TestIsocelesTriangle.java.

3.      The class files (Circle.class, Rectangle.class, IsoscelesTriangle.class) that you obtain from the tar file in activity 1, describe three basic shapes: a circle, a rectangle, and an Isosceles triangle.  These classes allow you to create objects that represent circles, rectangles, and Isosceles triangles. The shape objects provide methods that you can use to determine things such as the perimeter and area of the corresponding shape. Take a few minutes to understand what these classes do and how you use them.

4.      The three Java files are incomplete. You should complete them to calculate the perimeter and area of circle, rectangle and Isosceles triangle respectively. There are two ways to compute the perimeter and area of a shape: calculate by using mathematical formulas and get the return value by calling methods getPerimeter() and getArea() which are provided by all the three shape classes.

 

Following is an example of the output your program should generate for Assignment 2. The text in BOLD represents the data input by the user during execution. While your program should generate output that closely resembles the output of the demo program, it is not necessary to precisely match the spacing and formatting of the sample program. Keep in mind, however, that your output should be easy to understand and free from spelling, punctuation and grammatical errors.

 

Sample Output for TestCircle

Enter radius length:

4

Testing circle with radius = 4.0:

 

  Perimeter of my calculation = 25.13

  Perimeter obtained from getPerimeter() = 25.13

  Area of my calculation = 50.27

  Area obtained from getArea() = 50.27

 

 

Sample Output for TestRectangle

Enter rectangle length:

5

Enter rectangle width:

6

Testing rectangle with length = 5.0 and width = 6.0:

 

  Perimeter of my calculation = 22.00

  Perimeter obtained from getPerimeter() = 22.00

  Area of my calculation = 30.00

  Area obtained from getArea() = 30.00

 

 

Sample Output for TestIsocelesTriangle

Enter base for isosceles triangle:

3

Enter height for isosceles triangle:

4

Testing isosceles triangle with base = 3.0 and height = 4.0:

 

  Perimeter of my calculation = 11.54

  Perimeter obtained from getPerimeter() = 11.54

  Area of my calculation = 6.00

  Area obtained from getArea() = 6.00

 

 

Supplied Solution Components

You need not be concerned with how to read input from the keyboard – this part of the program has been implemented in the getInput() method supplied with the program. Note that import java.io.* is required at the head of the Main class to provide access to the methods used within getInput(). The other provided method is toDouble() which can convert String object to Double object and return its value. You can use getInput() to get user input and then convert this input to a double value.

You will need to use some methods and data values from java.lang.Math to complete this task. You might want to take a few minutes to review the documentation for this class before writing any code.

You might find the Pythagorean Theorem useful when writing TestIsocelesTriangle.java: A triangle is a right triangle if and only if the square of the length of the longest side is equal to the sum of the squares of the lengths of the two shorter sides:  c2 = a2 + b2.

Challenge – Extra Credit (10 points)

 

Copy your TestRectangle.java to TestRectangleRecord.java, and then add more codes to store the rectangle perimeter and area into a text file along with printing them to the screen. You might need to do the following:

 

1)      Open a file named “RectangleRecord.txt” in the current directory. If this file does not exist, create a new file.

2)      Append the your calculated perimeter, perimeter got from getPerimeter(), your calculated area, area got from getArea() to the file. You may use a space or comma delimited format.

3)      Close the “RectangleRecord.txt” file.

 

The following is a sample.

 

Sample RectangleRecord.txt

Testing rectangle with length = 5 and width = 6:

 

  Perimeter of my calculation = 22.00

  Perimeter obtained from getPerimeter() = 22.00

  Area of my calculation = 30.00

  Area obtained from getArea() = 30.00

 

 

Submission Procedure

 

This assignment is due Monday September 27, 2004 at the start of class. Your grade will be reduced by 10% for each day it is late. It is recommended you read the grading policy and grading guidelines on the course website for a complete explanation of how the assignments will be graded. Remember, your program should follow good programming style, include plenty of comments, and perform all of the functionality outlined above.

 

After completing the assignment, you must “handin” the following files on-line:

1.      Source file: TestCircle.java, TestRectangle.java, TestIsocelesTriangle.java, TestRectangleRecord.java

2.      Compiled file: TestCircle.class, TestRectangle.class, TestIsocelesTriangle.class, TestRectangleRecord.class

3.      Readme file: README.TXT

 

In addition, you must submit a stapled paper copy of your source code and README.TXT file.  Both these steps must be done by the start of the class on the day the assignment is due.