You are on page 1of 6

NATIONAL UNIVERSITY OF SINGAPORE

SCHOOL OF COMPUTING

Practical Exam 3 for Semester 1, AY2017/18


CS1010J — Programming Methodology

27 October 2017 Time Allowed: 1.5 hours


__________________________________________________________________________________________

INSTRUCTIONS TO CANDIDATES
1. This assessment paper consists of 2 exercises on 6 pages.
2. This is an open-book assessment. You may bring in any printed material, but not
electronic devices, including but not limited to laptops and thumb-drives.
3. You may bring in a calculator with you.
4. In line with the university rules, any form of communication with other students, or the
use of unauthorised materials is considered cheating and you are liable to the
disciplinary action.
5. Please switch off / silence your mobile phone and keep it out of view.
6. Please leave your student card on the desk in front of you throughout the PE.
7. Your computer has been logged in with a special account. Do not log off or try to log in
with your own NUSNET account.
8. You are to use DrJava installed on the lab computer to write your programs.
9. The skeleton programs have been planted into your working directory: c:\cs1010j.
10. The first 10 minutes of the PE is reserved for you to read questions and design
algorithms. You are not allowed to type your programs in this period of time.
11. You may assume that all input data are valid and no input validation is needed.
12. You are not allowed to use any Java language syntax out of the scope of this PE (e.g.
string or recursion). If in doubt, please check with the invigilator.
13. You are responsible for submitting your programs to CodeCrunch by the end of the PE.
A special password will be issued to you for your submission. Please submit your
programs to CodeCrunch frequently. Do not leave your submission to the last minute
in case of network congestion. You have 99 chances of submissions and only the last
submission will be graded.
14. At the end of the PE, the invigilators will log you out of your computer remotely after
issuing a warning.
15. Grading of the PE will take about 2 weeks.

ALL THE BEST!

CS1010J Programming Methodology - 1 of 6 - PE 3


Example Rough Marking Scheme for an Exercise of 50 Marks
1. Style: 10 marks
 Write program description and student name in the program header.
 Write a short method description for every method (except main).
 Consistent indentation and good naming of variables
2. Correctness: 40 marks
 Graders will manually go through your program and award method by
method.
3. Additional penalties:
 Program is not compilable: deduct 5 marks.
 Break programming restrictions: deduct 5 - 20 marks, depending on severity.

Exercise 1: Day of Week [50 marks]


Write a program DayOfWeek.java to report the day of the week for a given date. For
example, your program will print out “Friday” if the given date is 27 October 2017. You may
assume that the given date is between January 1, 2000 (Saturday) and January 1, 2020
(Wednesday).
Write in the skeleton program that has been loaded into your working directory. You are
required to complete the following two methods:
 int dayOfWeek(int date) that takes in a parameter date, which is a 8-digit
integer in the format yyyymmdd, where yyyy, mm and dd represent year, month and
day respectively. This method returns the day of the week for the given date as an
integer in the range 1 (for Monday) to 7 (for Sunday).
 boolean isLeapYear(int year) that returns true if year is a leap year, or
false otherwise. A year is a leap year if (1) it is divisible by 4 but not by 100; or (2) it is
divisible by 400. For example, 2004, 2016 and 2400 are leap years while 2006, 2017
and 2200 are not. FYI, a leap year has 366 days (i.e. 29 days in February).
You must NOT change the method headers given in the skeleton program. You are not
allowed to change the main() method as well.
Two sample runs are shown below with user's input shown in bold. (You may use the
calendar on your PC to test with more dates.)
Sample run #1
Enter date (yyyymmdd): 20000102
Sunday

Sample run #2
Enter date (yyyymmdd): 20171027
Friday

CS1010J Programming Methodology - 2 of 6 - PE 3


Exercise 2: Binary Image [50 marks]
A binary image is a 2D image where each pixel has a value of either 0 (representing white
color) or 1 (representing black color).
Write a program BinaryImage.java that reads in a n*n binary image (n > 1) and processes it
in one of the following three ways according to what the user chooses:
 Choice 1: transpose the binary image where all black pixels become white pixels and
white pixels become black pixels.
An example is given below.

5 * 5 binary image transposed binary image

You are to complete the following method for this task:


void transpose(int[][] image)

 Choice 2: shift the binary image to the left by one position where the left-most column
wraps around to the right.
An example is given below.

6 * 6 binary image binary image shifted to


the left by 1 position

You are to complete the following method for this task:


void shift(int[][] image)

CS1010J Programming Methodology - 3 of 6 - PE 3


 Choice 3: read a k*k binary pattern image (k > 1) and check how many times this pattern
image appears in the binary image.
In the first example below, there is 1 appearance of the pattern image in the binary
image as highlighted.

a match

3*3 pattern image


5 * 5 binary image

In the second example below, there are 3 matches of the pattern image in the binary
image as highlighted.

2*2 pattern image

5 * 5 binary image

You are to complete the following method for this task:


int match(int[][] image, int[][] pattern)

Write in the skeleton program BinaryImage.java that has been loaded into your working
directory. In addition to the above three methods, you are also required to complete the
main() method which reads a binary image, a user choice and invokes transpose(),
shift() or match() method according to user choice.
You must NOT change the method headers given in the skeleton program but may write
additional methods as necessary.
A printImage() method is given in the skeleton program. You should use it to print out
transposed/shifted image.
Four sample runs of the program are shown on the next two pages with user's input shown
in bold. For your convenience of testing, test inputs are also appended to the back of your
skeleton program.

CS1010J Programming Methodology - 4 of 6 - PE 3


Sample run #1
Enter the size of the binary image: 5
Enter the binary image:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Enter your choice:
(1) transpose
(2) shift
(3) match
1
Image after transposition:
0 1 1 1 1
1 0 1 1 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 0

Sample run #2
Enter the size of the binary image: 6
Enter the image:
1 1 1 0 0 0
1 0 0 0 0 0
1 1 1 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0
Enter your choice:
(1) transpose
(2) shift
(3) match
2
Image after shift:
1 1 0 0 0 1
0 0 0 0 0 1
1 1 0 0 0 1
0 0 0 0 0 1
0 0 0 0 0 1
0 0 0 0 0 1

CS1010J Programming Methodology - 5 of 6 - PE 3


Sample run #3
Enter the size of the binary image: 5
Enter the binary image:
1 1 1 0 0
1 0 0 0 0
1 1 1 0 0
1 0 0 0 0
1 0 0 0 0
Enter your choice:
(1) transpose
(2) shift
(3) match
3
Enter the size of the pattern image: 3
Enter the pattern image:
0 0 0
1 1 0
0 0 0
Pattern image appears 1 time(s)

Sample run #4
Enter the size of the binary image: 5
Enter the binary image:
0 0 1 0 0
0 1 0 0 0
0 1 0 1 0
1 0 1 0 1
0 0 0 1 0
Enter your choice:
(1) transpose
(2) shift
(3) match
3
Enter the size of the pattern image: 2
Enter the pattern image:
1 0
0 1
Pattern image appears 3 time(s)

=== END OF PAPER ===

CS1010J Programming Methodology - 6 of 6 - PE 3

You might also like