SWE 510: Object-Oriented Programming in JavaFAll 2010Muhammad Abdulla |
General Information | Textbooks | Schedule & Notes | Projects | Policies |
Homework 2
Due: 11:59 PM, Nov. 11, 2010 Reminders: Follow the submission instructions. Remember to comment your code! Total Points: 50 0) Geometry Solver. (50 pts) In this assignment you will write three classes that correspond to three geometric shapes: Circle, Rectangle, and Triangle. All three classes implement the same interface as follows: public Interface Shape { public String name(); public double perimeter(); public double area(); } The Circle, Rectangle, and Triangle classes will have constructors that take exactly one, two, and three arguments, respecitively. For Circle, the single argument will be its radius, for Rectangle, the two arguments will be its width and length, respectively, and for Triangle, the three arguments will be its three sides. To excercise your classes, you will have to provide a driver (main) class that asks the user for shape dimensions, and based on the user input, or on the number of dimensions given, to be more specific, should correctly decide which shape is requested, and print out shape information accordingly. For example, $ java Shapes Please enter shape dimensions: 3.0 5.0 4.0 Shape is: triangle Perimeter: 12.0 Area: 6.0 In the driver class, Shape will be used as the base type, and an object will be created from on the three classes for initialization based on the user input. In other words, you will be using dynamic binding for the methods exported by the Shape interface. You have to check the validity of the arguments for shape dimensions. For example, the dimensions should be between 1 and 3 (inclusive), the dimensions are positive decimal or integer numbers, and that no one side of the triangle is larger than or equal to the sum of the other two sides. You have to be sure to catch exceptions in reading and parsing user input. If the number of dimensions provided is zero or larger than 3, or any dimension is not recognized as a number, your program may print an appropriate error message and exit. You can decide where to do your input validation: in the driver or in the specific shape class constructors. Commonalities might be better tested in the driver, and shape-specific requirements might be better tested within the corresponding shape classes. For example, you may decide to test that dimensions are valid numbers in the driver, and check positiveness, and edge requirements (specifically for triangles) in the shape classes. If a shape determines that any dimension is not acceptable, then the shape constructor should throw an InvalidDimensionException, which you will implement implement, with appropriate error string. Based on the arguments given, your geometric shape classes should be able to correctly calculate the perimetors and areas of the geometric shapes that they correspond to. |
Date & Time
![]() ![]()
What is New?
|