Programmer-Defined Classes
Lab #2 (CS1101 AY2007/8 Semester 1)
Date of release: 4 September 2007, Tuesday, 23:59.
Submission deadline: 12 September 2007, Wednesday, 23:59.
School of Computing, National University of Singapore

0 Introduction

This lab requires you to do two exercises (four programs) using classes defined by you. You are advised to review the material covered in chapters 1 through 4 and read Programming Style, Design and Marking Guidelines before attempting this lab assignment. You should not use syntax or constructs not covered in lectures; you shouldn't need to and may even be penalized for doing so (if the objective of the assignment is undermined). Unless a template is given, you should start each program afresh (ie. don't cut and paste and modify) so that you get sufficient practice with writing a complete Java program.

A word of advice: Code incrementally. Don't try to finish all parts of a program then compile it. Write your program in bits and pieces and compile frequently. Try to maintain a compilable program even while you are working on it. Submitting a compilable program that partially works is better than submitting an un-compilable one. This last point especially applies to the programming exams. Seriously, code incrementally.

The overall learning objectives of this lab are:

The learning objectives of the individual exercises accompany the task statements below.

The following topics have not been covered and hence you should not use them:

You may assume that all input data are correct.

You should use Scanner class on System.in for input and System.out for output in your programs, unless otherwise stated.

Test your programs thoroughly with your own input data before you submit to CourseMarker. Do NOT use CourseMarker as a debugging tool. For this lab, the number of submissions is set to 15. For subsequent labs, it may be reduced. Only your last submission will be graded.


1 Exercise 1: Circle object (40%)

1.1 Learning objectives

1.2 Task

You are given two codes:

The Lab2Ex1 class is used to test the Circle class. Study the two codes carefully.

The Circle class contains an erroneous method (for you to find out), and an incomplete method computeArea() represented as a stub. A stub is a method which is not finished, but has enough code to be compiled and work with other methods. When one or more programmers are writing code, they frequently use stubs as placeholders until the programmer responsible has finished the work. This is the idea of incremental development.

In a stub, the body of the method usually contains only a comment saying 'this is a stub'. If the method needs to return a value in order to compile successfully, the programmer will add a line to return a dummy value, such as zero.

You are to correct and complete the Circle class, by correcting the erroneous method, and replacing the stub computeArea() with the actual code.

You are also to modify Lab2Ex1.java so that:

Hence, you need to submit two programs for this exercise: Circle.java and Lab2Ex1.java.

1.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ javac Circle.java
$ javac Lab2Ex1.java
$ java Lab2Ex1
Enter radius: 32.1
Area = 3238.43

Another sample run:

$ java Lab2Ex1
Enter radius: 88
Area = 24338.29

1.4 Submission

Submit your programs Circle.java and Lab2Ex1.java through CourseMarker.

1.5 Important notes

1.6 For discussion

You may discuss this at your discussion session.

Suppose we add another data member area to the Circle class. How could we maintain data integrity on the objects? That is to say, whenever a circle object's radius is modified, its area should also be recomputed and updated accordingly. How would you modify the program? (Do not submit the modified program. This is meant for discussion. Show your modified code to your discussion leader in class.)


2 Exercise 2: MyPoint objects (60%)

2.1 Learning objectives

2.2 Task

You are given these codes:

This class defines a 2D point with x-coordinate and y-coordinate, both values of type double. We name this class MyPoint to distinguish it from the java.awt.Point class.

In MyPoint.java, you are to write the following:

In Lab2Ex2.java, you are to write the following:

Note: A better way to write this program is to introduce appropriate additional methods. For instance, a method to compute the distance between two points, and a method to compute the area. Static method could possibly be used too. However, this involves the use of the keyword this and static method which are to be covered later in the course, therefore you are not expected to use them. Students who know these features may use them, but you will not receive extra credits, so as to maintain fairness. Your discussion leader will show you how the better program can be written at the next discussion session.

2.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ javac MyPoint.java
$ javac Lab2Ex2.java
$ java Lab2Ex2
Enter seed: 100
p = (5.882, 47.000)
q = (9.091, 18.000)
Distance = 29.17696
Area = 93.04813

Another sample run:

$ java Lab2Ex2
Enter seed: -99.99
p = (-5.882, -46.990)
q = (-9.090, -17.990)
Distance = 29.17692
Area = 93.03882

2.4 Submission

Submit your programs MyPoint.java and Lab2Ex2.java through CourseMarker.

2.5 Important notes


3 Exercise 3: Extension of Exercise 2 (0%)

You do not need to submit this Exercise 3. This is only for your own practice.

Write a separate class MyRectangle whose members are two points representing the opposite corners of a rectangle. Write suitable methods for this class, and write a program that uses this class to compute the area of a rectangle.


4 Deadline

The deadline for handing in all programs is 12 September 2007, Wednesday, 23:59. Late submissions will NOT be accepted.

Start early. Do not wait till the eleventh hour and rush to meet the deadline. Do note that your computer's clock might not be exactly the same as the CourseMarker's, so do not wait till the last few minutes to submit your programs.

Seriously, start early.





tantc@comp.nus.edu.sg
Fri Aug 31 09:42:41 SGT 2007