






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
6 Unsolved Practice Questions.
Typology: Exercises
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Family Name:.............................. Other Names:.............................
Student ID:................................ Signature..................................
Cross out rough working that you do not want marked. Specify the question number for work that you do want marked.
(b) [6 marks]
What will the following printValues method print out?
x:
y:
z:
public void printValues (){ double x = 50; double y = 20; double z = x + y; UI. println ("z is " + z);
x = y; y = x; UI. println ("x is " + x); UI. println ("y is " + y);
z = z + y; UI. println ("z is " + z);
z = x − z; UI. println ("z is " + z); }
z is
Question 2. Programs with text input and output [7 marks]
Complete the following calculateCylinder method that will ask the user for the height and radius of a cylinder and then print out the volume of the cylinder ( 2 π r^2 h).
You may use the value 3.14159 or the constant Math.PI in your method.
public void calculateCylinder (){
Question 4. Programs with graphical output [12 marks]
(a) [5 marks] Sketch what the following drawIt() method will draw in the graphics pane, if the user types 100 when asked for the size?
public static final double width = 50;
public void drawIt(){ double size = UI.askDouble("Enter the size:"); UI.drawRect(250, 150, width, size ); UI. fillOval (150, 50, size , width); UI.drawLine(width, size , width, 2 ∗ size ); }
100
200
300
50
150
250
50 150 250 350 Enter the size: 100
Extra copy (in case you made a mistake):
100
200
300
50
150
250
50 150 250 350 Enter the size: 100
(b) [7 marks]
Complete the following drawSign() method so that it draws the following sign in the UI window.
drawSign should
public void drawSign(){
public void testTurtles (){
Question 6. Programs with Methods and Parameters [5 marks]
The following fancyTurtles() method makes a collection of TurtleRobots draw some shapes on the window. It uses the fancy(TurtleRobot t, double ang, double distance) method that is also given below.
In the box, sketch what fancyTurtles() will draw on the window. (You may use any empty space to draw it if you get it wrong the first time.
public void fancyTurtles (){ TurtleRobot t1 = new TurtleRobot(Color.blue); this. fancy(t1 , 45, 50); TurtleRobot t2 = new TurtleRobot(Color.red); this. fancy(t2 , −120, 20); this. fancy(t2 , 120, 40); this. fancy(t1 , 0, 40); this. fancy(t1 , −90, 10); }
public void fancy(TurtleRobot t , double ang, double dist ){ t .penOn(); t. turn(ang); t .move(dist); t. turn(ang); t .move(dist); t. turn(ang); t .move(dist); t. turn(− 3 ∗ang); t .penOff(); t .move(10); }
(Question 6 continued on next page)