Assignment 1
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:
Page 1
CPE102/CSC102 Introduction to Programming Assignment 1
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.
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.
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.
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
(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:
6. MARKING SCHEME
The marking of each part of assignment (i.e. 20 marks) will be based on the following criteria:
7. REFERENCE
[1] The textbook for CPE102/CSC102.
Page 4