Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS1101: Programming Methodology Preparing for Practical Exam (PE) http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html 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. CS1101: Preparing for PE 2 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. CS1101: Preparing for PE 3 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! CS1101: Preparing for PE 4 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. CS1101: Preparing for PE 5 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: <enter> CS1101: Preparing for PE 6 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”; } CS1101: Preparing for PE 7 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 \"" ); + CS1101: Preparing for PE 8 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\"." ); } } } } + CS1101: Preparing for PE 9 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 CS1101: Preparing for PE 10 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++; Note: This code does not } work for negative value of System.out.println(); n. Correct it. + CS1101: Preparing for PE 11 Task 2: Factorisation (3/4) Modular program. Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); start(n); 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++; But this method is not } cohesive. (Why?) System.out.println(); + } CS1101: Preparing for PE 12 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; CS1101: Preparing for PE } Now this is cohesive. 13 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. CS1101: Preparing for PE 14 Task 3: Candles (2/3) Sample run: Enter n: 5 Enter k: 3 Number of candles Peter will burn = 7 New New + CS1101: Preparing for PE 15 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; int r = candles; while ( r >= k ) int p = r/k; candles += p; r = r%k + p; } // initial number of candles // initial number of residuals { // new candles // increment candles count // number of residuals for next round System.out.println("Number of candles Peter will burn = " + candles); } } + CS1101: Preparing for PE Other solutions possible. (For example, one involves only additiona and subtraction.) 16 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 1 1 1 2 1 1 3 3 1 1 CS1101: Preparing for PE 4 6 4 1 17 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 1 1 1 1 CS1101: Preparing for PE Output for n (number of rows) = 6. 1 2 3 4 5 1 3 1 6 4 1 10 10 5 1 18 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); } CS1101: Preparing for PE 19 Task 4: Pascal’s Triangle (4/5) n=6 1 0 0 0 0 0 1 1 0 0 0 0 1 2 1 0 0 0 1 3 3 1 0 0 . . . + CS1101: Preparing for PE 20 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(); } } } + CS1101: Preparing for PE 21 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 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. CS1101: Preparing for PE 22 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. CS1101: Preparing for PE 23 Task 5: Teams (3/3) Sample input: 6 3 Emily Hannah Emma Ashley Sarah Victoria Sample output: Emma Ashley Sarah Victoria Hannah Emily CS1101: Preparing for PE 24 More tasks at… Visit http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html CS1101: Preparing for PE 25 End of File CS1101: Preparing for PE 26