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:
- All programs in a lab assignment are submitted through CourseMarker
before the submission deadline. Submissions through other means, such
as emails to your discussion leader or lecturer, and late submissions
are not accepted.
- You have made reasonable efforts on your works. This means that
you should not merely submit the skeleton programs provided with no
or little changes done, and you should not copy programs from others.
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:
- You may assume that all input data are correct.
Hence you do not need to do input data validation.
- You should use Scanner class on System.in for
input and System.out for output in your programs,
unless otherwise stated.
- You should use println() instead of print()
for the last line of output your program generates.
- You should not use multiple instances of Scanner class,
that is, you should not have more than one variable of this class.
(You will understand this point better later. At the moment it
is unlikely that you will even think of creating multiple instances
of Scanner class.)
- Test your programs thoroughly with your own input data before
you submit to CourseMarker. Do NOT use CourseMarker as a
debugging tool.
- You do not need to worry too much about your marks for
"Features" and "Typography" tools in CourseMarker. These will
be manually graded by your discussion leader.
Pay attention to "Dynamic Tests" though, as this is the result
of CourseMarker testing your program with unknown test
data. If your program does not pass the "Dynamic Tests", please
debug your program.
- Even if you get full marks for "Dynamic Tests" from CourseMarker
at your submission, it might not mean that your program is correct.
Your program will be further tested on more data.
- Avoid submitting your program in the last minute as the system tends to
be very slow at that time.
- Feedback on your programs will be sent to you after grading.
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
- Creating your own class (Textbook chapter 4).
- Writing an application program to test out the class created.
- Using stubs in incremental development.
- Using class constant.
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
- The skeleton programs are provided here:
MyCircle.java (incorrect
version) and
Lab2Ex1.java.
- The corrected program MyCircle.java is provided here:
MyCircle.java (correct
version). This program is provided only after 1 September 2009.
- Do not use if or other selection statement for this
exercise.
- Do not change the return type and parameter types of
the given methods in MyCircle.java.
- Do not include additional methods in MyCircle.java.
- You are to use a class constant provided by a standard class (for
you to find out).
- Refer to
Java API: java.lang.Math and
Java API: java.text.DecimalFormat.
1.6 Estimated development time
- The time below is an estimate of how much time we expect you to spend
on this exercise. If you need to spend a lot more time than this,
it is an indication that some help might be needed.
- Devising and writing the algorithm (pseudo-code): 10 minutes
- Translating pseudo-code into code: 10 minutes
- Typing in the code: 15 minutes
- Testing and debugging: 15 minutes
- Total: 50 minutes
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
- Creating your own class (Textbook chapter 4).
- Writing an application program to test out the class created.
- Using class constant.
- Using selection statement (Textbook chapter 5).
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:
- Create two Elevator objects with their default level at 1.
- Read two lines of instruction from the user. Each instruction contains the
following 3 integers:
- A number (1 or 2) to indicate one of the two elevator objects.
- The from-level that indicates which level a passenger presses the button
of that elevator. The elevator would have to travel from where it is
to that level to pick up that person. (The from-level could be the
same as the level where the elevator is currently at, in which case
there is no need for the elevator to move.)
- The to-level that indicates which level that passenger wants to go to.
(The to-level is different from the from-level, although there is no
need for you to check.)
- For example, if the first instruction is "2 5 8", the second elevator would
have to move from level 1 (its original position) to level 5 to pick up the
passenger, and then move further up to level 8. In total, it moves 7 levels.
- Report, for each elevator, first elevator followed by second elevator, the
time (in number of seconds) it takes to complete the journey, and its final
position.
- You may assume that the two instructions are given at the same time (two
persons press the lift buttons at the same time). If both
instructions refer to the same elevator, you are to execute the first instruction
before the second.
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
- The skeleton programs are provided here:
Elevator.java and
Lab2Ex2.java.
- As repetition statements (Chapter 5) are not covered, you do not
need to use them. However, if you choose to, it is fine too.
We put the number of instructions as 2 so that it isn't too tedious
without using a loop. As you learn more programming constructs, you
are able to simplify your programs using the more appropriate
programming constructs.
- As "using class constant" is listed as one of the learning objectives,
you are expected to use it, without which you will be penalised on
design.
- Note that although you need to read in two lines of input from the user,
you need create only one Scanner instance, not two. The same Scanner
object could be used to read multiple inputs. (Incidentally, CourseMarker
does not work if you create multiple instances of Scanner. However, here
we create one Scanner object not to avoid CourseMarker's inability, but
because that is the right way.)
- Although we also listed "selection statement" as one of the learning
objectives, it doesn't mean that you have to use it indiscriminately.
We expect a selection statement to be needed to identify the elevator,
but elsewhere, particularly in the computation part, if it can be
more neatly done without using selection statement, then that would be
preferred.
2.6 Estimated development time
- The time below is an estimate of how much time we expect you to spend
on this exercise. If you need to spend a lot more time than this,
it is an indication that some help might be needed.
- Devising and writing the algorithm (pseudo-code): 10 minutes
- Translating pseudo-code into code: 10 minutes
- Typing in the code: 15 minutes
- Testing and debugging: 25 minutes
- Total: 60 minutes
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.
- 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.
- 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:
- ElevatorGUI.class
(We don't provide the source code as you don't really need it. However, if you want to get hold of it to study, or
our class file does not work on your computer for some reason,
we can provide the source code ElevatorGUI.java on
request.)
- rectangle-image1.gif
- rectangle-image43.gif
To make use of the ElevatorGUI class, you need to add
the following in your Lab2Ex2.java program:
- At the start of your main method, add this statement:
ElevatorGUI start = new ElevatorGUI();
- Call start.moveElevator(elevatorID, currentLevel, fromLevel, toLevel);
at the appropriate places. This method displays the movement
of the elevator (identified by elevatorID) from
its current level (currentLevel) to the
level where passenger pressed the button (fromLevel),
then to the passenger's destination level (toLevel).
- 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