Title: CS1101: Programming Methodology Preparing for Practical Exam (PE)
1CS1101 Programming MethodologyPreparing for
Practical Exam (PE)
2Important Notes (1/2)
- Have you been practising? In a simulated test
environment? - Manage your time well.
- Read the instructions carefully.
- Read the questions carefully. When in doubts,
ask. - Do not start coding right away. THINK about the
algorithm first. - Check that your algorithm works before you start
coding.
3Important Notes (2/2)
- If the problem seems hard, simplify it, break it
into smaller sub-problems. - In the worst case, solve a simplified version. A
partial program is better than no program. - Make sure that your programs can be compiled.
- Code incrementally.
- Test your programs thoroughly with your own test
data. - Your programs will be graded manually.
CourseMarker is just a tool.
4Other tips
- Two questions.
- Spend 20 30 minutes on thinking, algorithm,
etc. before you code. - Do not forget to add appropriate comments and
proper indentation in your codes. These are
graded as well. - Ask your lecturer! ?
5Task 1 Plurals (1/5)
- If a word ends in y, replace it with ies.
- If a word ends in s, ch or sh, add es.
- All other cases, just add s.
- Input consists of multiple lines. Each line
contains one word. Each word contains one or more
lowercase letters.
6Task 1 Plurals (2/5)
Enter a word dairy The plural form of "dairy" is
"dairies". Enter a word boss The plural form of
"boss" is "bosses". Enter a word dish The
plural form of "dish" is "dishes". Enter a word
bird The plural form of "bird" is "birds". Enter
a word ltentergt
7Task 1 Plurals (3/5)
while ( there is still input ) read str
n str.length() if (str ends with
y) print str.subString(0, n-1)
ies else if (str ends with s or ch
or sh) print str es else
print str s
8Task 1 Plurals (4/5)
9Task 1 Plurals (5/5)
10Task 2 Factorisation (1/4)
- Past PE question
- Time limit 30 minutes
- Write a program to read in a non-zero integer and
display the factorisation. - Examples Enter n 8 8 1 2 2 2
- Enter n -300
- -300 -1 2 2 3 5 5
- Enter n 77
- 77 1 7 11
11Task 2 Factorisation (2/4)
12Task 2 Factorisation (3/4)
13Task 2 Factorisation (4/4)
14Task 3 Candles (1/3)
Peter has n candles. He burns them one at a time
and carefully collects all unburnt residual wax.
Out of the residual wax of exactly k gt 1 candles,
he can roll out a new candle. How many candles
can Peter burn? The input contains two integers
giving the values of n and k. The output should
consist of just one integer giving the maximum
number of candles that Peter can burn.
15Task 3 Candles (2/3)
Sample run Enter n 5 Enter k 3 Number of
candles Peter will burn 7
16Task 3 Candles (3/3)
17Task 4 Pascals Triangle (1/5)
In this problem, you are asked to generate
Pascals Triangle. Pascals Triangle is useful in
many areas from probability to polynomials to
setting programming questions. It is a triangle
of integers with 1 on top and down the sides. Any
number in the interior equals the sum of the two
numbers above it. For example, here are the first
5 rows of the triangle.
18Task 4 Pascals Triangle (2/5)
Write a program to generate a Pascals Triangle
as shown. It should be observed that the next row
of the Pascals triangle can be generated from
the previous row. Thus, using a
single-dimensioned array to store the values of
the previous rows seems appropriate.
Output for n (number of rows) 6.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
19Task 4 Pascals Triangle (3/5)
import java.util. public class PascalTriangle
public static void main (String args)
Scanner scanner new Scanner(System.in)
System.out.print("Enter n ") int n
scanner.nextInt() int pascal new
intn printPascalTriangle(pascal)
20Task 4 Pascals Triangle (4/5)
n 6
1 0 0 0 0 0
21Task 4 Pascals Triangle (5/5)
22Task 5 Teams (1/3)
We need to split a group of n (an even number)
players into two teams. Here is what we do Line
up the players in a straight line. Stating from
the first player in the line, count the players
while reciting a song that consists of m words.
The mth player leaves the line and joins Team
A. Repeat the song, now starting with the player
who comes after the player who just left. The
next player who leaves the line joins Team
B. Whenever the end of the line is reached, the
counting resumes at the beginning of the
line. Repeat until all the players are assigned
to one of the two teams.
23Task 5 Teams (2/3)
The first 2 lines of the input are the numbers n
and m. The next n lines of the input are the
names of the players. The input name sequence
determines the order in which the players are
lined up. Your output should list all the players
in Team A followed by Team B in the order
which they joined the respective teams.
24Task 5 Teams (3/3)
Sample input 6 3 Emily Hannah Emma Ashley Sarah V
ictoria Sample output Emma Ashley Sarah Victoria
Hannah Emily
25More tasks at
Visit http//www.comp.nus.edu.sg/cs1101x/3_ca
/pe.html
26End of File