Chapter 8 Review Exercise Solutions

Chapter 8 Review Exercise Solutions. R8.1. class VendingMachine to display
available products. class Product to represent the each product in the machine.
class Coin to represent coin values such as quarters, nickels, etc.

Part of the document



Chapter 8 Review Exercise Solutions


R8.1

class VendingMachine to display available products
class Product to represent the each product in the machine
class Coin to represent coin values such as quarters, nickels, etc.

R8.2

class PayCheck to represent a pay check object, the total hours of work
plus any overtime paid over a period of time
class Employee to represent each employee and hours worked

R8.3

class Customer handles the information of a customer object, its shipping
and billing addresses.
class Invoice lists the products that the customer ordered, records
payments and calculates the amount due.
class Product represents the name of the products and the amount it costs.
class Address represents the shipping and billing addresses of each
customer.

R8.4

The System class is not cohesive because it includes many features that are
unrelated, such as console I/O, garbage collection, and the time in
milliseconds.

R8.5

[pic]

R8.6

[pic]

R8.7

The Integer class depends on the String class.

R8.8

The class Rectangle depends on the Rectangle2D, Point, Dimension, and
String classes.

R8.9

boolean hasNext() - Accessor
boolean hasNextDouble() - Accessor
boolean hasNextInt() - Accessor
boolean hasNextLine() - Accessor
String next() - Mutator
double nextDouble() - Mutator
int nextInt() - Mutator
String nextLine() - Mutator

R8.10

Accessor methods:

contains
createIntersection
createUnion
equals
getBounds
getBounds2D
getHeight
getLocation
getSize
getWidth
getX
getY
intersection
intersects
isEmpty
outcode
toString
union

Mutator methods:

add
grow
setBounds
setLocation
setRect
setSize
translate

R8.11

Of the three class, Rectangle, String, and Random, only class String is
immutable

R8.12

Of the three classes PrintStream, Date, and Integer, the Integer class is
immutable.

R8.13

public void print()
The side effect is using System.out to print on the console
public void print(PrintStream stream)
The side effect is modifying the stream parameter
public String toString()
There is no side effect

R8.14

If no function (including main) has a side effect, then you could not
observe the program doing anything. Such a program would be useless. (Note
that producing output on the screen is a side effect.)

R8.15

/**
Computes the square root of a number.
@param x the number
@return the square root
(Precondition: x >= 0)
*/
public static double sqrt(double x)

/**
Converts an integer to its Roman numeral form.
@param n the integer to be converted
@return the Roman numeric equivalent
(Precondition: n > 0)
*/
public static String romanNumeral(int n)

/**
Computes the slope of a line. The line should not be vertical.
@param a the Line object
@return the slope
(Precondition: a is not vertical)
*/
public static double slope(Line2D.Double a)

/**
Converts a day into its English name form.
@param day the day (0 = Monday, 1 = Tuesday, . . . , 6 = Sunday)
@return the English name form of the day
(Precondition: day < 7 && day >= 0)
*/
public static String weekday(int day)

R8.16

Math.sqrt: Argument is non-negative
Math.tan: Argument is an angle in radians
Math.log: Argument is positive
Math.exp: No precondition
Math.pow(double x, double y): (x > 0, or x = 0 and y > 0, or x < 0 and y is
an integer)
Math.abs: No precondition

R8.17

a. Integer.parseInt(String s): The string can be interpreted as an integer
b. StringTokenizer.nextToken(): hasMoreTokens() must be true
c. Random.nextInt(int n): n > 0
d. String.substring(int begin, int pastEnd):
begin >= 0 && pastEnd 0)
*/
public void enterPayment(int coinCount, Coin coinType)

R8.20

When you call falseSwap, then a is initialized with 3 and b is initialized
with 4. At the end of the falseSwap method, a is 4 and b is 3. Then the
method exits and its local variables (including the parameter variables)
are forgotten. The contents of x and y are not affected.

R8.21

public static void swap(Point2D.Double p)
{
p.setLocation(p.getY(), p.getX());
}
public static void main(String[] args)
{
double x = 3;
double y = 4;
Point2D.Double p = new Point2D.Double(x, y);
swap(p);
x = p.getX();
y = p.getY();
System.out.println(x + " " + y);

R8.22

[pic]

R8.23

[pic]

R8.24

We get the error message "non-static method print(int) cannot be referenced
from a static context" because the print() method is not declared as
static. If you change the header of the method to public static void
print(int x), then the program will work. The reason the method needs to be
declared as static is because we are calling it without an object reference
(implicit parameter), but only static methods can be called like that.

R8.25

decode
getInteger
highestOneBit
lowestOneBit
numberOfLeadingZeros
numberOfTrailingZeros
parseInt
reverse
reverseBytes
rotateLeft
rotateRight
signum
toBinaryString
toHexString
toOctalString
toString // all of the toString variations, except toString()
valueOf
They are static methods because these methods do not need an implicit
parameter.

R8.26

All of the valueOf() methods in the String class are static methods. Like
the methods in the Integer class, these methods are static because these
methods do not operate on an object and have only explicit parameters. They
create a new String instead of modifying an existing String (implicit
parameter, which these methods do not need). The format methods are also
static, for the same reason.

R8.27

It is not a good design because using public static fields are not a good
idea; they can accidentally get overwritten in large programs. A better way
to do this is to have static methods System.getIn() and System.getOut()
that return these streams.

R8.28

In the following class, all of the declarations of n are legal except for
2. See comments below.
public class X
{
public int f()
{
int n = 1;
return n;
}

public int g(int k)
{
int a;
for (int n = 1; n