Define the term package as it is used in Java. Give an example of a package and its contents based on what we've used in class.
A package is a group of related classes packaged together, like
the acm.graphics package, which includes classes for
graphical shapes such as GOval, GRect, and
GLine.
What distinguishes an instance method from a static method? Give an example of each from what we've seen in class.
An instance method, such as the GOval class's
move method, is something that only an individual
object of that class can perform; by contrast, a static method,
like the Math class's sqrt method, is something
that the class itself performs.
Thus, we can write Math.sqrt(4) in a program, asking
the Math class to perform the sqrt method; but
GOval.move(1, 1) would be illegal — we would need the
name of a GOval object on the left side, such as
ball.move(1,1), if ball were a
GOval.
Suppose we have the below classes and variables defined.
(For example, this table indicates that Grandchild is a
subclass of the Child class, and that we have defined a
Grandchild variable named jacob.)
| class name | superclass | variable name |
Parent | Object | abram |
Child | Parent | isaac |
Grandchild | Child | jacob, esau |
Which of the following assignment statements will the Java compiler accept?
| a. | jacob = isaac |
d. | abram = isaac | |
| b. | jacob = abram |
e. | abram = jacob | |
| c. | jacob = esau |
(c.), (d.), and (e.) are all acceptable.
Suppose we have a class named Child,
which is a subclass of another class named Parent.
Each includes a constructor with no parameters.
Also, Parent defines a
method named scold, and
Child defines a method named
misbehave; both are void
methods with no parameters.
Suppose we have three variables,
a Child variable named kid,
a Parent variable named mom,
and an Object variable named x.
Which of the following are legal statements?
a. mom = new Child();b. kid = new Parent();c. x = new Child();d. mom.misbehave();e. kid.scold();
(a.), (c.), and (e.) are legal; (b.) and (d.) are illegal.
Complete the below method so that it returns the number of
GOvals located in the window. You'll want to use the
getElement and getElementCount methods.
(You should not recursively descend into
GCompounds.)
public class Mystery extends GraphicsProgram {
public void run() {
// code adding shapes into window is not shown here
this.println("Number of ovals created: " + countOvals());
}
public int countOvals() {
int count = 0;
for(int i = 0; i < this.getElementCount(); i++) {
GObject obj = this.getElement(i);
if(obj instanceof GOval) {
count++;
}
}
return count;
}
}
| a. | 101101(2) | to decimal |
| b. | 1010101(2) | to decimal |
| c. | 23(10) | to binary |
| d. | 95(10) | to binary |
| a. | 101101(2) | = 45(10) |
| b. | 1010101(2) | = 85(10) |
| c. | 23(10) | = 10111(2) |
| d. | 95(10) | = 1011111(2) |
| a. | 85(10) |
| b. | −86(10) |
| c. | 00011010 |
| d. | 10011000 |
run and
weird.
public class Problem extends Program {
public void run() {
double x = 3;
double y = weird(x);
println(x + y);
}
public double weird(double z) {
println(z);
z = z * z;
println(z);
return z;
}
}
What will the program display when the
run method is invoked?
3 9 12
public class Problem extends Program {
public void run() {
double a = 3.0;
double x = 4.0;
weird(a);
weird(a);
println(x);
}
public void weird(double y) {
double x = y * 2;
y = x + 5.0;
println(y);
}
}
11.0 11.0 4.0
What will the below program display when executed?
public class Mystery extends Program {
public void run() {
int a = 3;
println(f(a));
println(a);
}
public int f(int x) {
println(g(x));
int a = x + 5;
println(g(a));
return a;
}
public int g(int y) {
y = y * 2;
println(y);
return y + 1;
}
}
6 7 16 17 8 3
What will the below program display when executed?
public class PrintLocations extends GraphicsProgram {
public void run() {
GRect a = new GRect(10, 10, 10, 10);
GRect b = new GRect(40, 40, 40, 40);
this.moveRects(a, b);
this.println(a.getX() + " " + b.getX());
// Note: getX returns x-coordinate of upper left corner
}
public void moveRects(GRect c, GRect d) {
c = new GRect(50, 50, 50, 50);
d.setLocation(20, 20); // moves GRect so (20,20) is upper left corner
}
}
fact method so
that it responds with the factorial of its parameter.
Once completed, the program below should execute as follows.
Factorial of what? 4 4 factorial is 24
public class Problem extends Program {
public void run() {
int input = readInt("Factorial of what? ");
int result = factorial(input);
println(input + " factorial is " + result);
}
public int fact(int query) {
int ret = 1;
int cur = 1;
while(cur <= query) {
ret = ret * cur;
cur = cur + 1;
}
return ret;
}
}
weird method below so that the
run method, when executed, displays
4.0,7.0.
(Do not modify run. There are several possible answers.)
public class Problem extends Program {
public void run() {
double a = 16.0;
double b = 49.0;
println(weird(a) + "," + weird(b));
}
public double weird(double x) {
if(x == 16.0) {
return 4.0;
} else {
return 7.0;
}
}
}
Two shorter answers:
return Math.sqrt(x);
or return (x + 28) / 11;