Download presentation
Presentation is loading. Please wait.
Published byRosamond Long Modified over 8 years ago
1
CS1101: Programming Methodology Preparing for Practical Exam (PE) http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html
2
CS1101: Preparing for PE2 Important 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.
3
CS1101: Preparing for PE3 Important 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.
4
CS1101: Preparing for PE4 Other 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!
5
CS1101: Preparing for PE5 Task 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.
6
CS1101: Preparing for PE6 Task 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:
7
CS1101: Preparing for PE7 Task 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”; }
8
CS1101: Preparing for PE8 Task 1: Plurals (4/5) import java.util.*; public class Plurals { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while ( true ) { System.out.print("Enter a word: "); String s = scanner.nextLine(); int n = s.length(); if (n == 0) return; s = s.trim(); System.out.print( "The plural form of \"" + s + "\" is \"" ); +
9
CS1101: Preparing for PE9 Task 1: Plurals (5/5) if ( s.endsWith("y") ) { System.out.println( s.substring(0,n-1) + "ies\"." ); } else if ( s.endsWith("s") || s.endsWith("ch") || s.endsWith("sh") ) { System.out.println( s + "es\"." ); } else { System.out.println( s + "s\"." ); } +
10
CS1101: Preparing for PE10 Task 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
11
CS1101: Preparing for PE11 Task 2: Factorisation (2/4) Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); if (n > 0) System.out.print(n + " = 1"); else System.out.print(n + " = -1"); int factor = 2; while (n > 1) { if (n % factor == 0) { System.out.print(" * " + factor); n /= factor; } else factor++; } System.out.println(); Note: This code does not work for negative value of n. Correct it. +
12
CS1101: Preparing for PE12 Task 2: Factorisation (3/4) Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); start(n); Modular program. private static void start(int value) { if (value > 0) System.out.print(value + " = 1"); else System.out.print(value + " = -1"); int factor = 2; while (value > 1) { if (value % factor == 0) { System.out.print(" * " + factor); n /= factor; } else factor++; } System.out.println(); } But this method is not cohesive. (Why?) +
13
CS1101: Preparing for PE13 Task 2: Factorisation (4/4) Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); String answer = start(n); System.out.println(answer); private static String start(int value) { String ans = ""; if (value > 0) ans += value + " = 1"; else ans += value + " = -1"; int factor = 2; while (value > 1) { if (value % factor == 0) { ans += " * " + factor; n /= factor; } else factor++; } return ans; } Now this is cohesive. +
14
CS1101: Preparing for PE14 Task 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 > 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.
15
CS1101: Preparing for PE15 Task 3: Candles (2/3) Sample run: Enter n: 5 Enter k: 3 Number of candles Peter will burn = 7 New +
16
CS1101: Preparing for PE16 Task 3: Candles (3/3) import java.util.*; public class Candles { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); System.out.print("Enter k: "); int k = scanner.nextInt(); int candles = n; // initial number of candles int r = candles; // initial number of residuals while ( r >= k ) { int p = r/k; // new candles candles += p; // increment candles count r = r%k + p; // number of residuals for next round } System.out.println("Number of candles Peter will burn = " + candles); } + Other solutions possible. (For example, one involves only additiona and subtraction.)
17
CS1101: Preparing for PE17 Task 4: Pascal’s Triangle (1/5) In this problem, you are asked to generate Pascal’s Triangle. Pascal’s 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. 1 11 121 1331 14641
18
CS1101: Preparing for PE18 Task 4: Pascal’s Triangle (2/5) Write a program to generate a Pascal’s Triangle as shown. It should be observed that the next row of the Pascal’s triangle can be generated from the previous row. Thus, using a single-dimensioned array to store the values of the previous rows seems appropriate. 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Output for n (number of rows) = 6.
19
CS1101: Preparing for PE19 Task 4: Pascal’s 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 int[n]; printPascalTriangle(pascal); }
20
CS1101: Preparing for PE20 Task 4: Pascal’s Triangle (4/5) 1 0 0 0 0 0 n = 6 1 1 0 0 0 0 0 0 0121 0 1331...... +
21
CS1101: Preparing for PE21 Task 4: Pascal’s Triangle (5/5) // Prints the Pascal's triangle public static void printPascalTriangle(int[] arr) { arr[0] = 1; for (int row = 0; row < arr.length; row++) { for (int col = row; col > 0; col--) { arr[col] += arr[col-1]; } for (int col = 0; col <= row; col++) { System.out.print(arr[col] + " "); } System.out.println(); } +
22
CS1101: Preparing for PE22 Task 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 m th 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.
23
CS1101: Preparing for PE23 Task 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.
24
CS1101: Preparing for PE24 Task 5: Teams (3/3) Sample input: 6 3 Emily Hannah Emma Ashley Sarah Victoria Sample output: Emma Ashley Sarah Victoria Hannah Emily
25
CS1101: Preparing for PE25 More tasks at… Visit http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html
26
CS1101: Preparing for PE26 End of File
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.