1 / 30

CS1101X: Programming Methodology Discussion on Past-Year’s Papers

CS1101X: Programming Methodology Discussion on Past-Year’s Papers. Information. www.comp.nus.edu.sg/~cs1101x/3_ca/exams.html contains links to Exam time-table Past-years’ papers How to prepare for exams. Ready?. Take out your pen and paper and get ready…. AY2005/6 Sem 1 Q4 (1/3).

kathleeno
Download Presentation

CS1101X: Programming Methodology Discussion on Past-Year’s Papers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1101X: Programming MethodologyDiscussion onPast-Year’s Papers

  2. Information • www.comp.nus.edu.sg/~cs1101x/3_ca/exams.html contains links to • Exam time-table • Past-years’ papers • How to prepare for exams

  3. Ready? • Take out your pen and paper and get ready…

  4. AY2005/6 Sem 1 Q4 (1/3) public static boolean lessThan (Point p, Point q) { if (p.x == q.x && p.y == q.y) returnfalse; int x, y; for (int i=0; true; i++) { // outer loop x = 0; y = i; for (int j=0; j <= i; j++) { // inner loop if (x == p.x && y == p.y) return true; if (x == q.x && y == q.y) return false; x++; y--; } } }

  5. AY2005/6 Sem 1 Q4 (2/3) If p = (3, 2) and q = (4, 5), what is the output of lessThan(p, q)? If p = (6, 3) and q = (4, 5), what is the output of lessThan(p, q)? Answers:

  6. AY2005/6 Sem 1 Q4 (3/3) Rewrite the lessThan method to remove all loops.

  7. AY2005/6 Sem 1 Q5 (1/4) int[][] number = new int[6][6];

  8. AY2005/6 Sem 1 Q5 (2/4) for (int i = 0; i < 6; i++) selectionSort( number[i] ); // At this point, fill in table (a) on the content // in the number array. int[] tempNumber = newint[6]; for (int j = 0; j < 6; j++) { for (int i = 0; i < 6; i++) tempNumber[i] = number[i][j]; selectionSort( tempNumber ); for (int i = 0; i < 6; i++) number[i][j] = tempNumber[i]; } // At this point, fill in table (b) on the content // in the number array.

  9. AY2005/6 Sem 1 Q5 (3/4) Table (a) Table (b)

  10. AY2005/6 Sem 1 Q5 (4/4) static boolean search (int key, int[][] table, int startX, int startY, int endX, int endY) { if (startX > endX || startY > endY) return _________ ; int midX = (startX + endX) / 2; int midY = (startY + endY) / 2; if (key == table[midX][midY]) return __________ ; if (key < table[midX][midY] ) return ____________________________________________ ; else return ____________________________________________ ; }

  11. AY2004/5 Sem 1 Q1 (1/3) 1: public class dice { 2: 3: int[] freq; 4: 5: public Dice() { 6: freq = new int[7]; // freq[0] is not used 7: } 8: 9: public void getFreq(int face) { 10: return freq[face]; 11: } 12: 13: public void setFreq(int face, int value) { 14: freq[face] = value; 15: }

  12. AY2004/5 Sem 1 Q1 (2/3) 16: 17: public static void main(String[] args) { 18: Dice die = new Dice(); 19: initFreq(); 20: printFreq(); 21: } 22: 23: public void initFreq() { 24: Random num = new Random(); 25: int face = 0; 26: for (int roll = 1; roll == 6000; roll++) { 27: int face = 1 + num.nextInt() * 6; 28: setFreq(face, getFreq(face)); 29: } 30: }

  13. AY2004/5 Sem 1 Q1 (3/3) 31: 32: public void printFreq() { 33: for (int i = 1; i <= 6; ++i) { 34: System.out.println ("Face " + i + 35: " has frequency " + getFreq(face)); 36: } 37: } 38: }

  14. AY2004/5 Sem 1 Q2 Output:

  15. AY2004/5 Sem 1 Q3(a)

  16. AY2004/5 Sem 1 Q3(b)

  17. AY2004/5 Sem 1 Q4(d) Covered in lecture #10 Array, slide 19. Adjust the variable names accordingly. public static int numCoins(int amount) { int[] denominations = {100, 50, 20, 10, 5, 1}; int coins = 0; while (amount > 0) { } return coins; }

  18. AY2004/5 Sem 1 Q5(a) (1/2) Part (a): Complete the equals() method. // Ball import java.awt.*; publicclass Ball { privatedouble radius; private Color color; public Ball() { this(10.0, Color.BLUE); } public Ball(double r, Color c) { setRadius(r); setColor(c); } public void setRadius(double r) { radius = r; } public void setColor(Color c) { color = c; } public double getRadius() { return radius; } public Color getColor() { return color; } public boolean equals(Object v) { // to be filled in } }

  19. AY2004/5 Sem 1 Q5(a) (2/2) Part (a): Part (a): Complete the equals() method.

  20. AY2004/5 Sem 1 Q5(b) (1/3) Extend Ball class to BallOn2D, with radius, colour and centre. Centre is an object of class Point. private Point centre; public Point getCentre() { return centre; } public void setCentre(Point p) { centre = p; } Write (i) two constructors; (ii) a main method that creates two BallOn2D objects; and (iii) the toString() method.

  21. AY2004/5 Sem 1 Q5(b) (2/3)

  22. AY2004/5 Sem 1 Q5(b) (3/3)

  23. AY2004/5 Sem 1 Q5(c) (1/2) [Recursion] A prime number is a positive integer whose only distinct factors are 1 and itself. Write a boolean method isPrime(int n) that returns true if n is prime, or false otherwise. This method should simply call another auxiliary method, which you need to write, that employs recursion. [Only partial credit will be given if you change the formal parameter list of the isPrime(int n) method.]

  24. AY2004/5 Sem 1 Q5(c) (2/2) isPrime(int n)

  25. AY2004/5 Sem 2 Q1 Output:

  26. AY2004/5 Sem 2 Q2(b)

  27. AY2004/5 Sem 2 Q4 (1/3) Checkered matrices: A B A B A B A B A B A B A B A @ # @ # # @ # @ @ # @ # # @ # @ Non-checkered matrices: A B A B A A B A B A A B A B A @ # # @ # @ @ # # @ @ # @ # # @

  28. AY2004/5 Sem 2 Q4 (2/3)

  29. AY2004/5 Sem 2 Q4 (3/3)

  30. End of File

More Related