|
Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- HW 01
|
See Schedule Page for due date!
Save all of your work in a directory called HW01 on your U: drive
(Owlnet account) inside a directory called Comp201 .
See the following web page for directions on
how to create a
project in DrJava.
At this stage, we recommend that you set DrJava's
Language Level to "Elementary". If you
have prior programming experience in Java, you may choose to to use the "Full
Java" Language level, but if you do, please see the section below on "How
to write a constructor".
Create a project called "HW01" that will contain all of
your work. Be sure to use the proper directory structure when creating
your project!
- (15 pts total) Write a class called MathFuncs
that represents a collection of mathematical functions.
- (5 pts) Write a method called add
that takes two doubles
as input parameters and returns a double.
The returned value should be the sum of the input values.
- (5 pts) Write a method called sqr
that takes one double
as input parameters and returns a double.
The returned value should be the square of the input value.
- (5 pts) Write a method called sumsqr
that takes two doubles
and returns a double.
The return value should be the sum of the squares of the input values.
Do not duplicate the above math! Use the methods that you have already
written. Note: an object can call one of its own methods simply by
using the method's name (i.e. no object name is necessary).
- (15 pts total) Write a class that represents rectangle, called Rectangle
with the following specifications:
- (7 pts) Two
double
fields called _width
and _height. (We will
use the convention that all field names begin with an underbar.)
- To instantiate a Rectangle
object, write something like "new Rectangle(3.4,
5.6)". Don't forget to assign the instance to a variable of
type Rectangle!
- If you are using "Full Java" language level, you
must write the constructor at this stage. See below.
- (8 pts) A method called
getArea
that takes no input parameters and returns a
double.
The area of the rectangle should be returned.
- (45 pts total) Repeat the previous problem but instead, create classes to
represent a square, a circle, and a triangle. You will need to modify the
number of fields and input parameters and their names.. Use names that
are appropriate for the process being performed! Hint: Math.PI
is a value that represents the number pi.
You are required to create
unit tests for all your code. Remember these points
when creating unit tests:
- Be complete in your tests. Be sure to test all
classes and all methods of each class.
- When input parameters are involved, be sure to test
not only "generic" values but also more pathological values, e.g.
- values = 0. If you have more than one
parameter be sure to test each one being zero and both being zero.
- value = 1
- value < 0
- multiple values equal to eachother
- Other potentially problematic values, such as
multiples or for trigonometric functions, factors of Pi.
75 pts total.
When you have completed your homework, zip up the
entire HW01 directory and submit the zipped file using the file upload
link on the Upload Page.
How to write a constructor
The Elementary language level does this for you behind the
scenes. You only need to write a constructor if you are using Full
Java language level
A constructor is a special method that whose name is the same as the class it is
defined in, and has no return type. Constructors are used to
initialize the object to make it ready for use. Most commonly, a
constructor will initialize the fields of the object with some given values.
For instance, in the Rectangle class, the _width and _height fields need to be
initialized. So we write a constructor that takes two values, the desired
width and height as input parameters and sets the internal fields to those
values:
Rectangle(double width, double height) {
_width = width;
_height = height;
}
The constructors for the square, triangle and circle classes
will be very similar, though their names will differ.
Grading Criteria
- 15 pts total: class declaration and curly braces – 3 pts (spread
across sub problems if necessary)
- 5 pts total:
- Input params correct - 1 pts
- Math and return value correct – 1 pts
- 5 pts total
- Input params correct - 1 pts
- Math and return value correct – 1 pts
- 5 pts total
- Input params correct – 1 pts
- Correct delegated call – 1 pts
- Correct return value – 1 pt
- 6 pts total spread across remaining points of
parts a, b, c.: Test code for all classes
- exists - 3
- completeness - 3
- 15 pts total:: class declaration and curly braces – 3 pts
(spread across sub problems if necessary)
- 7 pts: total:
- has fields – 2 pt
- Proper type on fields – 3 pts
- 8 pts total:
- method exists – 1 pt
- Proper return type – 1 pt
- No input parameters – 1 pt
- Proper math and return value – 1 pts.
- 6 pts total spread across remaining points of
parts a and b: Test code for all classes
- exists - 3
- completeness - 3
- 45 pts total. Criteria same as Prob. 2.
Last Revised
Thursday, 03-Jun-2010 09:50:22 CDT
©2008 Stephen Wong and Dung Nguyen