here

advertisement
Chapter 8.
Question 8.3.
Class Customers
Class Products
Class Invoices
Class Sales
Question 8.7.
java.lang.Object
java.lang.Number
java.lang.Integer
public final class Integer extends Number implements Comparable, Serializable
Question 8.13.
public void print()
You rely on System.out. A method that relies on System.out won’t work in an embedded system, such as
the computer inside an automatic teller machine.
public void print(PrintStream stream)
The side effect is modifying the stream parameter
public String toString()
There is no side effect
Question R8.14.
No. Side effects cannot be completely eliminated.
Question 8.15(a).
public static double sqrt(double x)
/**
Calculate Square Root.
@param x the value to compute Square Root
(Precondition: x >= 0)
*/
Question 8.15(c).
public static double slope(Line2D.Double a)
/**
Calculate the Slope of the line a.
@param a the line to compute Slope
(Precondition: a.getX1() != a.getX2())
*/
Question R8.16(b)
Math.tan
/**
Calculate the Tangent of the line an angle X.
@param X the angle to calculate Tangent
(Precondition: X !=((2*n+1)*pi/2) with n=0,1,2,3…)
*/
Question R8.16(d)
Math.pow(double Base,double Exponent)
Nothing.
Base and Exponent might be doubles.
(x > 0, or x = 0 and y > 0, or x < 0 and y is an integer)
Question R8.17.
Integer.parseInt(String s):
The string can be interpreted as an integer
StringTokenizer.nextToken():
hasMoreTokens() must be true
Random.nextInt(int n):
n>0
String.substring(int begin, int pastEnd):
begin >= 0 && pastEnd <= String.length() && begin <= pastEnd
Question R8.20.
x and y aren’t reference parameters; any change inside method is lost when finished.
x and y are value parameter, a copy of them is created inside of method and the original values are
preserved without change.
Question R8.22.
Question R8.26.
copyValueOf(char[] data)
Returns a String that represents the character sequence in the array specified.
copyValueOf(char[] data, int offset, int count)
Returns a String that represents the character sequence in the array specified.
valueOf(boolean b)
Returns the string representation of the boolean argument.
valueOf(char c)
Returns the string representation of the char argument.
valueOf(char[] data)
Returns the string representation of the char array argument.
valueOf(char[] data, int offset, int count)
Returns the string representation of a specific subarray of the char array argument.
valueOf(double d)
Returns the string representation of the double argument.
valueOf(float f)
Returns the string representation of the float argument.
valueOf(int i)
Returns the string representation of the int argument.
valueOf(long l)
Returns the string representation of the long argument.
valueOf(Object obj)
Returns the string representation of the Object argument.
Question R8.28.
public class X
{
public int f()
{
int n = 1;
return n;
}
public int g(int k)
{
int a;
for (int n = 1; n <= k; n++)
a = a + n;
//error: The local variable a may not have been initialized
return a;
//error: The local variable a may not have been initialized
}
public int h(int n)
{
int b;
for (int n = 1; n <= 10; n++) // error: Duplicate local variable n
b = b + n;
return b + n;
}
public int k(int n)
{
if (n < 0)
{
int k = -n;
int n = (int) (Math.sqrt(k)); // error: Duplicate local variable n
return n;
}
else return n;
}
public int m(int k)
{
int a;
for (int n = 1; n <= k; n++)
a = a + n; //error: The local variable a may not have been initialized
for (int n = k; n >= 1; n++)
a = a + n; //error: The local variable a may not have been initialized
return a; //error: The local variable a may not have been initialized
}
private int n;
}
Questions R8.31.
The exception is reported, and the remaining methods continue to be executed. This is an advantage
over a simple tester class whose main method would terminate when an exception occurs, skipping all
remaining tests.
Question P8.5.
import java.util.Scanner;
public class Geometry {
public static double sphereVolume(double r){
return 4 * Math.PI * r * r * r / 3;
}
public static double sphereSurface(double r){
return 4 * Math.PI * r * r;
}
public static double cylinderVolume(double r, double h){
return Math.PI * r * r * h;
}
public static double cylinderSurface(double r, double h){
return 2 * Math.PI * r * (r + h);
}
public static double coneVolume(double r, double h){
return Math.PI * r * r * h / 3;
}
public static double coneSurface(double r, double h){
return Math.PI * r * r + Math.PI * r * Math.sqrt(r*r + h*h);
}
public static void main( String [] args ) {
Scanner sc = new Scanner( System.in );
double r=0;;
double h=0;
System.out.println("Insert r: "); r = sc.nextDouble();
System.out.println("Insert h: "); h = sc.nextDouble();
System.out.println("Sphere Volume
: " + Geometry.sphereVolume(r));
System.out.println("Sphere Surface : " + Geometry.sphereSurface(r));
System.out.println("Cylinder Volume : " + Geometry.cylinderVolume(r,
h));
System.out.println("Cylinder Surface: " + Geometry.cylinderSurface(r,
h));
System.out.println("Cone Volume
: " + Geometry.coneVolume(r, h));
System.out.println("Cone Surface
: " + Geometry.coneSurface(r, h));
}
}
Question P8.10.
import java.awt.geom.Point2D;
import java.util.Scanner;
public class Geometry {
public static double sphereVolume(double r){
return 4 * Math.PI * r * r * r / 3;
}
public static double sphereSurface(double r){
return 4 * Math.PI * r * r;
}
public static double cylinderVolume(double r, double h){
return Math.PI * r * r * h;
}
public static double cylinderSurface(double r, double h){
return 2 * Math.PI * r * (r + h);
}
public static double coneVolume(double r, double h){
return Math.PI * r * r * h / 3;
}
public static double coneSurface(double r, double h){
return Math.PI * r * r + Math.PI * r * Math.sqrt(r*r + h*h);
}
public static double angle(Point2D.Double p, Point2D.Double q){
/*
* Preconditions p.getX != q.getX
*/
return (p.getY()-q.getY())/(p.getX()-q.getY());
}
public static double slope(Point2D.Double p, Point2D.Double q){
/*
* Preconditions p.getX != q.getX
*/
return (p.getY()-q.getY())/(p.getX()-q.getY());
}
}
Both methods return the same value
Question P8.13.
Consider the following algorithm for computing xn for an integer n.
If n < 0, xn is 1/x–n.
If n is positive and even, then xn = (xn/2)2.
If n is positive and odd, then xn = xn–1 ⋅ x.
Implement a static method double intPower(double x, int n) that uses this algorithm. Add it to a class
called Numeric.
public class Numeric{
static double intPower(double x, int n){
double power=0;
if(n<0) power = 1/Math.pow(x, -n);
if(n>=0)
if(n%2==0) power = Math.pow(Math.pow(x, n/2),2);
else power = Math.pow(x, n-1)*x;
return power;
}
}
Question P8.18.
import static org.junit.Assert.*;
import org.junit.Test;
public class TaxReturnTest {
@Test
public void testGetTax1() { /*SINGLE UNDER LIMIT*/
TaxReturn tr = new TaxReturn(20000,1);
assertTrue(tr.getTax() == 200);
}
@Test
public void testGetTax2() { /*SINGLE OVER LIMIT*/
TaxReturn tr = new TaxReturn(40000,1);
assertTrue(tr.getTax() == 600);
}
@Test
public void testGetTax3() { /*MARRIAGED UNDER LIMIT*/
TaxReturn tr = new TaxReturn(40000,2);
assertTrue(tr.getTax() == 400);
}
}
Download