Chapter 3 Reference Answers

Section A
==========

1a.
// Commission.java
// Chapter 3, Section A, Exercise 1a
// Creates a class for computing commission
public class Commission {
    public static void main(String[] args){
        double sales = 50000.0;
        double commission = 0.0;
        int rate = 5;
        commission = computeCommission(sales, rate);
        System.out.println("Commission on sales of "
                + sales
                + " with a rate of " + rate + "%"
                + " is "
                + commission);
        double drate = 5.0
        commission = computeCommission(sales, drate);
        System.out.println("Commission on sales of "
            + sales
            + " with a rate of " + drate + "%"
            + " is "
            + commission);
    }
    public static double computeCommission(double s, double r) {
        return (( (double) r / 100.0) * s);
    }
    public static double computeCommission(double s, int r) {
        return (( (double) r / 100.0) * s);
    }
}
1b.
// Commission2.java
// Chapter 3, Section A, Exercise 1b
// Creates a class for computing commission
public class Commission2 {
    public static void main(String[] args) {
        double sales = 50000.0;
        double commission = 0.0;
        int rate = 5;
        commission = computeCommission(sales, rate);
        System.out.println("Commission on sales of "
                + sales
                + " with a rate of " + rate + "%"
                + " is "
                + commission);
        commission = computeCommission(sales);
        System.out.println("Commission on sales of "
                + sales
                + " with a rate of 7.5%"
                + " is "
                + commission);
    }
    public static double computeCommission(double s, double r) {
        return (( (double) r / 100.0) * s);
    }
    public static double computeCommission(double s, int r) {
        return (( (double) r / 100.0) * s);
    }
    public static double computeCommission(double s) {
        return (( 7.5 / 100.0) * s);
    }
}
2.
// Pay.java
// Chapter 3, Section A, Exercise 2
public class Pay {
    public static void main(String[] args) {
        double workHours = 40.0;
        double hourlyRate = 4.65;
        double withholdingRate = .15;
        double grossPay;
        double netPay;
        netPay = computeNetPay(workHours,
                hourlyRate,withholdingRate);
    System.out.println("Net pay for "
            + workHours + " hours is "
            + netPay);
    netPay = computeNetPay(workHours,hourlyRate);
    System.out.println("Net pay for "
            + workHours + " hours is "
            + netPay);
    netPay = computeNetPay(workHours);
    System.out.println("Net pay for "
            + workHours + " hours is "
            + netPay);
    }
    public static double computeNetPay(double hrs, double pr, double wr){
        return (hrs * pr) - (hrs * pr * wr);
    }
    public static double computeNetPay(double hrs, double pr) {
        return (hrs * pr) - (hrs * pr * .15);
    }
    public static double computeNetPay(double hrs) {
        return (hrs * 4.65) - (hrs * 4.65 * .15);
    }
}
3a -3c.
// Household.java
// Chapter 3, Section A, Exercise 3a, b, c
// additional constructor form exercises 3b and 3c
// are commented below
public class Household {
    int occupants;
    double annualIncome;
// constructor added for exercise 3b
    public Household(int occ, double inc) {
        occupants = occ;
        annualIncome = inc;
    }
// constructor added for exercise 3a
    public Household(int occ) {
        occupants = occ;
        annualIncome = 0.0;
    }
    public Household() {
        occupants = 1;
        annualIncome = 0.0;
    }
    public int getOccupants() {
        return occupants;
    }
    public void setOccupants(int occ) {
        occupants = occ;
    }
    public double getAnnualIncome() {
        return annualIncome;
    }
    public void setAnnualIncome(double inc) {
        annualIncome = inc;
    }
}
// TestHousehold.java
// Chapter 3, Section A, Exercise 3a-3c
public class TestHousehold {
    public static void main(String[] args) {
        Household house = new Household();
        System.out.println("The house has " + house.getOccupants()
                + " occupants and an income of "
                + house.getAnnualIncome());
        house.setAnnualIncome(55000);
        house.setOccupants(3);
// code to test constructor added for exercise 3a
        Household house2 = new Household(2);
        System.out.println("The second house has " + house2.getOccupants()
                + " occupants and an income of "
                + house2.getAnnualIncome());
// code to test constructor added for exercise 3b
        Household house3 = new Household(7,80000.0);
        System.out.println("The third house has " + house3.getOccupants()
                + " occupants and an income of "
                + house3.getAnnualIncome());
    }
}
4.
// Box.java
// Chapter 3, Section A, Exercise 4
public class Box{
    int length, width, height;
    public Box(int l){
        length = l;
        width = 0;
        height = 0;
    System.out.println("Line created...");
}
    public Box(int l, int w) {
        length = l;
        width = w;
        height = 0;
        System.out.println("Rectangle created...");
    }
    public Box(int l, int w, int h){
        length = l;
        width = w;
        height = h;
        System.out.println("Box created...");
    }
}
// TestBox.java
// Chapter 3, Section A, Exercise 4
public class TestBox {
    public static void main(String[] args) {
        Box box1 = new Box(2);
        Box box2 = new Box(12,5);
        Box box3 = new Box(10,12,20);
    }
}

