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:

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:

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: Box Surface Area

1.1 Learning objectives

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

1.6 Estimated development time

1.7 Exploration


2 Exercise 2: Investment

2.1 Learning objectives

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

2.6 Estimated development time


3 Exercise 3: Date Conversion

3.1 Learning objectives

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

3.6 Estimated development time


4 Exercise 4: Hypothenuse

4.1 Learning objectives

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

4.6 Estimated development time


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