Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 1

R7.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.

R7.3 class Customer handles the information of a customer object, its shipping and billing addresses, and what product this customer ordered. class Invoice generates the products that the customer ordered, create an invoice and calculate 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

R7.7 The Integer class depends on the String class.

R7.9 int countTokens() - Accessor boolean hasMoreElements() - Accessor Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 2

boolean hasMoreTokens() - Accessor

Object nextElement() - Mutator

String nextToken() - Mutator

String nextToken(String delim) - Mutator

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

R7.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

R7.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) Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 3

@return the English name form of the day (Precondition: day = 0) */ public static String weekday(int day)

R7.17 Integer.parseInt(String s): The characters in the string must all be decimal digits

StringTokenizer.nextToken(): hasMoreTokens() must be true

Random.nextInt(int n): n > 0

String.substring(int m, int n): m > 0 && n 0) (Postcondition: getTotal() > 0) */ public void addCoin(Coin aCoin)

R7.21 public static void swap(Point2D.Double p) < p.setLocation(p.getY(), p.getX()); >public static void main(String[] args)

R7.23 parseInt(String s) toBinaryString(int i) toHexString(int i) toOctalString(int i) toString(int i) Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 4 valueOf(String s) They are static methods because these methods do not need an implicit parameter.

R7.25 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 improve this is to have static methods System.getIn() and System.getOut() that return these streams.

R7.27 A qualified name is a name prefixed by its class name or by an object reference, such as Math.sqrt or other.balance. An unqualified name is a name without a class or object prefix such as account or getBalance

R7.29 To write a java program without import statements, the user needs to specify the path names of the classes that are used in the program /** An applet that computes and draws the intersection points of a circle and a line. */ public class IntersectionApplet extends java.applet.Applet

double r = 100; // the radius of the circle

// draw the circle

java.awt.geom.Ellipse2D.Double circle = new java.awt.geom.Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS); g2.draw(circle);

// draw the vertical line

java.awt.geom.Line2D.Double line = new java.awt.geom.Line2D.Double(x, 0, x, 2 * RADIUS); g2.draw(line);

// compute the intersection points

double a = RADIUS; double b = RADIUS; Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 5

double root = Math.sqrt(RADIUS * RADIUS - (x - a) * (x - a)); double y1 = b + root; double y2 = b - root;

// draw the intersection points

LabeledPoint p1 = new LabeledPoint(x, y1); LabeledPoint p2 = new LabeledPoint(x, y2);

private static final double RADIUS = 100; private double x; >

/** Gets the coin value. @return the value */ public double getValue()

private double value; private String name; >

/** A purse computes the total value of a collection of coins. */ public class Purse < /** Constructs an empty purse. */ public Purse()

/** Add a coin to the purse. @param aCoin the coin to add */ public void add(Coin aCoin)

/** Get the total value of the coins in the purse. @return the sum of all coin values */ public double getTotal()

private double total; >

PurseTest.java import javax.swing.JOptionPane;

Purse myPurse = new Purse();

/** Computes the surface area of a sphere @param r the radius @return surface area of a sphere */ Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 8

public static double sphereSurface(double r)

/** Computes the volume of a cylinder @param r the radius @param h the height @return volume of a cylinder */ public static double cylinderVolume(double r, double h)

/** Computes the surface area of a cylinder @param r the radius @param h the height @return surface area of a cylinder */ public static double cylinderSurface(double r, double h)

/** Computes the volume of a cone @param r the radius @param h the height @return volume of a cone */ public static double coneVolume(double r, double h) < return (1.0 / 3.0) * Math.PI * r * r * h; >

/** Computes the surface area of a cone @param r the radius @param h the height @return surface area of a cone */ public static double coneSurface(double r, double h) < return Math.PI * r * (h + r); >>

ExP7_3.java import javax.swing.JOptionPane;

/** This is a test driver for the Geometry class Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 9

System.out.println("The volume of the sphere is: " + Geometry.sphereVolume(r)); System.out.println("The surface area of the sphere is: " + Geometry.sphereSurface(r)); System.out.println("The volume of the cylinder is: " + Geometry.cylinderVolume(r, h)); System.out.println("The surface area of the cylinder is: " + Geometry.cylinderSurface(r, h)); System.out.println("The volume of the cone is: " + Geometry.coneVolume(r, h)); System.out.println("The surface area of the cone is: " + Geometry.coneSurface(r, h));

P7.5 EllipseHelper.java import java.awt.geom.Ellipse2D;

/** The standard API for the Ellipse does not provide methods to compute the area and perimeter of an Ellipse. Because we cannot add or modify library classes, we must provide our own helper class. It makes sense to use static methods here because these methods do not require an implicit parameter */ public class EllipseHelper < /** Computes the area of an ellipse @param e the ellipse @return area of the ellipse */ public static double area(Ellipse2D.Double e) < double a = e.getWidth() / 2; double b = e.getHeight() / 2; return Math.PI * a * b; Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 10

/** Computes the perimeter of an ellipse @param e the ellipse @return the perimeter of the ellipse */ public static double perimeter(Ellipse2D.Double e) < double a = e.getWidth() / 2; double b = e.getHeight() / 2;

return Math.PI * (1.5 * (a + b) - Math.sqrt(a * b)); > >

ExP7_5.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JOptionPane; import java.awt.geom.Ellipse2D;

input = JOptionPane.showInputDialog( "Please enter the height:"); h = Double.parseDouble(input); >

Ellipse2D.Double e = new Ellipse2D.Double(0, 0, w, h); g2.draw(e);

g2.drawString("Perimeter: " + EllipseHelper.perimeter(e), 100, 260); g2.drawString("Area: " + EllipseHelper.area(e), 100, 290); >

double w; double h; > Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 11

P7.7 Geometry.java import java.awt.geom.Point2D; import java.awt.geom.Ellipse2D;

/* Note that the center of the ellipse has coordinates (e.getX() + a, e.getY() + b) */

double x = (p.getX() - e.getX() - a) / a; double y = (p.getY() - e.getY() - b) / b;

double xySquare = x * x + y * y;

return xySquare = 0) System.out.println("The zip code is: " + val); else System.out.println("Incorrect bar code data");

System.exit(0); > > P7.15 Greeter.java Solutions Manual: Chapter 7 Big Java, by Cay Horstmann 21 package com.yahoo.janedoe.example; public class Greeter

public void getGreeting()

private String name; private String adj; >

Greeter g = new Greeter(name, adjective);