v1 Java Study Question
v1 Java Study Question
first edition
by Carl Burch
Copyright c 2004, by Carl Burch. This publication may be redistributed, in part or in whole, provided that
this page is included. A complete version, and additional resources, are available on the Web at
http://www.cburch.com/socs/
Questions JS1
This document contains study questions to help in studying the material covered in the textbook, The
science of computing: Java supplement. Each questions label has two parts separated by a dash. Question
J31, for example, is the first study question for the material covered in Chapter J3 of The science of
computing: Java supplement.
}
}
}
}
JS2 Questions
double n;
n = win.requestInt();
Robot robbie;
robbie = new Robot(win, n, n);
robbie.move(200 - 2 * n);
robbie.turn(-90);
robbie.move(200 - 2 * n);
robbie.turn(-135);
robbie.move(200 - 2 * n);
robbie.switchOff();
}
}
double n = win.requestDouble();
Robot robbie = new Robot(win, 100, 100);
robbie.move(n);
robbie.turn(n);
robbie.move(n);
robbie.turn(n);
robbie.move(n);
robbie.turn(n);
robbie.move(n);
robbie.switchOff();
}
}
Questions JS3
int num;
num = win.requestInt();
int drawn;
drawn = 1;
while(drawn <= num) {
Robot r2d2 = new Robot(win, 10, 20 * drawn);
r2d2.move(20 * drawn);
drawn = drawn + 1;
r2d2.switchOff();
}
}
}
a int a = win.requestInt();
b int b = win.requestInt();
int drawn = 1;
drawn Robot r = new Robot(win, 70, 100);
while(drawn < 6) {
b. When the program ended, how r.move(b);
would its window appear? drawn = drawn + 1;
r.turn(90);
int c = a + b;
a = b;
b = c;
}
r.switchOff();
}
}
JS4 Questions
}
}
tal lines evenly spaced down the window, public static void main(String[] args) {
with each line extending from to RobotWindow win = new RobotWindow();
. For example, were the user to win.show();
}
}
Questions JS5
Number? 10
Number? 2
}
Number? 4
}
Number? 0
16
? This is a test.
8
In this example, the program displays 8
because the users string contains eight }
letters before the letter a. }
E
n
g
e
l
}
}
Questions JS7
Number? 2
Number? 77
Number? -34
Number? 104 }
Number? -1 }
3
? First
? Second
different
}
}
JS8 Questions
Question J81: (Solution, p JS16) Explain the difference between using an instance method in a Java
program and using a class method.
Questions JS9
}
}
}
}
Robot diamond;
diamond = new Robot(win, 100, 125);
diamond.turn(45);
diamond.move(75);
diamond.turn(90);
diamond.move(75);
diamond.turn(90);
diamond.move(75);
diamond.turn(90);
diamond.move(75);
diamond.switchOff();
}
}
CircleStamp gray;
gray = new CircleStamp(win, 0.5);
gray.stamp(100, 90);
CircleStamp black;
black = new CircleStamp(win, 1.0);
black.stamp(100, 110);
}
}
a. a 13 21 34 55 89 144
b 21 34 55 89 144 233
drawn 123456
b.
Solutions JS13
int steps;
steps = win.requestInt();
a. num 5
i 12345
k 0 2 5 9 14
b. 19
Solution J81: (Question, p JS8) Instance methods are messages that are sent to objects of a class; for
example, since move is an instance method of a Robot class, we can send the move message to an individual
Robot object (created using new). Class methods, however, apply to the class itself: That is, a class method
is a message we send to the class, not to objects of that class. Since pow is a class method of the Math class,
we send it to the Math class, not to individually created Math objects.
a. k 57
sk 256
i 23456
b. 6
Solutions JS17
a.
import socs.*;