Title: CS1101X: Programming Methodology Discussion on Past-Year
1CS1101X Programming MethodologyDiscussion
onPast-Years Papers
2Information
- www.comp.nus.edu.sg/cs1101x/3_ca/exams.html
contains links to - Exam time-table
- Past-years papers
- How to prepare for exams
3Ready?
- Take out your pen and paper and get ready
4AY2005/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 i0 true
i) // outer loop x 0 y
i for (int j0 j lt i j) // inner
loop if (x p.x y p.y) return
true if (x q.x y q.y) return
false x y--
5AY2005/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
6AY2005/6 Sem 1 Q4 (3/3)
- Rewrite the lessThan method to remove all loops.
7AY2005/6 Sem 1 Q5 (1/4)
int number new int66
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
8AY2005/6 Sem 1 Q5 (2/4)
for (int i 0 i lt 6 i) selectionSort(
numberi ) // At this point, fill in table (a)
on the content // in the number array. int
tempNumber new int6 for (int j 0 j lt 6
j) for (int i 0 i lt 6 i)
tempNumberi numberij selectionSort(
tempNumber ) for (int i 0 i lt 6 i)
numberij tempNumberi // At this
point, fill in table (b) on the content // in
the number array.
9AY2005/6 Sem 1 Q5 (3/4)
Table (a)
Table (b)
10AY2005/6 Sem 1 Q5 (4/4)
static boolean search (int key, int table,
int startX, int startY,
int endX, int endY) if
(startX gt endX startY gt endY) return _________
int midX (startX endX) / 2 int
midY (startY endY) / 2 if (key
tablemidXmidY) return __________ if
(key lt tablemidXmidY ) return
____________________________________________
else return
____________________________________________
11AY2004/5 Sem 1 Q1 (1/3)
1 public class dice 2 3 int freq
4 5 public Dice() 6 freq new
int7 // freq0 is not used 7 8 9
public void getFreq(int face) 10 return
freqface 11 12 13 public void
setFreq(int face, int value) 14 freqface
value 15
12AY2004/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
13AY2004/5 Sem 1 Q1 (3/3)
31 32 public void printFreq() 33 for
(int i 1 i lt 6 i) 34
System.out.println ("Face " i 35
" has frequency " getFreq(face)) 36
37 38
14AY2004/5 Sem 1 Q2
Output
15AY2004/5 Sem 1 Q3(a)
16AY2004/5 Sem 1 Q3(b)
17AY2004/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 gt 0)
return coins
18AY2004/5 Sem 1 Q5(a) (1/2)
Part (a) Complete the equals() method.
// Ball import 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
19AY2004/5 Sem 1 Q5(a) (2/2)
Part (a)
Part (a) Complete the equals() method.
20AY2004/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.
21AY2004/5 Sem 1 Q5(b) (2/3)
22AY2004/5 Sem 1 Q5(b) (3/3)
23AY2004/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.
24AY2004/5 Sem 1 Q5(c) (2/2)
isPrime(int n)
25AY2004/5 Sem 2 Q1
Output
26AY2004/5 Sem 2 Q2(b)
27AY2004/5 Sem 2 Q4 (1/3)
Checkered matrices
A B A B A B A B A B A B A B A
_at_ _at_ _at_ _at_ _at_ _at_ _at_ _at_
Non-checkered matrices
A B A B A A B A B A A B A B A
_at_ _at_ _at_ _at_ _at_ _at_ _at_ _at_
28AY2004/5 Sem 2 Q4 (2/3)
29AY2004/5 Sem 2 Q4 (3/3)
30End of File