CS 1302 – HW 7

advertisement
CS 1302 – HW 7
This homework has 3 problems which deal with abstract classes (Ch 13) & exceptions (Ch 12)
Eclipse Reminder


You will create a Java Project in Eclipse with the name: hw7_lastNameFirstInitial
You will have three packages: prob1, prob2, prob3
Problem 1
Read problem 11.1 from the text (p.445). You have been provided the GeometricObject class which you should not
modify. Do the following:
a. Model this situation with a class diagram. Include a jpg or Word document that contains the diagram. You may
draw this by hand, neatly (scan or photo, make sure it is easily readable).
b. Create a prob1 package.
c. Write the Triangle class as described.
d. Write a tester class named TriangleTester as described. Additional notes on the tester:
1. It should produce a dialog like this:
Enter three sides: 2.2 3.3 4.4
Enter the color: purple
Enter a boolean value for filled: true
The area is 3.51
The perimeter is 9.90
Triangle: side1 = 2.2, side2 = 3.3, side3 = 4.4
2. The area and perimeter should be displayed with exactly two decimals as shown above.
Problem 2
Read problem 12.5 from the text (p.488). For this problem you can continue to work in the prob1 package above.
Do the following:
a. Create a prob2 package.
b. Write the IllegalTriangleException class as described. Add this feature to the class (if you can): the class should
store the three sides of an illegal triangle and provide a getter for each. The three sides should be supplied,
along with a message, when the exception is created.
1
c. Write the class TriangleWithException as described. Note:
1. Copy the Triangle class from Problem 1, rename, and then modify.
2. Either copy the GeometricObject class or import it.
e. Write a tester class named IllegalTriangleTester that creates one legal and one illegal triangle. The output should
look like this:
Legal triangle
The area is 1.33
The perimeter is 6.50
Triangle: side1 = 1.5 side2 = 2.0 side3 = 3.0
Illegal triangle
prob2.IllegalTriangleException: The sum of two sides is less than the other side
Side1: 1.0, Side2: 2.0, Side3: 3.0
Problem 3
Do the following:
a. Create a prob3 package.
b. Create a Foo class and copy this code exactly (you will not modify this code):
public class Foo {
int[] x;
public Foo() {}
public Foo(int[] x) {
this.x = x;
}
public int divide(int i, int j) throws NullPointerException,
ArrayIndexOutOfBoundsException {
if( x==null)
throw new NullPointerException("Array has not been initialized");
if( i>=x.length || j>=x.length)
throw new ArrayIndexOutOfBoundsException("Bad array index");
return x[i]/x[j];
}
}
2
c. Create a FooTester class. In main, we would like to create a Foo instance and call the divide method and catch
any exception that might be thrown. Thus, you will write code like this:
try{
Foo foo = new Foo( new int[]{0,44,5});
System.out.println( foo.divide(1, 22));
}
catch(...
catch(...
catch(...
When we test your program, we will replace the two lines in the try block with several test cases as shown
below. Make sure your code produces exactly the results shown below.
1. Test case 1:
try{
Foo foo = new Foo( new int[]{0,44,5});
System.out.println( foo.divide(1, 22));
}
catch(...
The output for this example would be:
Purple
java.lang.ArrayIndexOutOfBoundsException: Bad array index
2. Test case 2:
try{
Foo foo = new Foo( new int[]{0,44,5});
System.out.println( foo.divide(1, 0));
}
catch(...
The output for this example would be:
Green
java.lang.ArithmeticException: / by zero
3. Test case 3:
try{
Foo foo = new Foo(null);
System.out.println( foo.divide(1, 2));
}
catch(...
The output for this example would be:
Red
java.lang.NullPointerException: Array has not been initialized
3
Submission
Follow the directions in HW 1 to archive your work. Submit your zip file to Blazeview by the due date. The name of
your file should be: hw7_lastnameFirstIinitial.zip.
4
Download