Practice
Test 1 Computer Science IQuestion 1. (15 points) Suppose we have the following declarations:
int m, n, i = 3, j = 4, k = 5;
double v, w, x = 2.8, y = 5.0;
final int p = 2;
Determine the value assigned to the variable in each of the following assignment statements or explain why it is not a valid assignment.
a) m = 1 + k / 2 + i % 2;
b) n = (int) x * 3.0 + 6;
c) v = p * y + k;
d) w = j / i + k;
e) m = 5 * x - (k % 2);
f) p = 2 * (int)0 x + k + j;
g) n = j * k + (int) x;
Question 2. (10 points) Write a Java statement to calculate the following formula, where x, y, z are variables of type double.
Hints: Use Math.sqrt( ) to calculate the square root (e.g, Math.sqrt(9.0) returns 3.0)
Use Math.pow( ) to calculate the power (e.g., Math.pow(2.0, 3.0) returns 8.0)
y = z +
Question 3. (10 points) Based on the value of the "percentageOfTuitionIncrease" variable, write nested-if statements that prints the appropriate messages in a Text object.
Tuition Increase Range |
Message to Print |
percentageOfTuitionIncrease £ 1 |
"Great!" |
1 < percentageOfTuitionIncrease < 4 |
"Reasonable" |
4 £ percentageOfTuitionIncrease £ 8 |
"Tolerable" |
8 < percentageOfTuitionIncrease |
"Outrageous!" |
Question 4. (10 points) Show the exact output generated by the below code:
int i = 50, j = 60, k, m; new Text( i + j + "= Sum", 10, 50, canvas); new Text( "Sum = " + i + j, 10, 100, canvas); new Text( "Sum = " + (i + j), 10, 150, canvas ); |
canvas |
|
Question 5. (40 points) Suppose we have the following two Java files:
ProductTester.java |
|
Product.java |
public class ProductTester { public static void main (String[ ] args) { double x, y, total; total = 123.0; Product p1 = new Product("gum", 3, 1.00); Product p2 = new Product("corn",8, 2.50); x = p1.doSomething(4); y = p2.doSomething(2); p1 = p2; } // end main } // end ProductTester |
|
public class Product { private String name; private int groupCount; private double priceForGroup; public Product (String name, int group, double price) { this.name = name; groupCount = group; priceForGroup = price; } // end Product public double doSomething(int g) { double total; total = g*priceForGroup; return total; } // end totalCost } // end class Product |
a) At the end of the "main" method of ProductTester, what would be the value of the following:
x:
y:
total:
p1.groupCount:
p2.groupCount:
b) Write a new method for the Product class called "grossCost" that returns the cost of a gross of the product. A gross is defined to be 12 dozen or 144 units.
c) Write a statement for the ProductTester to determine the cost of a gross of Product p1 using your "grossCost" method.
d) On the code at the top of the page, label one of each of the following: an instance variable, a parameter, a local variable, a Product object, a return type, an instantiable class.
Question 6. (10 points) Below is the template for a class that extends WindowController. The begin method draws a FramedRect with a Text object with the word "HI!". You are to add the code to allow the user to drag both of these with the mouse.
import
objectdraw.*;
import java.awt.*;
public class T1_Q6 extends WindowController {
// Instance variables
private FramedRect box;
private Text message;
public void begin() {
box = new FramedRect(100, 100, 50, 50, canvas);
Text message = new Text("HI!", 115, 115, canvas);
} // end begin
public void onMouseClick(Location point) {
} // end onMouseClick
public void onMousePress(Location point) {
} // end onMousePress
public void onMouseRelease(Location point) {
} // end onMouseRelease
public void onMouseDrag(Location point) {
} // end on MouseDrag
} // end T1_Q6 class
Question 7. (5 points) What type of values make good named constants (using "final") in a program?