The science of computing: Java study questions
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.
Question J21: (Solution, p JS11)
Write a program that draws a diamond in import socs.*;
a window, as illustrated below. (The dia-
public class DrawDiamond {
mond need not be the same size or in the public static void main(String[] args) {
same position.) RobotWindow win = new RobotWindow();
win.show();
}
}
Question J22: (Solution, p JS11)
Suppose we had in our library a CircleStamp class with import socs.*;
the following methods.
public class DrawTwoCircle {
CircleStamp(RobotWindow win, double d) public static void run() {
RobotWindow win;
(Constructor method) Constructs a circle stamp for
win = new RobotWindow();
win, with a darkness level of d. The d parameter win.show();
should be between 0.0 and 1.0, with 0.0 represent-
ing white and 1.0 representing black.
void stamp(double x, double y)
Draws a circle with a radius of 10, centered at .
At right, complete the program so that it draws a gray cir-
cle on top of a black circle. Your program must use Cir-
cleStamp to complete this problem.
}
}
JS2 Questions
Question J31: (Solution, p JS11)
Suppose the user runs the Java program import socs.*;
at right, typing 5 when told to choose a
public class Mystery {
number. What would the computer draw? public static void main(String[] args) {
RobotWindow win;
win = new RobotWindow();
win.show();
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();
}
}
Question J32: (Solution, p JS12)
Suppose the user runs the Java program import socs.*;
at right, typing 20 when told to choose a
public class Mystery {
number. What would the computer draw? public static void main(String[] args) {
RobotWindow win = new RobotWindow();
win.show();
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
Question J41: (Solution, p JS12)
Suppose a user runs the Java program at import socs.*;
right, entering 5 when told. When the
public class Mystery {
program ended, how would its window public static void main(String[] args) {
appear? RobotWindow win;
win = new RobotWindow();
win.show();
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();
}
}
}
Question J42: (Solution, p JS12)
Suppose a user runs the Java program at import socs.*;
right, entering 13, then 21, when told.
public class Mystery {
public static void main(String[] args) {
a. Show the values taken on by the RobotWindow win;
following variables as the program win = new RobotWindow();
runs. win.show();
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
Question J43: (Solution, p JS13)
At right, complete the program so that it import socs.*;
reads an integer from the user and draws
public class Steps {
a set of stairs with that many steps. For public static void run() {
example, if the user were to type 5, the RobotWindow win;
programs window should look like the win = new RobotWindow();
win.show();
following when the program completes.
int steps;
steps = win.requestInt();
}
}
Question J44: (Solution, p JS13)
Write a program that reads an integer import socs.*;
from the user and draws horizon-
public class DrawLines {
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();
type , the program would display the fol-
lowing.
}
}
Questions JS5
Question J51: (Solution, p JS14)
Complete the program at right so that import socs.*;
when run, it repeatedly reads integers
public class PrintSum {
from the user until the user enters 0. Then public static void run() {
it should print the sum of the users inte- IOWindow io = new IOWindow();
gers.
For example, if a user ran your program
and entered 10, 2, 4, and 0, the user
should see the following.
Number? 10
Number? 2
}
Number? 4
}
Number? 0
16
Question J52: (Solution, p JS14)
Complete the program at right so that import socs.*;
when run, it reads a line from the user
public class FindFirstA {
and displays how many letters precede public static void run() {
the first lower-case a in the string. (Your IOWindow io = new IOWindow();
program may assume that the user types
a line containing an a.)
For example, a user typing the program
should see the following, assuming the
user types what is in boldface.
? This is a test.
8
In this example, the program displays 8
because the users string contains eight }
letters before the letter a. }
Question J53: (Solution, p JS14)
Suppose the user ran the program at right import socs.*;
and typed what is in boldface below.
public class Mystery {
public static void run() {
? Java Programming IOWindow io = new IOWindow();
io.print("? ");
What would the program then print? String str = io.readLine();
String a = str.substring(5, 9);
String b = str.substring(1, 3)
io.println(a + b);
}
}
JS6 Questions
Question J54: (Solution, p JS14)
Suppose the user ran the program at right import socs.*;
and typed 5 when prompted.
public class Mystery3 {
public static void run() {
a. Show the sequence of values taken IOWindow io = new IOWindow();
by the variables num, i, and k. io.print("? ");
int num = io.readInt();
num
int i = 1;
i int k = 0;
k while(i < num) {
i = i + 1;
b. What does the program print? k = k + i;
}
io.println(num + k);
}
}
Question J55: (Solution, p JS14)
Suppose the user ran the program at right import socs.*;
and typed 5 when prompted. What does
public class Mystery4 {
the program print? public static void run() {
IOWindow io = new IOWindow();
io.print("? ");
int num = io.readInt();
int i = 1;
String k = "0";
while(i < num) {
i = i + 1;
k = k + i;
}
io.println(num + k);
}
}
Question J56: (Solution, p JS15)
At right, complete the program so that it import socs.*;
reads a line from the user and then prints
public class WritingDown {
the characters of that string, one character public static void run() {
per line. For example, a user who runs the IOWindow io = new IOWindow();
program and types P Engel should see
the following
? P Engel
P
E
n
g
e
l
}
}
Questions JS7
Question J61: (Solution, p JS15)
Suppose a user runs the Java program at import socs.*;
right. When the program ended, how
public class Mystery {
would its window appear? public static void run() {
RobotWindow win = new RobotWindow();
win.show();
Robot rob = new Robot(win, 50, 50);
rob.turn(45);
int drawn = 0;
while(drawn < 7) {
rob.move(20);
if(drawn % 2 == 0) {
rob.turn(-90);
} else {
rob.turn(90);
}
drawn++;
}
rob.switchOff();
}
}
Question J62: (Solution, p JS15)
Complete the program at right so that it import socs.*;
reads a sequence of numbers ending in
, whereupon it prints the number of
public class CountEvens {
public static void run() {
even numbers typed. IOWindow io = new IOWindow();
I should see the following were I to run
your program and enter the numbers ,
, , , and .
Number? 2
Number? 77
Number? -34
Number? 104 }
Number? -1 }
3
Question J63: (Solution, p JS16)
Complete the program at right so that it import socs.*;
reads two lines from the user and displays
public class SameDifferent {
either same or different depending on public static void run() {
whether they are the same. For example, IOWindow io = new IOWindow();
a user typing the program should see the
following, assuming the user types what
is in boldface.
? First
? Second
different
}
}
JS8 Questions
Question J71: (Solution, p JS16)
Suppose we run the program at right and import socs.*;
see the following. (Boldface indicates
public class Mystery {
what the user types.) public static void run() {
IOWindow io = new IOWindow();
: 1 int[] a = new int[5];
: 3 int i = 0;
: 2 while(i < 5) {
: 4 io.print(": ");
: 0 a[io.readInt()] = i;
i++;
What does the program print now? }
i = 4;
while(i >= 0) {
io.print(a[i]);
i--;
}
}
}
Question J72: (Solution, p JS16)
Suppose the user ran the program at right import socs.*;
and typed what is in boldface below.
public class Mystery {
3 public static void run() {
5 IOWindow io = new IOWindow();
2 int[] a = new int[5];
4 int i = 0;
1 while(i < 5) {
a[i] = io.readInt();
What would the program then print? i++;
}
i = 0;
while(i < 5) {
i = a[i];
io.println(i);
}
}
}
Question J81: (Solution, p JS16) Explain the difference between using an instance method in a Java
program and using a class method.
Questions JS9
Question J82: (Solution, p JS16)
Suppose we had in our library a StringUtil class with import socs.*;
the following class method.
public class Palindrome {
static String reverse(String what) public static void run() {
IOWindow io = new IOWindow();
Returns a string with the same characters as
what, except in reverse order.
At right, complete the program so that it reads a
line from the user and displays palindrome or not
palindrome depending on whether that line is a
palindrome. (A palindrome is a word that reads the
same forwards and backwards, such as civic or noon,
but not bad or palindrome.)
For example, if a user should see the following on
}
running your program and typing good doog.
}
? good doog
palindrome
Your program must use StringUtils reverse
method to accomplish this task.
Question J83: (Solution, p JS16)
Suppose we were to execute the method
at right passing . for the
public static int myst(int[] intArray) {
int k;
int sk;
array parameter intArray. if(intArray[0] > intArray[1]) {
k = intArray[0];
a. Show the sequence of values taken sk = intArray[1];
by the variables k, sk, and i. } else {
k = intArray[1];
k sk = intArray[0];
sk }
i int i = 2;
while(i < intArray.length) {
b. What value does the method re- if(intArray[i] > k) {
sk = k;
turn? k = intArray[i];
} else if(intArray[i] > sk) {
sk = intArray[i];
}
i++;
}
return sk;
}
JS10 Questions
Question J84: (Solution, p JS17)
a. The Java method at right is set up to import socs.*;
take an array of floating-point numbers
public class FindMin {
as a parameter and return a floating-point public static double getMinimum(double[] nums) {
number. Complete it so that it returns the
minimum number in its parameter array.
}
}
b. Complete the Java program at right so import socs.*;
that it uses the method of part a. to com-
public class UseFindMin {
pute the minimum of the array scores, public static void run() {
which it should then print on the IOWin- IOWindow io = new IOWindow();
dow. double[] scores = new double[10];
int i = 0;
while(i < scores.length) {
scores[i] = io.readDouble();
i++;
}
}
}
Question J85: (Solution, p JS17)
Suppose we were to execute the run import socs.*;
method at right. What would the program
public class Mystery {
print? public static double f(double x) {
return x + x;
}
public static double g(double y) {
y = f(y);
return y * f(y);
}
public static void run() {
IOWindow io = new IOWindow();
double z = 2.0;
io.print(g(z));
io.println(" " + z);
}
}
Solutions JS11
Solution J21: (Question, p JS1)
import socs.*;
public class DrawDiamond {
public static void main(String[] args) {
RobotWindow win = new RobotWindow();
win.show();
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();
}
}
Solution J22: (Question, p JS1)
import socs.*;
public class DrawTwoCircle {
public static void run() {
RobotWindow win;
win = new RobotWindow();
win.show();
CircleStamp gray;
gray = new CircleStamp(win, 0.5);
gray.stamp(100, 90);
CircleStamp black;
black = new CircleStamp(win, 1.0);
black.stamp(100, 110);
}
}
Solution J31: (Question, p JS2)
JS12 Solutions
Solution J32: (Question, p JS2)
Solution J41: (Question, p JS3)
Solution J42: (Question, p JS3)
a. a 13 21 34 55 89 144
b 21 34 55 89 144 233
drawn 123456
b.
Solutions JS13
Solution J43: (Question, p JS4)
import socs.*;
public class Steps {
public static void run() {
RobotWindow win;
win = new RobotWindow();
win.show();
int steps;
steps = win.requestInt();
Robot r = new Robot(win, 10, 190);
int i;
i = 0;
while(i < steps) {
r.move(20);
r.turn(90);
r.move(20);
r.turn(-90);
i++;
}
r.switchOff();
}
}
Solution J44: (Question, p JS4)
import socs.*;
public class DrawLines {
public static void main(String[] args) {
RobotWindow win = new RobotWindow();
win.show();
int num = win.requestInt();
double gap = 200.0 / (num + 1);
int y = gap;
while(y < 200.0) {
Robot line = new Robot(win, 50, y);
line.move(100.0);
line.switchOff();
y += gap;
}
}
}
JS14 Solutions
Solution J51: (Question, p JS5)
import socs.*;
public class PrintSum {
public static void run() {
IOWindow io = new IOWindow();
io.print("Number? ");
int num = io.readInt();
int sum = 0;
while(num != 0) {
sum += num;
io.print("Number? ");
int num = io.readInt();
}
io.println(num);
}
}
Solution J52: (Question, p JS5)
import socs.*;
public class FindFirstA {
public static void run() {
IOWindow io = new IOWindow();
io.print("? ");
String line = io.readLine();
int index = 0;
while(!(line.substring(index, index + 1)).equals("a")) {
index++;
}
io.println(index);
}
}
Solution J53: (Question, p JS5) Progav
Solution J54: (Question, p JS6)
a. num 5
i 12345
k 0 2 5 9 14
b. 19
Solution J55: (Question, p JS6) 502345
Solutions JS15
Solution J56: (Question, p JS6)
import socs.*;
public class WritingDown {
public static void run() {
IOWindow io = new IOWindow();
io.print("? ");
String line = io.readLine();
int i = 0;
while(i < line.length()) {
String letter = line.substring(i, i + 1);
io.println(letter);
i++;
}
}
}
Solution J61: (Question, p JS7)
Solution J62: (Question, p JS7)
import socs.*;
public class CountEvens {
public static void run() {
IOWindow io = new IOWindow();
io.print("Number? ");
int num = io.readInt();
int count = 0;
while(num != -1) {
if(num % 2 == 0) {
count++;
}
io.print("Number? ");
num = io.readInt();
}
io.println(count);
}
}
JS16 Solutions
Solution J63: (Question, p JS7)
import socs.*;
public class SameDifferent {
public static void run() {
IOWindow io = new IOWindow();
io.print("? ");
String a = io.readLine();
io.print("? ");
String b = io.readLine();
if(a.equals(b)) {
io.println("same");
} else {
io.println("different");
}
}
}
Solution J71: (Question, p JS8) 31204
Solution J72: (Question, p JS8)
3
4
1
5
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.
Solution J82: (Question, p JS9)
import socs.*;
public class Palindrome {
public static void run() {
IOWindow io = new IOWindow();
io.print("? ");
String s = io.readLine();
String r = StringUtil.reverse(s);
if(s.equals(r)) {
io.println("palindrome");
} else {
io.println("not palindrome");
}
}
}
Solution J83: (Question, p JS9)
a. k 57
sk 256
i 23456
b. 6
Solutions JS17
Solution J84: (Question, p JS10)
a.
import socs.*;
public class FindMin {
public static double getMinimum(double[] nums) {
double min = nums[0];
int i = 0;
while(i < min) {
if(nums[i] < min) {
min = nums[i];
}
i++;
}
return min;
}
}
b.
import socs.*;
public class UseFindMin {
public static void run() {
IOWindow io = new IOWindow();
double[] scores = new double[10];
int i = 0;
while(i < scores.length) {
scores[i] = io.readDouble();
i++;
}
double min = FindMin.getMinimum(scores);
io.println("Minimum is " + min);
}
}
Solution J85: (Question, p JS10) 32.0 2.0