Assignment 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CPE102/CSC102 Introduction to Programming Assignment 1

ASSIGNMENT 1

1. OBJECTIVE
The objective of this assignment is to practice your programming skills on branching, looping and
methods. You need to submit this assignment for grading.

2. LABORATORY
This lab is conducted in the Computing Lab 1 (N4-b1-10) in SCE.

3. EQUIPMENT
Hardware: Workstation/PC running under the Linux environment.
Software: text editor
Java 2 Platform

4. EXPERIMENT
First create a sub-directory called assign1 in your home directory. Do all the programming in this
assign1 sub-directory and leave all the programs in this assign1. Use the test cases suggested to
test your program and understand why these test cases are used. You do not need to do (extra)
error checking on input.

4.1 Write a Java program that reads in user input on birth year and prints out the corresponding
Chinese horoscope sign. The program allows a user to enquire about horoscope signs until a
sentinel value of “–1” is used to terminate the process. There are 12 signs in the Chinese
horoscope. The calculation of a horoscope sign is based on birth year. The 12 Chinese horoscope
signs and some of their corresponding years are given below:

Horoscope Sign Birth Year


Rat …1972, 1984, 1996…
Cow …1973, 1985, 1997…
Tiger …1974, 1986, 1998…
Rabbit …1975, 1987, 1999…
Dragon …1976, 1988, 2000…
Snake …1977, 1989, 2001…
Horse …1978, 1990, 2002…
Goat …1979, 1991, 2003…
Monkey …1980, 1992, 2004…
Rooster …1981, 1993, 2005…
Dog …1982, 1994, 2006…
Pig …1983, 1995, 2007…

A sample input and output session is given below:


Enter your birth year: 1973
Your horoscope sign: Cow

Page 1
CPE102/CSC102 Introduction to Programming Assignment 1

Enter your birth year: 2006


Your horoscope sign: Dog
Enter your birth year: –1
<Program terminates>

Important: Remember to name the source code of this program as P1.java and name the
compiled class code as P1.class (this is automatically done after compilation) inside the sub-
directory assign1.

Test cases & Expected outputs: use the sample inputs and outputs.
(20 marks)

4.2 Write a Java program that reads in a number of non-negative integers between 0 and 99
inclusively, one per line, and prints the word-equivalent of each input digit in reverse as the
output. The program should first read in a count of the number of input integers. The program
should also check whether the input integer exceeds 99. If the input integer exceeds 99, then the
program should display an appropriate error message and continue with the next input. Note that
an error input is also counted as one of the input integers. For this question, your program must
NOT use an array.

A sample input and output session is given below:


Enter number of inputs: 4
Enter input integer: 28
Output: eight two
Enter input integer: 155
Output: Error – input exceeds 99
Enter input integer: 45
Output: five four
Enter input integer: 7
Output: seven
<Program terminates>

Important: Remember to name the source code of this program as P2.java and name the
compiled class code as P2.class inside the sub-directory assign1.

Test cases & Expected outputs: use the sample inputs and outputs.
(20 marks)

4.3 Write a Java program that reads repeatedly an user input on height between 1 and 9, and then
displays a pyramid according to the value of height. For example, if the user input is 9, then the
following pyramid will be printed on the display:

1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109

The program reads the user input on height and prints the pyramid until a sentinel value of “–1” is
used to terminate the program.

Page 2
CPE102/CSC102 Introduction to Programming Assignment 1

Important: Remember to name the source code of this program as P3.java and name the
compiled class code as P3.class inside the sub-directory assign1.

Test cases: (1) height = 2; (2) heoght = 5; (3) height = 9.


Expected outputs:
(1) height = 2
1
232
(2) height = 5
1
232
34543
4567654
567898765
(3) height = 9 (same as the sample output)
(20 marks)

4.4 Write a Java program that reads in a non-negative integer number, and prints true if it is a
numerical palindrome and false if it is not. A numerical palindrome is a number whose digits read
the same forward and backward. You must NOT use array and String methods in this question.

The program reads the user input and prints the result until a sentinel value of “–1” is used to
terminate the program.

A sample input and output session is given below:


Enter integer number: 15951
Output: true
Enter integer number: 1551
Output: true
Enter input integer: 15931
Output: false
Enter input integer: –1
<Program terminates>

Important: Remember to name the source code of this program as P4.java and name the
compiled class code as P4.class inside the sub-directory assign1.

Test cases & Expected outputs: use the sample inputs and outputs.

(20 marks)

4.5 Write a Java program that reads x as a double-typed number, and prints the result according to the
following formula:

1 x3 1 * 3 x5 1* 3 * 5 x7 1 * 3 * ... * 13 x15
x+ * + * + * + ...... + *
2 3 2*4 5 2*4*6 7 2 * 4 * ... * 14 15
Assume that –1 <= x <= 1. Hence, the program does not need to do any error checking.

Important: Remember to name the source code of this program as P5.java and name the
compiled class code as P5.class inside the sub-directory assign1.

Page 3
CPE102/CSC102 Introduction to Programming Assignment 1

Test cases: 0.9, 0, -0.9

Expected outputs: x = 0.9 – result = 1.1132165759127661; x = 0 – result = 0;


x = -0.9 – result = -1.1132165759127661.

(20 marks)

5. REPORT
I. This assignment is due at 12.00 noon 11 October 2006 (Wednesday).

II. For the report, one needs to submit hard copies of his/her in the following format, and make
sure that their programs are correctly named and located in the right subdirectories:

For each part of the assignment, you need to write:


1. Program logic
- Discuss the logic of your programs and justify why you design your programs in this
way. For example, if you use a while loop in your assignment, state whether you
have used a counter-controlled while loop in your program and explain why you
think counter-controlled while loop is suitable for your program.
2. Program listings with comments of P1.java, P2.java, P3.java, P4.java and P5.java.
3. The typescript file(s) to show the results of the testing of your programs using the test
cases specified in this manual only.

6. MARKING SCHEME
The marking of each part of assignment (i.e. 20 marks) will be based on the following criteria:

1. Program logic (5 marks)


2. Java programs (15 marks)
- presentation of the Java codes (whether the program is properly indented);
- documentation of codes (whether the program is well commented to aid
understanding); and
- correctness of the program.
3. Oral interview – will be conducted by lab supervisor during your lab sessions. This aims
to verify that you understand the pseudocode, the program logic and the Java programs
that you have submitted. Marks will be deducted if you are found not understanding your
submitted work.

7. REFERENCE
[1] The textbook for CPE102/CSC102.

Page 4

You might also like