310 likes | 328 Views
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).
E N D
CS1101X: Programming MethodologyDiscussion onPast-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) 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--; } } }
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:
AY2005/6 Sem 1 Q4 (3/3) Rewrite the lessThan method to remove all loops.
AY2005/6 Sem 1 Q5 (1/4) int[][] number = new int[6][6];
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.
AY2005/6 Sem 1 Q5 (3/4) Table (a) Table (b)
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 ____________________________________________ ; }
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: }
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: }
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: }
AY2004/5 Sem 1 Q2 Output:
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; }
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 } }
AY2004/5 Sem 1 Q5(a) (2/2) Part (a): Part (a): Complete the equals() method.
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.
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.]
AY2004/5 Sem 1 Q5(c) (2/2) isPrime(int n)
AY2004/5 Sem 2 Q1 Output:
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 @ # # @ # @ @ # # @ @ # @ # # @