Basics
Take-Home Lab #1 (CS1101 AY2009/10 Semester 1)
Date of release: 22 August 2009, Saturday, 7:00hr.
Submission deadline: 31 August 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 four exercises.
You are advised to review the material covered in chapters 1 through 3 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.
The following topics have not been covered and hence you must not use them:
- Selection statements (example: if, switch).
- Repetition statements (example: for, while, do-while).
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 leaders.
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: Box Surface Area
1.1 Learning objectives
- Standard output (Textbook section 2.4.1), standard input (use Scanner class)
(Textbook section 2.4.4), and getting numerical input (Textbook section 3.5).
- The int data type. (Textbook section 3.1)
- Simple arithmetic computation on integers. (Textbook section 3.2)
1.2 Task statement
Write a program Box.java that reads three positive integers
representing the length, width and height of a box, and computes its
surface area. The surface area of a box is the total area of the six
faces of the box.
You may assume that the surface area of the box does not exceed the
maximum value representable in the int data type.
(What is that maximum value? See 1.7 Exploration
below.)
1.3 Sample run
Sample run using interactive input (user's input shown in
blue; output shown in
bold purple).
Note that the first two lines (in green) below) are
commands issued to compile and run your Java program if you are using in-line
commands, for example, on UNIX. If you are not, you may ignore them.
$ javac Box.java
$ java Box
Enter length: 12
Enter width : 3
Enter height: 10
Surface area = 372
1.4 Submission
Submit your program through CourseMarker.
1.5 Important notes
- The skeleton program is provided here:
Box.java.
When you click on Setup in CourseMarker, this skeleton program will
be loaded for you. (You are reminded to click Setup only once
for each exercise, as your program will be replaced by the skeleton
program each time you click Setup.)
- As mentioned in the Trial Lab, CourseMarker awards mark for correctness ONLY
if your output adheres to the given format. Hence,
do not add any other characters (such as blanks) that are not asked
for in your output, or change the spelling in your output.
The following outputs will all be graded as incorrect for
the above example:
- surface area = 372
(reason: "Surface" misspelt as "surface")
- Surface area=372
(reason: spaces around = sign missing)
- Surface area = 372
(reason: additional spaces before "Surface")
- Surface area = 372
(reason: too many spaces around = sign)
- Surface area = 372.
(reason: additional dot at end of line)
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): 5 minutes
- Translating pseudo-code into code: 5 minutes
- Typing in the code: 2 minutes (most of the code is already given in the skeleton program)
- Testing and debugging: 13 minutes
- Total: 25 minutes
1.7 Exploration
- What is the maximum possible integer value in Java? Your lecturer
will tell you in class, but you can do a little exploration on your own.
You can refer to the Java API documentation on the class Integer
Java API: java.lang.Integer and see that the maximum
value is given in the constant Integer.MAX_VALUE which has a value of
231 - 1. Do you know how to write a simple program to see that
value?
2 Exercise 2: Investment
2.1 Learning objectives
- Standard output (Textbook section 2.4.1), standard input (use Scanner class)
(Textbook section 2.4.4), and getting numerical input (Textbook section 3.5).
- The Math class. (Textbook section 3.6)
- The DecimalFormat class. (Textbook section 3.4)
2.2 Task statement
If you invest a principal amount of P dollars at R percent interest
rate compounded annually, in N years, your investment will grow to
P × ( 1 - (R / 100) N+1 )
1 - R / 100
dollars.
Write a program Invest.java that accepts positive integers
P, R and N and computes the amount of money earned
after N years, presented in two decimal places. You should use the
DecimalFormat class to format your output.
You may assume that the interest rate is always smaller than 100.
2.3 Sample run
Sample run using interactive input (user's input shown in
blue; output shown in
bold purple).
Note that the first two lines (in green) below are
commands issued to compile and run your Java program if you are using in-line
commands, for example, on UNIX. If you are not, you may ignore them.
$ javac Invest.java
$ java Invest
Enter principal amount: 100
Enter interest rate : 10
Enter number of years : 5
Amount = $111.11
Another sample run:
$ java Invest
Enter principal amount: 20000
Enter interest rate : 5
Enter number of years : 10
Amount = $21052.63
2.4 Submission
Submit your program through CourseMarker.
2.5 Important notes
- The skeleton program is provided here:
Invest.java.
When you click on Setup in CourseMarker, this skeleton program will
be loaded for you. (You are reminded to click Setup only once
for each exercise, as your program will be replaced by the skeleton
program each time you click Setup.)
- In the task statement, we refer to the variables as P, R,
N. However, in your program, you should name these variables
according to Java naming convention. For instance, P should be
called principal. Yes, the name is longer than just p
but code readability is important. Naming of variables is important and marks
will be deducted for poor naming.
- Refer to
Java API: java.lang.Math and
Java API: java.text.DecimalFormat.
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): 5 minutes
- Translating pseudo-code into code: 5 minutes
- Typing in the code: 10 minutes
- Testing and debugging: 20 minutes
- Total: 40 minutes
3 Exercise 3: Date Conversion
3.1 Learning objectives
- Standard output (Textbook section 2.4.1), and standard input (use Scanner class)
(Textbook section 2.4.4).
- String and its methods (Textbook section 2.4.2).
3.2 Task statement
There are two common formats used for dates. For example "December 25, 2005"
is more commonly used in the UK, whereas "25 December 2005" is more popular
with the Americans.
Write a program DateConvert.java that reads the UK date format
and outputs the equivalent American format. The input consists of the
month, a space, the day, a comma, a space, and the year.
Do NOT use the Date class, the SimpleDateFormat class or any other
date formatter class in your program.
3.3 Sample run
Sample run using interactive input (user's input shown in
blue; output shown in
bold purple).
Note that the first two lines (in green) below are
commands issued to compile and run your Java program if you are using in-line
commands, for example, on UNIX. If you are not, you may ignore them.
$ javac DateConvert.java
$ java DateConvert
Enter date in UK format: December 25, 2005
American format: 25 December 2005
3.4 Submission
Submit your program through CourseMarker.
3.5 Important notes
- The skeleton program is provided here:
DateConvert.java.
When you click on Setup in CourseMarker, this skeleton program will
be loaded for you. (You are reminded to click Setup only once
for each exercise, as your program will be replaced by the skeleton
program each time you click Setup.)
- Do not use the Date class, the SimpleDateFormat class or any other
date formatter class.
- Be aware of the indexing convention used in the substring
method. See
Java API: java.lang.String.
- You do NOT need to use the line.separator and
useDelimiter() method as shown in section 2.4.4 of
your textbook. A simple way to use another method in
the Scanner class. Hence, you need to look up
Java API: java.util.Scanner and
explore yourself.
In fact, it is important to cultivate the habit of
looking up the relevant API pages to find out more about
the classes you need for your programs.
3.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): 15 minutes
- Translating pseudo-code into code: 5 minutes
- Typing in the code: 10 minutes
- Testing and debugging: 20 minutes
- Total: 50 minutes
4 Exercise 4: Hypothenuse
4.1 Learning objectives
- Standard output (Textbook section 2.4.1), standard input (use Scanner class)
(Textbook section 2.4.4), and getting numerical input (Textbook section 3.5).
- The Math class. (Textbook section 3.6)
- The Random class. (To be covered in lecture.)
- The DecimalFormat class. (Textbook section 3.4)
- The typecast operator. (Textbook section 3.2)
4.2 Task statement
A right-angled triangle has a base, a height, and a longest side called the
hypothenuse. See figure below.
The relationship among the lengths of these three sides are given as:
hypothenuse2 = base2 + height2
Write a program Hypothenuse.java that generates two random integers
in the range [10, 1000] (this means from 10 to 1000 inclusive) representing the
base and height of a right-angled triangle, and computes the length of the hypothenuse.
The length of the hypothenuse should be reported first as a double value
correct to 2 decimal places (use the DecimalFormat class), and then as
an int value with the decimal portion discarded.
To generate the random integers, instead of using the random() method
in Math class, you are required to use the Random class
for this exercise.
To compute the hypothenuse, check the Math class to look for the best method to
use.
4.3 Sample run
Sample run using interactive input (user's input shown in
blue; output shown in
bold purple).
Note that the first two lines (in green) below are
commands issued to compile and run your Java program if you are using in-line
commands, for example, on UNIX. If you are not, you may ignore them.
$ javac Hypothenuse.java
$ java Hypothenuse
Base = 381
Height = 252
Hypothenuse = 456.80 or 456
Another sample run:
$ java Hypothenuse
Base = 601
Height = 294
Hypothenuse = 669.06 or 669
4.4 Submission
Submit your program through CourseMarker.
4.5 Important notes
- The skeleton program is provided here:
Hypothenuse.java.
When you click on Setup in CourseMarker, this skeleton program will
be loaded for you. (You are reminded to click Setup only once
for each exercise, as your program will be replaced by the skeleton
program each time you click Setup.)
- As the input data are generated randomly, CourseMarker would not be able
to perform Dynamic Tests on your program with pre-determined input data.
Hence, there are no Dynamic Tests for this exercise, but your discussion
leader will study your program and award marks for correctness.
- To generate the random integers for base and height, use the
Random class instead of the random() method in Math
class. The Math is still required though, for the computation
of hypothenuse.
- Refer to
Java API: java.text.DecimalFormat,
Java API: java.lang.Math and
Java API: java.util.Random.
4.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.
- Exploring Random and Math classes: 15 minutes
- Devising and writing the algorithm (pseudo-code): 10 minutes
- Translating pseudo-code into code: 5 minutes
- Typing in the code: 10 minutes
- Testing and debugging: 20 minutes
- Total: 60 minutes
5 Deadline
The deadline for submitting all programs is 31 August 2009, Monday,
23:59hr. Late submissions will NOT be accepted.
Go back to Labs page.
Aaron Tan
Sun Jun 21 10:39:40 SGT 2009