Chapter 5 Reference Answers

1. b 8. c 15. b
2. a 9. d 16. d
3. b 10. a 17. c
4. c 11. b 18. a
5. d 12 b 19. c
6. a 13. d 20. b
7. a 14. d
// Chapter 5, Section B, Exercise 1
public class IntArray {
    public static void main (String[] args) throws Exception {
        int[] numbers = new int[5];
        int i;
        for (i = 0; i < 5; i++)
            numbers[i] = i + 10;
        for (i = 0; i < 5; i++)
            System.out.print("" + numbers[i] + " ");
        System.out.print("\n");
        for (i = 4; i >= 0; i--)
            System.out.print("" + numbers[i] + " ");
            System.out.print("\n");
    }
}
2.
// Choice.java
// Chapter 5, Section A, Exercise 2
public class Choice {
    public static void main (String[] args) throws Exception {
        char[] sizes = {'S','M','L','X'};
        double[] prices = {6.99,8.99,12.50,15.00};
        char selection;
        int i;
        System.out.print("Enter pizza size (S,M,L,X): ");
        selection = (char)System.in.read();
        System.in.read();
        System.in.read();
        for (i = 0; i < 4; i++)
            if (selection == sizes[i]) {
                System.out.println("The price is " + prices[i]);
                return;
            }
        System.out.println("Invalid Entry");
    }
}
3a-b.
// Taxpayer.java
// Chapter 5, Section A, Exercise 3
public class Taxpayer {
    private int social;
    private double income;
    Taxpayer(int s, double i) {
        social = s;
        income = i;
    }
    public int getSocial() {
        return social;
    }
    public double getIncome() {
        return income;
    }
}
// UseTaxpayer.java
// Chapter 5, Section A, Exercise 3a
public class UseTaxpayer {
    public static void main (String[] args) {
        Taxpayer[] workers = new Taxpayer[10];
        int i;
        for (i = 0; i < 10; i++)
            workers[i] = new Taxpayer(999999999,0);
        for (i = 0; i < 10; i++)
            System.out.println("Taxpayer " + i
                + " has SSN " + workers[i].getSocial()
                + " and an income of " + workers[i].getIncome());
    }
}
// UseTaxpayer2.java
// Chapter 5, Section A, Exercise 3b
public class UseTaxpayer2 {
    public static void main (String[] args) {
        Taxpayer[] workers = new Taxpayer[10];
        int i;
        for (i = 0; i < 10; i++)
            workers[i] = new Taxpayer(i + 1,(i + 1) * 10000.0);
        for (i = 0; i < 10; i++)
            System.out.println("Taxpayer " + i
                        + " has SSN " + workers[i].getSocial()
                        + " and an income of " + workers[i].getIncome());
    }
}
4.
// Prices.java
// Chapter 5, Section A, Exercise 3
public class Prices {
    public static void main (String[] args) {
        double[] prices = {2.34,7.89,3.55,6.99,8.99,2.39,
                1.29,11.88,56.35,12.33,4.50,1.19,1.79,2.59,7.10,
                9.99,4.55,12.39,4.44,5.55};
        double average;
        double total = 0.0;
        int i;
        for (i = 0; i < 20; i++) {
            total += prices[i];
            if (prices[i] < 5.00)
            System.out.println(prices[i] + " is less than 5.00");
        }
        average = total / (i + 1);
        System.out.println("\nThe average price is " + average + "\n");
        for (i = 0; i < 20; i++) {
            if (prices[i] > average)
                System.out.println(prices[i] + " is greater than the average");
        }
    }
}
5a-c.
// Student.java
// Chapter 5, Section A, Exercise 5a-c
// code added in each exercise is commented
public class Student {
    static int nextStudent = 1;
    private int studentNumber;
    private char[] studentGrades = new char[5];
    // added in exercise 5b
    static char[] grades = {'A','B','C','D','F'};
    static int[] points = {4,3,2,1,0};
    private int[] studentPoints = new int[5];
    
