Page 400 - Introduction to Programming with Java: A Problem Solving Approach
P. 400

                366 Chapter 9 Classes with Class Members 6. [after §9.3] Consider the following program.
public class Test
{
}
// end Test class
private int x;
private static int y;
public void doIt()
{
}
}
}
Apago PDF Enhancer
x = 1;
y = 2;
public static void tryIt()
{
x = 3;
y = 4;
public static void main(String[] args)
{
doIt();
tryIt();
Test t = new Test();
t.doIt();
Test.doIt();
Test.tryIt();
a) Mark all of the lines of code that have a compilation error. b) For each compilation-error line, explain why it is incorrect.
Note:
• There are no errors in the variable declarations and there are no errors in the method headings, so
don’t mark any of those lines as having an error.
• For each compilation error, just provide the reason that the error occurs. In particular, do not solve
the problem by fixing the code until you get rid of all the compilation errors.
7. [after §9.4] Why is it safe to declare named constants public?
8. [after §9.4] Write appropriate declarations for the following constants. In each case, decide whether to include the keyword, static, and whether to include initialization in the declaration. Also, make each constant as easily accessible as possible, consistent with protection from inadvertent corruption.
a) The year of birth of a person.
b) The format string, "%-25s%,13.2f%,13.2f%(,15.2f\n", to use in several printf statements
in a single method.
c) The “golden ratio” or width / length of a golden rectangle. It’s equal to (sqrt(5) 􏰀 1) / 2 􏰁
0.6180339887498949.
9. [after §9.5] Write a utility class called RandomDistribution, which contains the following four class methods. You should be able to implement all of these methods with calls to Math class methods and/or calls to one of the uniform methods within the RandomDistribution class.
a) Write a method called uniform that generates a double random number from a continuous distribu- tion that is uniform between double values min and max.
b) Write another (overloaded) method called uniform that generates an int random number from a dis- crete distribution that is uniform between int values min and max, including both of these end points.
























































   398   399   400   401   402