Java I CS 142 Week 06 Assignment.docx

advertisement
Java I CS 142 Week 06 Assignment
This assignment is due in the OIS drop box by noon, Sunday October 30. Be sure to have your name in
a comment within the first few line of each class.
This program will use methods and classes. You will submit three files (all zipped up in a single file):
1. Week06.java – containing the main program (in a class named Week06)
2. Circle.java – containing the Circle class
3. Trapezoid.java – containing the Trapezoid class
Your main program should:
1. Display your name and the assignment (“Week06”) for ease while I’m grading, using either
console I/O or JOptionPane, your choice.
2. Have a loop which:
a. Asks the user whether a new object is to be a “Circle”, a “Trapezoid”, or “Done”.
b. Exit the loop if the user enters “Done”.
c. Then asks the user relevant information about the object:
i. If a Circle, ask the user for radius
ii. If a Trapezoid, ask the user:
1. Two or three arguments
2. If two arguments, ask the user for base and height, use the two-arg
constructor to create the trapezoid.
3. If three arguments, ask the user for b1, b2, and height, and use the
three-arg constructor.
d. Create an instance of the object using a constructor.
e. Determine the area of the object using a method named “.getArea()”
f. Display the area
g. If the object is a Circle, display the circumference and diameter.
3. When done with the loop, the program should display a count of how many objects were
created, the number of Trapezoids and Circles, and the total of all the areas.
The Circle class should:
1.
2.
3.
4.
5.
6.
Have a single double field (radius)
Have a constructor that accepts a single argument: radius.
Have a set and a get for radius named setRadius and getRadius, respectively.
Have a method named getArea() which returns a double, the area of the circle.
Have a method named getDiameter(), which returns the diameter of the circle.
Have a method named getCircumference(), which returns the circumference of the circle.
The Trapezoid class should:
1. Have three double fields (base1, base2, and height)
2. Have a constructor which accepts three double arguments in the above order.
3. Have a second constructor, with two arguments, base and height. It will construct a trapezoid
with both bases equal to the given argument.
4. Have set and get methods: setBase1, getBase1, setBase2, getBase2, setHeight and getHeight
5. Have a method named getArea(), which returns a double, the area of the trapezoid
Note: even though your test program may not use the sets and gets, I will have a test program that will!
The file should be “zipped up” in a file titled “Week06.zip”, and this single file should be submitted to
the drop box. Windows can create zip files (Ask in class for a demo if you don’t know how), and zipping
programs are available for Mac and Linux.
Sample output from my previous version:
Week 06 programming assignment for Dave
Enter 'Circle', 'Trapezoid', or 'Done':
Enter radius: 7.5
Area is: 176.7145867644257
Diameter is: 15.0
Circumference is: 47.12388980384685
Enter 'Circle', 'Trapezoid', or 'Done':
Enter base1: 4.5
Enter base2: 7
Enter height: 9.2
Area is: 52.9
Enter 'Circle', 'Trapezoid', or 'Done':
There were 2 figures.
The total area was 229.6145867644257
Circle:
Tapezoid:
Straayer
Circle
Trapezoid
Done
area = Math.PI*radius*radius
Circumference = 2*pi*radius
Diameter = 2*radius
area = ((base1 + base2)*height)/2
Download