CSCE 155 H
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 (Programming: 85 points; Puzzle: 15 points)
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 |
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.
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 |
There are 7 people, each making a statement about what today is. For example:
A: The day after tomorrow is Wednesday.
B: No, it’s Wednesday today.
C: You’re quite wrong; it’s Wednesday tomorrow.
D: Nonsense, today is neither Monday, Tuesday nor Wednesday
E: I’m quite sure yesterday was Thursday.
F: No, you’ve got it the wrong way around. Tomorrow is Thursday.
G: Anyway yesterday was not Saturday.
Now, only one of the above statements is true. Which one? What is today?
First, to write a program to identify the correct statement, you may want to think of today as D, tomorrow is D+1, the day after tomorrow is D+2, yesterday is D-1, and so on. Then, you may think of Monday as 1, Tuesday as 2, Wednesday as 3, Thursday as 4, Friday as 5, Saturday as 6, and Sunday as 7. So, re-writing the above statements yields:
A: D+2 = 3
B: D = 3
C: D+1 = 3
D: D ≠ 1; D ≠ 2; D ≠ 3
E: D-1 = 4
F: D+1 = 4
G: D-1 ≠ 6
So, after doing some logical elimination of the impossibilities, you may find out that the true statement is D’s statement. Today is actually Sunday.
Now, write an algorithm that will take any 7 groups of statements as input. Each group is in the following format:
S1; S2; …; SN
where each statement is in the following format.
D[+-][1-3] = [1-7]
The algorithm should find out which statement is true and then what today is. (Hint: Think about converting each statement to the following format first: D = [1-7].)
* Based on Emmet 1995.