5. The variable scopeInt declared in the Scope class is overridden 
by the local variable declared in the
scopeDisplay( ) method. The result is two different variables with
values of 1 and 10. The scopeInt local
to the scopeDisplay( ) method with a value of 10 is printed.

Section B
===========
1. a 8. d 15. a
2. a 9. a 16. b
3. d 10. c 17. d
4. a 11. c 18. d
5. b 12. c 19. d
6. d 13. c 20. a
7. c 14. b

1.
// Shirt.java
// Chapter 3, Section B, Exercise 1
public class Shirt {
    private int collarSize, sleeveLength;
    private String material;
    public Shirt(int col, int len) {
        collarSize = col;
        sleeveLength = len;
        material = new String("cotton");
    }
    public void display(){
        System.out.println("The collar size is "
                + collarSize
                + ", the sleeve length is " + sleeveLength
                + ", and the material is " + material);
    }
}
// TestShirt.java
// Chapter 3, Section B, Exercise 1
public class TestShirt {
    public static void main(String[] args) {
        Shirt Shirt1 = new Shirt(16,34);
        Shirt Shirt2 = new Shirt(15,33);
        Shirt Shirt3 = new Shirt(14,30);
        Shirt1.display();
        Shirt2.display();
        Shirt3.display();
    }
}
2.
// CheckingAccount.java
// Chapter 3, Section B, Exercise 2
public class CheckingAccount {
    private int accountNumber;
    private double balance;
    private static double minBalance = 200.0;
    public CheckingAccount(int num, double bal){
        accountNumber = num;
        balance = bal;
    }
    public void display() {
        System.out.println("The account number is "
                + accountNumber
                + ", the balance is " + balance
                + ",\nand the minimum balance is " + minBalance);
    }
}
// TestAccount.java
// Chapter 3, Section B, Exercise 2
public class TestAccount {
    public static void main(String[] args) {
        CheckingAccount Account1 = new CheckingAccount(123,5000);
        CheckingAccount Account2 = new CheckingAccount(234,2500);
        Account1.display();
        Account2.display();
    }
}
3.
// MathTest.java
// Chapter 3, Section B, Exercise 3
import java.math.*;
public class MathTest {
    public static void main(String[] args) {
        System.out.println( "The square root of 30 is "
                + Math.sqrt(30)
                + "\nthe sine of 100 is " + Math.sin(100)
                + "\nthe cosine of 100 is " + Math.cos(100)
                + "\nthe floor of 44.7 is " + Math.floor(44.7)
                + "\nthe ceiling of 44.7 is " + Math.ceil(44.7)
                + "\nthe round of 44.7 is " + Math.round(44.7)
                + "\nthe largest of K and 70 is " + Math.max('K',70)
                + "\nthe minimum of K and 70 is " + Math.min('K',70));
    }
}
4.
// Summer.java
// Chapter 3, Section B, Exercise 4
import java.util.*;
public class Summer {
    public static void main (String args[]){
        Date today = new Date();
        Date summer = new Date(99,5,21);
        long diff = summer.getTime() - today.getTime();
        System.out.println("Summer is in "
                + (diff / 1000 /60 / 60 / 24)
                + " days");
    }
}
5.
// YearEnd.java
// Chapter 3, Section B, Exercise 4
import java.util.*;
public class YearEnd {
    public static void main (String args[]) {
        Date today = new Date();
        Date newyear = new Date(97,0,1);
        long diff = newyear.getTime() - today.getTime();
        System.out.println("There are "
                + (diff / 1000 /60 / 60 / 24)
                + " days left in the year");
    }
}
6. The output of the program is:
1
2
-1
-1
The Math.round() function adds .5 to its argument and returns the 
largest integer that is less than or equal to the result. Therefore
1.49 + .5 equals 1.99, and 1 is the largest integer that is less than
or equal to 1.99. 1.50 + .5 = 2, therefore 2 is returned, since it is 
the largest integer that is less than or equal to 2. -1.49 + .5 
equals -.99, and -1 is the largest integer that is less than or equal 
to -.99. -1.50 + .5 = -1, and -1 is returned, since it is the largest 
integer that is less than or equal to -1.
7. The output of the program is:
2
2
-1
-1
8. The Math.ceil() function returns the smallest integer that is 
greater than or equal to its argument.
The output of the program is:
1
1
-2
-2
The Math.floor() function returns the largest integer that is less 
than or equal to its argument
9.
// EmployeeWithDate.java
// Chapter 3, Section B, Exercise 9
import java.util.*;
public class EmployeeWithDate {
    static private int COMPANY_ID = 12345;
    private int empNum;
    private double empsalary;
    Employee(int num) {
        empNum = num;
    }
    public void showCompanyID() {
        Date today = new Date();
        System.out.println("Worker " + empNum
                + " has company ID " + COMPANY_ID
                + "\nToday is: " + today.toGMTString());
    }
}
// UseEmployeeWithDate.java
/ Chapter 3, Section B, Exercise 9
import java.util.*;
public class UseEmployeeWithDate {
    public static void main (String args[]) {
        EmployeeWithDate a = new EmployeeWithDate(100);
        EmployeeWithDate b = new EmployeeWithDate(200);
        a.showCompanyID();
        b.showCompanyID();
    }
}
Debugging Exercises
10a.
public class DebugThree1{
// This program assigns values to two variables
// and performs mathematical operations with them