    Student() {
        studentNumber = nextStudent++;
    }
    public void setGrade(int sub, char grade) {
        studentGrades[sub] = grade;
        // added in exercise 5b
        for (int i = 0; i < 5; i++)
            if (grade == grades[i])
                studentPoints[sub] = points[i];
    }
    public int getNumber() {
        return studentNumber;
    }
    // printGrades() added in exercise 5b
    public void printGrades() {
        System.out.println("Student number " + studentNumber);
        int total = 0;
        for (int i = 0; i < 5; i++) {
            System.out.println("\t" + " grade " + (i + 1)
                                + " is "
                                + studentGrades[i]
                                + " for "
                                + studentPoints[i] + " points ");
            total += studentPoints[i];
    }
    // added in exercise 5c
    System.out.println("\tGrade point average is " + (double)(total/5.0));
    }
}
// GradePoint.java
// Chapter 5, Section A, Exercise 5a-c
// GradePoint.java
// Chapter 5, Section A, Exercise 5a-c
public class GradePoint {
    public static void main (String[] args) throws Exception {
        char[] grades = {'A','B','C','D','F'};
        int i;
        Student[] theClass = new Student[10];
        for (i = 0; i < 10; i++)
             theClass[i] = new Student();
        double[] classGPAs = new double[10];
        char g;
        int x, j;
        boolean match;
        for (i = 0; i < 10; i++) {
            for (j = 0; j < 5; j++) {
                System.out.print("Enter grade " + (j + 1)
                        + " for student " + theClass[i].getNumber() + ": ");
                g = (char)System.in.read();
                System.in.read();
                System.in.read();
                match = false;
                for (x = 0; x < 5; x++)
                    if (g == grades[x])
                        match = true;
                if (match == false) {
                    --j;
                    System.out.println("Please reenter grade");
                }
                else
                    theClass[i].setGrade(j,g);
            }
        }
        for (i = 0; i < 10; i++) {
            theClass[i].calcGPA();
            classGPAs[i] = theClass[i].getGPA();
            theClass[i].printGrades();
        }
    }
}
6a-b.
// Quiz.java
// Chapter 5, Section A, Exercise 6a-b
public class Quiz {
    public static void main (String[] args) throws Exception {
        char[] answers = {'A','B','C','A','A','A','B','C','A','A'};
        char[] responses = new char[10];
        String[] question = new String[10];
        question[0] = "5 + 2=\n\tA. 7\n\tB. 5\n\tC. 6";
        question[1] = "3 + 3=\n\tA. 7\n\tB. 6\n\tC. 5";
        question[2] = "6 + 1=\n\tA. 17\n\tB. 5\n\tC. 7";
        question[3] = "8 + 8=\n\tA. 16\n\tB. 15\n\tC. 6";
        question[4] = "5 * 2=\n\tA. 10\n\tB. 15\n\tC. 26";
        question[5] = "5 * 3=\n\tA. 15\n\tB. 5\n\tC. 6";
        question[6] = "5 * 4=\n\tA. 7\n\tB. 20\n\tC. 6";
        question[7] = "5 * 5=\n\tA. 17\n\tB. 15\n\tC. 25";
        question[8] = "5 * 6=\n\tA. 30\n\tB. 25\n\tC. 36";
        question[9] = "5 * 7=\n\tA. 35\n\tB. 25\n\tC. 36";
        int i;
        char g;
        boolean match;
        for (i = 0; i < 10; i++) {
            System.out.print("\nWhat is the result of "
                    + question[i] + "\n\n\tEnter answer (A,B or C): ");
            g = (char)System.in.read();
            System.in.read();
            System.in.read();
            responses[i] = g;
            match = false;
            if (g == answers[i])
                match = true;
            if (match == true)
                System.out.println("Correct! ");
            else
                System.out.println("Wrong, correct answer is "
                                    + answers[i]);
        }
        // added in exercise 6b
        int numberCorrect = 0;
        for (i = 0; i < 10; i++)
            if (responses[i] == answers[i])
        numberCorrect++;
        System.out.println("\nYour score is "
                + numberCorrect + " out of 10");
    }
}
7a.
// EnterNumbers.java
// Chapter 5, Section A, Exercise 7a
public class EnterNumbers {
    public static void main (String[] args) throws Exception {
        char[] numbers = new char[10];
        int i = 0;
        char g = ' ';
        while (g != 'X' && g != 'x') {
            System.out.print("\nEnter a number 1 thru 9 or X to EXIT: ");
            g = (char)System.in.read();
            System.in.read();
            System.in.read();
            if ((g < '1' || g > '9')&&(g != 'X' && g != 'x')) {
                System.out.println("Invalid entry ");
                continue;
            }
            if (i > 9) {
                System.out.println("You have entered the maximum entries");
                break;
            }
            numbers[i++] = g;
        }
    }
}

