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!

  1. (15 pts total) Write a class called MathFuncs that represents a collection of mathematical functions.
    1. (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.
    2. (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.
    3. (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).
  2. (15 pts total) Write a class that represents rectangle, called Rectangle with the following specifications:
    1. (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.
    2. (8 pts) A method called getArea that takes no input parameters and returns a double. The area of the rectangle should be returned.
  3. (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:

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

  1. 15 pts total: class declaration and curly braces – 3 pts (spread across sub problems if necessary)
    1. 5 pts total:
      1. Input params correct - 1 pts
      2. Math and return value correct – 1 pts
    2. 5 pts total
      1. Input params correct - 1 pts
      2. Math and return value correct – 1 pts
    3. 5 pts total
      1. Input params correct – 1 pts
      2. Correct delegated call – 1 pts
      3. Correct return value – 1 pt
    4. 6 pts total spread across remaining points of parts a, b, c.: Test code for all classes
      1. exists - 3
      2. completeness - 3
  2. 15 pts total:: class declaration and curly braces – 3 pts (spread across sub problems if necessary)
    1. 7 pts: total:
      1. has fields – 2 pt
      2. Proper type on fields – 3 pts
    2. 8 pts total:
      1. method exists – 1 pt
      2. Proper return type – 1 pt
      3. No input parameters – 1 pt
      4. Proper math and return value – 1 pts.
    1. 6 pts total spread across remaining points of parts a and b: Test code for all classes
      1. exists - 3
      2. completeness - 3
  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