    public static void main(String args[]){
        int x = 5;
        int y = 8;
        System.out.println("adding " +(x + y));
        int z = 19;
        System.out.println("subtracting " + (z - y));
        System.out.println("dividing " + (z/x));
        System.out.println("multiplying " + (x * z));
    }
}
10b.
public class DebugThree2 {
    public static void main(String args[]) {
        double radius = 12.6;
        System.out.println("Circle statistics");
        double area = java.lang.Math.PI * radius * radius;
        System.out.println("area is " + area);
        double diameter = 2 * radius;
        System.out.println("diameter is " + diameter);
    }
}
10c.
import java.util.*;
public class DebugThree3 {
    public static void main(String args[]) {
        Date toDay = new Date();
        Date graduationDay = new Date(102,4,16);
        System.out.println(toDay);
        System.out.print("Current month is ");
        System.out.println(toDay.getMonth());
        System.out.print("Current day is ");
        System.out.println(toDay.getDate());
        System.out.print("Current year is ");
        System.out.println(toDay.getYear());
        System.out.print("Graduation month is ");
        System.out.println( graduationDay.getMonth());
        System.out.print("Graduation day is ");
        System.out.println( graduationDay.getDate());
        System.out.print("Graduation year is ");
        System.out.println( graduationDay.getYear());
    }
}
10d.
public class DebugThree4{
// This class discounts prices by 10%

    public static void main(String args[]) {
        int price = 100;
        double price2 = 100.00;
        tenPercentOff(price);
        tenPercentOff(price2);
    }
    public static void tenPercentOff(int p) {
        double newPrice = p * .90;
        System.out.println("Ten percent off");
        System.out.println("New price is " + newPrice);
    }
    public static void tenPercentOff(double p) {
        double newPrice = p * .90;
        System.out.println("Ten percent off");
        System.out.println("New price is " + newPrice);
    }
}