7b-c.
// EnterNumbers2.java
// Chapter 5, Section A, Exercise 7b, 7c
public class EnterNumbers2 {
    public static void main (String[] args) throws Exception {
        char[] numbers = new char[10];
        char selection = 'F';
        while (selection < 'A' || selection > 'E') {
            System.out.println("\n\tA. Enter numbers");
            System.out.println("\tB. Modify a number");
            System.out.println("\tC. Delete a number");
            System.out.println("\tD. Display a number");
            System.out.println("\tE. EXIT");
            System.out.print("\tEnter option (A,B,C,D,E): ");
            selection = (char)System.in.read();
            System.in.read();System.in.read();
             switch(selection) {
                case 'A':
                    selection = 'F';
                    fillArray(numbers);
                    break;
                case 'B':
                    selection = 'F';
                    modifyNum(numbers);
                    break;
                case 'C':
                    selection = 'F';
                    deleteNum(numbers);
                    break;
                case 'D':
                    selection = 'F';
                    displayNum(numbers);
                    break;
                case 'E':
                    return;
            }
        }
    }
    public static void fillArray(char[] n) throws Exception {
        int i = 0;
        char g = ' ';
        while (g != 'X' && g != 'x') {
            System.out.print("\nEnter a number 1 thru 9 or X to EXIT: ");
            g = (char)System.in.read();
            System.in.read();
            System.in.read();
            if ((g < '1' || g > '9')&& (g != 'X' && g != 'x') ){
                System.out.println("Invalid entry ");
                continue;
            }
            if (i > 9) {
                System.out.println("You have entered the maximum entries");
                break;
            }
            n[i++] = g;
        }
    }
    public static void modifyNum(char[] n) throws Exception {
        char num = 0;
        char input = ' ';
        while (input < '0' || input > '9') {
            System.out.print("\nEnter element to modify (0 thru 9): ");
            input = (char)System.in.read();
            System.in.read();
            System.in.read();
        }
        while (num < '1' || num > '9') {
            System.out.print("\nEnter new number (1 thru 9): ");
            num = (char)System.in.read();
            System.in.read();
            System.in.read();
        }
        n[((int)input)-48] = num;
    }
    public static void deleteNum(char[] n) throws Exception {
        char input = ' ';
        while (input < '1' || input > '9') {
            System.out.print("\nEnter number to delete (0 thru 9): ");
            input = (char)System.in.read();
            System.in.read();
            System.in.read();
        }
        n[((int)input)-48] = '0';
    }
    public static void displayNum(char[] n) throws Exception {
        char input = ' ';
        while(input < '1' || input > '9') {
            System.out.print("\nEnter number to display (0 thru 9): ");
            input = (char)System.in.read();
            System.in.read();
            System.in.read();
        }
        System.out.println("The number is " + n[((int)input)-48]);
    }
}
****************************
section B
Questions
1. b 7. c 13. d
2. d 8. b 14. d
3. a 9. c 15. c
4. c 10. c 16. b c
5. b 11. c 17. d
6. a 12. d 18. a

