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

Review Exercises

R8.1

  • unit test: A form of test whereby a procedure or function is compiled outside the program it will be used.
  • test harness: A program that feeds arguments to a procedure during testing.

R8.3

  • Regression testing: Regression testing involves repeating previously run tests to ensure that known failures
    of prior versions do not appear in new versions of the software.
  • Test suite: A test suite is a set of tests for repeated testing..

R8.5

  • positive test cases:
    -0.5 returns -Math.PI / 6
    -Math.sqrt(2) / 2 returns -Math.PI / 4
    Math.sqrt(3) / 2 returns Math.PI / 3
  • boundary case:
    1 returns Math.PI / 2
  • negative test cases:
    -2 returns error
    1.00000001 returns error.

R8.7

  • Stepping into a method means entering the first line of that method. This is used to check whether the method carries out its job correctly.
  • Stepping over a method means going to the next program line. This is used when one knows a method works correctly.

R8.9

The procedure depends on the debugger. Most debuggers know about the String class and display its contents immediately when you ask to inspect or watch a string. However, you can also display the instance variables and inspect the value instance variable.

R8.11

The "divide-and-conquer" strategy involves stepping over the methods in main to pinpoint the location of the failure. When a failure occurs just after a particular method (say f), then restart main, set a breakpoint before f, step into f, and now apply the same strategy to f. Keep going until the error is found.

Programming Exercises

P8.1

ArcSinApproximator1.java

/**

This class provides methods to compute the

arcsin of a number

*/

public class ArcSinApproximator

{

/**

Constructs an ArcSinApproximator object

@param anX the number to compute

the arcsin

*/

public ArcSinApproximator(double anX)

{

x = anX;

term = x;

total = x;

i = 3;

}

/**

Determine the next closest guess to the arcsin

@return total the next closest approximation

to the arcsin

*/

public double nextGuess()

{

term = term * x * x * (i - 2) * (i - 2) / ((i - 1) * i);

total = total + term;

i = i + 2;

return total;

}

/**

Get the arcsin of a number

@return total the arcsin

*/

public double getArcSin()

{

while (Math.abs(term) > EPS)

nextGuess();

return total;

}

public static final double EPS = 0.000001;

private double x;

private double term;

private double total;

private int i;

}

ExP8_1.java

import javax.swing.JOptionPane;

/**

This class tests the ArcSinApproximator class

*/

public class ExP8_1

{

public static void main(String[] args)

{

String input = JOptionPane.showInputDialog(

"x:");

double x = Double.parseDouble(input);

ArcSinApproximator as = new ArcSinApproximator(x);

System.out.println("arcsin: " + as.getArcSin());

System.exit(0);

}

}

P8.3

ArcSinApproximator.java

/**

This class provides methods to compute the

arcsin of a number

*/

public class ArcSinApproximator

{

/**

Constructs an ArcSinApproximator object

@param anX the number to compute

the arcsin

*/

public ArcSinApproximator(double anX)

{

x = anX;

term = x;

total = x;

i = 3;

}

/**

Determine the next closest guess to the arcsin

@return total the next closest approximation

to the arcsin

*/

public double nextGuess()

{

term = term * x * x * (i - 2) * (i - 2) / ((i - 1) * i);

total = total + term;

i = i + 2;

return total;

}

/**

Get the arcsin of a number

@return total the arcsin

*/

public double getArcSin()

{

while (Math.abs(term) > EPS)

nextGuess();

return total;

}

public static final double EPS = 0.000001;

private double x;

private double term;

private double total;

private int i;

}

ExP8_3.java

/**

This test class automatically generates test cases for

the ArcSinApproximator class.

*/

public class ExP8_3

{

public static void main(String[] args)

{

double x;

for (x = -1; x <= 1; x = x + 0.1)

{

ArcSinApproximator as = new ArcSinApproximator(x);

double y = as.getArcSin();

System.out.println("arc sine of " + x + " = " + y);

}

}

}

P8.5

ArcSinApproximator.java

/**

This class provides methods to compute the

arcsin of a number

*/

public class ArcSinApproximator

{

/**

Constructs an ArcSinApproximator object

@param anX the number to compute

the arcsin

*/

public ArcSinApproximator(double anX)

{

x = anX;

term = x;

total = x;

i = 3;

}

/**

Determine the next closest guess to the arcsin

@return total the next closest approximation

to the arcsin

*/

public double nextGuess()

{

term = term * x * x * (i - 2) * (i - 2) / ((i - 1) * i);

total = total + term;

i = i + 2;

return total;

}

/**

Get the arcsin of a number

@return total the arcsin

*/

public double getArcSin()

{

while (Math.abs(term) > EPS)

nextGuess();

return total;

}

public static final double EPS = 0.000001;

private double x;

private double term;

private double total;

private int i;

}

ExP8_5.java

import java.util.Random;

/**

This class tests the ArcSinApproximator class by verifying

Math.sin(new ArcSinApproximator(x).getArcSin())

is approximately equal to x

*/

public class ExP8_5

{

public static void main(String[] args)

{

int i;

Random generator = new Random();

for (i = 1; i < 100; i++)

{

final double EPS = 0.000001;

double x = -1 + 2 * generator.nextDouble();

ArcSinApproximator as = new ArcSinApproximator(x);

double y = as.getArcSin();

double z = Math.sin(y);

if (Math.abs(x - z) <= EPS) /* this is too tight--it will fail for some inputs */

System.out.print("Test passed. ");

else System.out.print("Test failed. ");

System.out.println("arc sine of " + x + " = " + y + ", sine is " + z);

}

}

}

