Basic Object-Oriented Programming
Take-Home Lab #2 (CS1101 AY2009/10 Semester 1)
Date of release: 29 August 2009, Saturday, 7:00hr.
Submission deadline: 7 September 2009, Monday, 23:59hr.
School of Computing, National University of Singapore

0 Introduction

Take-home labs are graded to provide you with feedback. However, the marks do not contribute to your final grade. Instead, one mark (attempt-mark) is awarded for each assignment -- and this mark goes into the computation of your final grade -- on the following conditions:

This lab requires you to do two exercises. You are advised to review the material covered in chapters 1 through 5 and read Programming Style, Design and Marking Guidelines before attempting this lab assignment. You should not use syntax or constructs not yet covered in lectures; you shouldn't need to and may even be penalized for doing so (if the objective of the assignment is undermined). If in doubt, please ask for clarification in lecture or in the IVLE discussion forum.

Remember to spend some time thinking about the algorithm for each exercise and write out the pseudo-code on your own before you start typing in your program.

A word of advice: Code incrementally. Do not type in the whole program (especially when it is long) and then compile it. Instead, type your program in bits and pieces and compile it 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 your sit-in labs and practical exam.

Note that:

For more information on CourseMarker, please refer to CS1101 Labs page.

For this lab, the number of submissions is set to 10. Only your last submission will be graded.

If you have any questions, you may post your queries in the IVLE discussion forum. Do not post your programs (partial or complete) in the forum before the deadline!


1 Exercise 1: MyCircle

1.1 Learning objectives

1.2 Task statement

You have written Java application programs (also called client programs or driver programs) to make use of standard classes such as String, Math, DecimalFormat, Random, etc. Sometimes, you would need to create your own class, because the standard classes do not cater to your needs.

In this exercise, you will create your own MyCircle class by writing MyCircle.java and use this class in your application program Lab2Ex1.java. Hence, there are two Java programs involved.

A skeleton MyCircle.java program is given, which 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. Even if you are working alone, you may use stubs for certain parts of your code while you are focussing on other parts first. This is the idea of incremental development.

In a stub, the body of the method usually contains only a comment like '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 complete the MyCircle class, by correcting the erroneous method, and replacing the stub computeArea() with the actual code. In computeArea(), you are to use the pow() method and the π (pi) constant, both defined in the Math class. (Failure to follow this instruction will result in penalty.)

We also provide you a corrected version of MyCircle (see 1.5 Important notes below). This is so that students who got stuck with the erroneous MyCircle.java could replace it with the corrected version and then move on to work on Lab2Ex1.java. However, we urge that you work out MyCircle on your own and refer to the corrected version only for checking purpose.

In Lab2Ex1.java, you are to read a positive value (of type double) from the user and create a MyCircle instance (object) with that radius value. You are then required to compute and display the area of that MyCircle object correct to 2 decimal places (use DecimalFormat).

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

1.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first three lines (in green) below are commands issued to compile and run your Java programs if you are using in-line commands, for example, on UNIX. If you are not, you may ignore them.

$ javac MyCircle.java
$ javac Lab2Ex1.java
$ java Lab2Ex1
Enter radius: 32.1
Area = 3237.13

Another sample run:

$ java Lab2Ex1
Enter radius: 88
Area = 24328.49

1.4 Submission

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

1.5 Important notes

1.6 Estimated development time

1.7 For discussion and exploration

This might be your first attempt at writing a truly OOP application. We have not discussed many OOP issues yet, so do not worry if you come across advanced features used by some of your fellow course-mates. However, you must get the basics right. Consult your lecturer and discussion leader if you are unsure of how to create a simple class and what basic methods your should put into the class.

For those who have understood the basics and have done this exercise successfully, let's discuss a little further. Is it a good idea to add another data member area to the MyCircle class? What are the reasons for and against it?

Suppose we do add this data member area to the MyCircle 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 your MyCircle.java program? (Do not submit the modified program. This is meant for discussion only. You may present your modified code to your discussion leader in week 5.)


2 Exercise 2: Elevator

2.1 Learning objectives

2.2 Task statement

You are to design an Elevator class. Every elevator instance (object) has a data member called level which indicates which level (a positive integer) the elevator is currently at. A newly created Elevator instance has a default level of 1.

All Elevator instances have a common speed, which is an integer value representing the number of seconds an elevator takes to travel one level (up or down).

You are to write a program Elevator.java to define this class. You are also to write an application program Lab2Ex2.java to test out the Elevator class.

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

2.3 Sample run

Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first three lines (in green) below are commands issued to compile and run your Java programs if you are using in-line commands, for example, on UNIX. If you are not, you may ignore them.

$ javac Elevator.java
$ javac Lab2Ex2.java
$ java Lab2Ex2
Enter instruction: 2 5 8
Enter instruction: 1 9 7
Elevator 1 took 20 sec. and ended at level 7
Elevator 2 took 14 sec. and ended at level 8

Another sample run:

$ java Lab2Ex2
Enter instruction: 1 9 7
Enter instruction: 1 3 10
Elevator 1 took 42 sec. and ended at level 10
Elevator 2 took 0 sec. and ended at level 1

2.4 Submission

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

2.5 Important notes

2.6 Estimated development time

2.7 For discussion and exploration

There are a number of enhancements you could make to this exercise (please do not submit it to CourseMarker). You can try them on your own. Your discussion leader may also discuss them in class and ask you to demonstrate to him/her.

  1. As we have not covered Repetition statements, you find that we only cater to two instructions. After you have learned Repetiton statements, you may want your program to read in many instructions.

  2. We provide a class ElevatorGUI to display the elevators' movements graphically on screen. Your lecturer may show it to you during lecture. You need to download the following files: To make use of the ElevatorGUI class, you need to add the following in your Lab2Ex2.java program:
  3. You may combine both enhancements (1) and (2) above.


3 Deadline

The deadline for submitting all programs is 7 September 2009, Monday, 23:59hr. Late submissions will NOT be accepted.


Go back to Labs page.

Aaron Tan
Mon Jul 6 10:12:52 SGT 2009