public class Ex1_5b {
    public static void main (String args[]) throws Exception {
        char[] vowels = {'a','e','i','o','u' };
        int i = 0;
        char g = ' ';
        boolean isVowel = false;
        System.out.print("Enter a character: ");
        g = (char)System.in.read();
        System.in.read();
        System.in.read();
        for (i = 0; i < 5; i++) {
            if (g == vowels[i])
                isVowel = true;
        }
        if (isVowel == true)
            System.out.println(g + " is a vowel");
        else
            System.out.println(g + " is NOT a vowel");
    }
}
2.
// Ex2_5b.java
// Chapter 5, Section B, Exercise 2
public class Ex2_5b {
    public static void main (String args[]) throws Exception {
        char[] characters = new char[40];
        int i = 0, alphabet = 0;
        for (i = 0; i < 40; i++)
            characters[i] = (char)(i + 40);
        for (i = 0; i < 40; i++) {
            if (characters[i] >= 'A' && characters[i] <= 'z')
                alphabet++;
            System.out.print(characters[i] + (i != 39?", ":"\n"));
        }
        System.out.println(alphabet + " of the above are in the alphabet");
    }
}
3.
// Ex3_5b.java
// Chapter 5, Section B, Exercise 3
public class Ex3_5b {
    public static void main (String args[]) throws Exception {
        int i = 0;
        char[] temp = new char[10];
        System.out.print("Enter your first name (up to 10 characters): ");
        while ((temp[i++] = (char)System.in.read()) != '\n');
        System.out.print("Hello ");
        for (i = 0; i < 10; i++)
            System.out.print(temp[i]);
    }
}
// Ex4_5b.java
// Chapter 5, Section B, Exercise 4
public class Ex4_5b {
    public static void main(String[] args)throws Exception {
        String[] names = {"Washington","Howard",
                            "Fine","Marx",
                            "Clinton","Reagan",
                            "Kennedy","Nixon",
                            "Ford","Bush",
                            "Howard","Fine",
                            "Marx","Clinton",
                            "Reagan","Kennedy",
                            "Nixon","Ford",
                            "Bush","Truman"};
        int[] IDnum = {10,11,12,13,14,15,16,17,18,19,20,
                        21,22,23,24,25,26,27,28,29};
        char a1, a2; // the two digits of the two numbers
        int n1, n2; // the digits as numbers
        int numA; // the number
        int ta;
        System.out.print("Enter two-digit ID (10-29): ");
        a1 = (char)System.in.read(); // read first digit
        a2 = (char)System.in.read(); // read second digit
        ta = System.in.read();
        ta = System.in.read();
        n1 = Character.digit(a1, 10); // convert to a number
        n2 = Character.digit(a2, 10); // convert to a number
        numA = (n1*10) + n2;
        int choice = 0, i;
        for (i = 0; i < 20; i++)
            if (IDnum[i] == numA)
                choice = i;
            System.out.println("The name is: " + names[choice]);
    }
}
5.
// Ex5_5b.java
// Chapter 5, Section B, Exercise 5
import java.util.*;
public class Ex5_5b {
public static void main(String[] args) {
    String[] days = {"Sunday","Monday",
                    "Tuesday","Wednesday",
                    "Thursday","Friday",
                    "Saturday"};
    int theDay;
    // create a Date object for Dec 20, 1999
    // a Sunday
    Date today = new Date(99,11,20);
    theDay = today.getDay();
    System.out.println("The weekday is "
                        + days[theDay]);
    }
}
6.
// Ex6_5b.java
// Chapter 5, Section B, Exercise 6
public class LearnJava {
    public static void main(String[] args)throws Exception {
        String[] reasons = {"No pointer math",
                            "No destructors",
                            "Nice logo",
                            "Free SDK",
                            "Runs on the web",
                            "Reminds me of coffee",
                            "Multiple inheritance ",
                            "Duke is cool",
                            "Great support",
                            "Everything is a class"};
        char choice;
        System.out.print("\nEnter 0 thru 9: ");
        choice = (char)System.in.read();
        System.in.read();
        System.in.read();
		//0-9 (char type vaule is 48-57)
        System.out.println("The reason I like Java: "
                            + reasons[choice - 48]); 
    }
}