P8.7

ArcSinApproximator.java

/**

This class provides methods to compute the

arcsin of a number

*/

public class ArcSinApproximator

{

/**

Constructs an ArcSinApproximator object

@param anX the number to compute

the arcsin

*/

public ArcSinApproximator(double anX)

{

x = anX;

term = x;

total = x;

i = 3;

}

/**

Determine the next closest guess to the arcsin

@return total the next closest approximation

to the arcsin

*/

public double nextGuess()

{

term = term * x * x * (i - 2) * (i - 2) / ((i - 1) * i);

total = total + term;

i = i + 2;

return total;

}

/**

Get the arcsin of a number

@return total the arcsin

*/

public double getArcSin()

{

while (Math.abs(term) > EPS)

nextGuess();

return total;

}

public static final double EPS = 0.000001;

private double x;

private double term;

private double total;

private int i;

}

ExP8_7.java

import javax.swing.JOptionPane;

/**

This class test the ArcSinApproximator class

If the input x is (x < -1 || x > 1), the test fails.

(x < -1 || x > 1) is the domain of the arc sine function

*/

public class ExP8_7

{

public static void main(String[] args)

{

String input = JOptionPane.showInputDialog(

"x:");

double x = Double.parseDouble(input);

ArcSinApproximator as = new ArcSinApproximator(x);

System.out.println("arcsin: " + as.getArcSin());

System.exit(0);

}

}

P8.9

Word.java

import java.util.logging.Logger;

/**

This class describes words in a document.

Similar to using the debugger, adding logging messages to the program

can help find bugs. By logging the info of the variables i, j, text,

end, and ch, one can find 2 errors. The messages are informative

enough to spot the bugs. For example, when the user inputs "hello",

the logging messages state that the variables text = "hell" and

ch = "l" when the user knows that the text variable is suppose to

contain the string "hello" and ch = "o", the last character in the

string. The user can thus understand why "hello" returns only 1 syllable.

*/

*/

public class Word

{

/**

Constructs a word by removing leading and trailing non-

letter characters, such as punctuation marks.

@param s the input string

*/

public Word(String s)

{

logger = Logger.getLogger("global");

int i = 0;

logger.info("i:" + i);

while (i < s.length()

& !Character.isLetter(s.charAt(i)))

i++;

logger.info("i++:" + i);

int j = s.length() - 1;

logger.info("j:" + j);

while (j > i

& !Character.isLetter(s.charAt(j)))

j--;

logger.info("j--:" + j);

text = s.substring(i, j);

logger.info("text:" + text);

}

/**

Returns the text of the word, after removal of the

leading and trailing non-letter characters.

@return the text of the word

*/

public String getText()

{

return text;

}

/**

Counts the syllables in the word.

@return the syllable count

*/

public int countSyllables()

{

int count = 0;

int end = text.length() - 1;

logger.info("end:" + end);

if (end < 0) return 0; // the empty string has no syllables

// an e at the end of the word doesn't count as a vowel

char ch = Character.toLowerCase(text.charAt(end));

logger.info("ch:" + ch);

if (ch == 'e') end--;

logger.info("end:" + end);

boolean insideVowelGroup = false;

for (int i = 0; i <= end; i++)

{

ch = Character.toLowerCase(text.charAt(i));

String vowels = "aeiouy";

if (vowels.indexOf(ch) >= 0)

{

// ch is a vowel

if (!insideVowelGroup)

{

// start of new vowel group

count++;

insideVowelGroup = true;

}

}

}

// every word has at least one syllable

if (count == 0)

count = 1;

return count;

}

private String text;

}

WordTest.java

import java.util.StringTokenizer;

import javax.swing.JOptionPane;

/**

This program tests the countSyllables method of the Word class.

*/

public class WordTest

{

public static void main(String[] args)

{

String input

= JOptionPane.showInputDialog("Enter a sentence");

StringTokenizer tokenizer = new StringTokenizer(input);

while (tokenizer.hasMoreTokens())

{

String token = tokenizer.nextToken();

Word w = new Word(token);

int syllables = w.countSyllables();

System.out.println("Syllables in " + token + ": "

+ syllables);

}

System.exit(0);

}

}

P8.11

ArcSinApproximator.java

/**

This class provides methods to compute the

arcsin of a number

*/

public class ArcSinApproximator

{

/**

Constructs an ArcSinApproximator object

@param anX the number to compute

the arcsin

*/

public ArcSinApproximator(double anX)

{

x = anX;

term = x;

total = x;

i = 3;

}

/**

Determine the next closest guess to the arcsin

@return total the next closest approximation

to the arcsin

*/

public double nextGuess()

{

term = term * x * x * (i - 2) * (i - 2) / ((i - 1) * i);

total = total + term;

i = i + 2;

/**

At this point, i == 17,

*/

return total;

}

/**

Get the arcsin of a number

@return total the arcsin

*/

public double getArcSin()

{

while (Math.abs(term) > EPS)

nextGuess();

return total;

}

public static final double EPS = 0.000001;

private double x;

private double term;

private double total;

private int i;

}

ExP8_11.java

import javax.swing.JOptionPane;

/**

This class test the ArcSinApproximator class

*/

public class ExP8_11

{

public static void main(String[] args)

{

String input = JOptionPane.showInputDialog(

"x:");

double x = Double.parseDouble(input);

ArcSinApproximator as = new ArcSinApproximator(x);

System.out.println("arcsin: " + as.getArcSin());

System.exit(0);

}

}