cs1101x: programming methodology discussion on past-year’s papers
DESCRIPTION
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). - PowerPoint PPT PresentationTRANSCRIPT
CS1101X: Programming Methodology
Discussion onPast-Year’s Papers
Past 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
Past Year's Papers 3
Ready?
Take out your pen and paper and get ready…
Past Year's Papers 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) return false;
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--; } } }
Past Year's Papers 5
AY2005/6 Sem 1 Q4 (2/3)
a. If p = (3, 2) and q = (4, 5), what is the output of lessThan(p, q)?
b. If p = (6, 3) and q = (4, 5), what is the output of lessThan(p, q)?
Answers:
Past Year's Papers 6
AY2005/6 Sem 1 Q4 (3/3)
c. Rewrite the lessThan method to remove all loops.
Past Year's Papers 7
AY2005/6 Sem 1 Q5 (1/4)
int[][] number = new int[6][6];
15 22 8 11 59 44
50 64 29 10 34 20
17 86 27 98 57 21
6 16 88 42 36 45
31 26 12 23 82 54
28 66 58 69 41 1
Past Year's Papers 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 = new int[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.
Past Year's Papers 9
AY2005/6 Sem 1 Q5 (3/4)
Table (a) Table (b)
Past Year's Papers 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
____________________________________________ ; }
Past Year's Papers 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: }
Past Year's Papers 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: }
Past Year's Papers 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: }
Past Year's Papers 14
AY2004/5 Sem 1 Q2Output:
Past Year's Papers 15
AY2004/5 Sem 1 Q3(a)
Past Year's Papers 16
AY2004/5 Sem 1 Q3(b)
Past Year's Papers 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;}
Past Year's Papers 18
AY2004/5 Sem 1 Q5(a) (1/2)
// Ballimport java.awt.*;public class Ball { private double 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 }}
Part (a): Complete the equals() method.
Past Year's Papers 19
AY2004/5 Sem 1 Q5(a) (2/2)Part (a):Part (a): Complete the equals() method.
Past Year's Papers 20
AY2004/5 Sem 1 Q5(b) (1/3)
private Point centre; public Point getCentre() { return centre; } public void setCentre(Point p) { centre = p; }
Extend Ball class to BallOn2D, with radius, colour and centre. Centre is an object of class Point.
Write (i) two constructors; (ii) a main method that creates two BallOn2D objects; and (iii) the toString() method.
Past Year's Papers 21
AY2004/5 Sem 1 Q5(b) (2/3)
Past Year's Papers 22
AY2004/5 Sem 1 Q5(b) (3/3)
Past Year's Papers 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.]
Past Year's Papers 24
AY2004/5 Sem 1 Q5(c) (2/2)isPrime(int n)
Past Year's Papers 25
AY2004/5 Sem 2 Q1Output:
Past Year's Papers 26
AY2004/5 Sem 2 Q2(b)
Past Year's Papers 27
AY2004/5 Sem 2 Q4 (1/3)
A B A B A B A B A B A B A B A
Checkered matrices:
@ # @ # # @ # @ @ # @ # # @ # @
Non-checkered matrices:
A B A B A A B A B A A B A B A
@ # # @ # @ @ # # @ @ # @ # # @
Past Year's Papers 28
AY2004/5 Sem 2 Q4 (2/3)
Past Year's Papers 29
AY2004/5 Sem 2 Q4 (3/3)
Past Year's Papers 30
End of File