Solutions to the Try It Out Exercises in Java For Dummies, 8th Edition
by Barry Burd

Chapter 11: Using Arrays to Juggle Values

In this chapter:

Too Many Words

import java.util.Scanner;

public class NumberOfWords {
    public static void main(String[] args) {
        int[] words = {0, 296, 342, 405, 363, 350, 323, 101};

        var keyboard = new Scanner(System.in);
        System.out.print("Which page? ");
        int input = keyboard.nextInt();
        System.out.println("That page has " + words[input - 1] + " words.");
    }
}

Spell It Backwards

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import static java.lang.System.out;

public class ShowGuests {

    public static void main(String[] args) throws IOException {
        int[] guests = new int[10];
        var diskScanner = new Scanner(new File("GuestList.txt"));

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            guests[roomNum] = diskScanner.nextInt();
        }

        out.println("Room\tGuests");

        for (int roomNum = 9; roomNum >= 0; roomNum--) {
            out.print(roomNum);
            out.print("\t");
            out.println(guests[roomNum]);
        }

        diskScanner.close();
    }
}

Make Your Marks

import java.util.Scanner;

public class MakeYourMarks {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);
        int[] numbers = new int[5];

        for (int i = 0; i < 5; i++) {
            System.out.print("Enter a number: ");
            numbers[i] = keyboard.nextInt();
        }

        System.out.println();

        for (int i = 0; i < 5; i++) {
            for (int count = 0; count < numbers[i]; count++) {
                System.out.print("*");
            }
            System.out.println();

        }
    }
}

Rooms with a View

import com.allmycode.dummiesframe.DummiesFrame;

public class Main {

    public static void main(String[] args) {
        var frame = new DummiesFrame("Hotel");
        frame.addRow("Room number");
        frame.setButtonText("Number of Guests in the Room");
        frame.go();
    }

    public static int calculate(int room) {
        int guests[] = {1, 4, 2, 0, 2, 1, 4, 3, 0, 2};

        return guests[room];
    }
}

Track Your Weight

public class TrackYourWeight {

    public static void main(String[] args) {
        double[] weight = {145.7, 148.3, 147.2, 146.2, 147.0, 148.5, 146.9};

        for (int i = 0; i < weight.length - 1; i++) {
            System.out.printf("%5.2f\n", weight[i + 1] - weight[i]);
        }
    }
}

Find the Biggest

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import static java.lang.System.out;

public class FindTheBiggest {

    public static void main(String[] args) throws IOException {
        int[] guests = new int[10];
        var diskScanner = new Scanner(new File("GuestList.txt"));

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            guests[roomNum] = diskScanner.nextInt();
        }

        out.println("Room\tGuests");

        int larestNumberOfGuests = 0;
        for (int roomNum = 0; roomNum < 10; roomNum++) {
            out.print(roomNum);
            out.print("\t");
            out.println(guests[roomNum]);
            if (guests[roomNum] > larestNumberOfGuests) {
                larestNumberOfGuests = guests[roomNum];
            }
        }
        out.println();
        for (int roomNum = 0; roomNum < 10; roomNum++) {
            if (guests[roomNum] == larestNumberOfGuests) {
                out.println(larestNumberOfGuests + " guests in Room " + roomNum);
            }
        }

        diskScanner.close();
    }
}

Find the Total

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import static java.lang.System.out;

public class FindTheTotal {

    public static void main(String[] args) throws IOException {
        int[] guests = new int[10];
        var diskScanner = new Scanner(new File("GuestList.txt"));

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            guests[roomNum] = diskScanner.nextInt();
        }

        out.println("Room\tGuests");

        int totalGuests = 0;
        for (int roomNum = 0; roomNum < 10; roomNum++) {
            out.print(roomNum);
            out.print("\t");
            out.println(guests[roomNum]);
            totalGuests += guests[roomNum];
        }
        out.println();
        out.println("Total number of guests is " + totalGuests);

        diskScanner.close();
    }
}

Tally Ho!

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import static java.lang.System.out;

public class TallyHo {

    public static void main(String[] args) throws IOException {
        int[] guests = new int[10];
        var diskScanner = new Scanner(new File("GuestList.txt"));

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            guests[roomNum] = diskScanner.nextInt();
        }

        int[] howManyRoomsWith = new int[10];

        out.println("Room\tGuests");

        for (int roomNum = 0; roomNum < 10; roomNum++) {
            out.print(roomNum);
            out.print("\t");
            out.println(guests[roomNum]);
            howManyRoomsWith[guests[roomNum]]++;
        }
        out.println();

        out.println("Guests\tHow many rooms");
        for (int numGuests = 0; numGuests < 10; numGuests++) {
            out.print(numGuests);
            out.print("\t\t");
            out.println(howManyRoomsWith[numGuests]);
        }

        diskScanner.close();
    }
}

A Classy Hotel

// Room.java

import java.text.NumberFormat;
import java.util.Scanner;

import static java.lang.System.out;

public class Room {
    private int guests;
    private double rate;
    private boolean smoking;
    private static NumberFormat currency = NumberFormat.getCurrencyInstance();

    public void readRoom(Scanner diskScanner) {
        guests = diskScanner.nextInt();
        rate = diskScanner.nextDouble();
        smoking = diskScanner.nextBoolean();
    }

    public void writeRoom() {
        out.print(guests);
        out.print("\t");
        out.print(currency.format(rate));
        out.print("\t");
        out.println(smoking ? "yes" : "no");
    }

    public int getGuests() {
        return guests;
    }

    public void setGuests(int guests) {
        this.guests = guests;
    }

    public double getRate() {
        return rate;
    }

    public boolean isSmoking() {
        return smoking;
    }
}

// RoomWithMax.java

import java.io.PrintStream;
import java.util.Scanner;

import static java.lang.System.out;

public class RoomWithMax extends Room {
    private int maxOccupancy;

    public int getMaxOccupancy() {
        return maxOccupancy;
    }

    @Override
    public void readRoom(Scanner diskScanner) {
        super.readRoom(diskScanner);
        maxOccupancy = diskScanner.nextInt();
    }

    public void writeRoom(PrintStream listOut) {
        listOut.println(getGuests());
        listOut.println(getRate());
        listOut.println(isSmoking());
        listOut.println(maxOccupancy);
    }
}

// RoomList.txt

1
60.00
true
4
4
60.00
true
4
2
60.00
false
2
0
60.00
false
2
2
80.00
true
2
1
80.00
false
2
4
80.00
false
4
3
80.00
false
4
0
100.00
true
4
2
100.00
false
4


// FindVacancy.java

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

import static java.lang.System.out;

public class FindVacancy {

    public static void main(String[] args) throws IOException {
        var room = new RoomWithMax[10];
        int roomNum;

        var diskScanner = new Scanner(new File("RoomList.txt"));
        for (roomNum = 0; roomNum < 10; roomNum++) {
            room[roomNum] = new RoomWithMax();
            room[roomNum].readRoom(diskScanner);
        }
        diskScanner.close();

        var keyboard = new Scanner(System.in);
        out.print("How many people? ");
        int howMany = keyboard.nextInt();
        out.print("Smoking? (y/n) ");
        boolean wantSmoking = keyboard.findWithinHorizon(".", 0).charAt(0) == 'y';
        keyboard.close();

        roomNum = 0;
        while (roomNum < 10 && (room[roomNum].getGuests() != 0 || room[roomNum].isSmoking() != wantSmoking || room[roomNum].getMaxOccupancy() < howMany)) {
            roomNum++;
        }

        if (roomNum == 10) {
            out.println("Sorry, no v cancy");
        } else {
            out.println("You're in room " + roomNum);
            room[roomNum].setGuests(howMany);

            var listOut = new PrintStream("RoomList.txt");
            for (roomNum = 0; roomNum < 10; roomNum++) {
                room[roomNum].writeRoom(listOut);
            }

            listOut.close();
        }
    }
}

Double Your Pressure

public enum Major {
  COMPUTER_SCIENCE, MATHEMATICS, LITERATURE, PHYSICS, HISTORY
}

public class Student {
  private String name;
  private int id;
  private Major major = null;
  private double[] grades;

  public Student(String name, int id) {
    this.name = name;
    this.id = id;
  }

  public Student(String name, int id, Major major) {
    this.name = name;
    this.id = id;
    this.major = major;
  }

  public Student(String name, int id, Major major, double[] grades) {
    this.name = name;
    this.id = id;
    this.major = major;
    this.grades = grades;
  }

  public String getInfo() {
    String info = "Name:" + name + "; ";
    info += "ID:" + id + "; ";
    info += "Major:" + (((major != null)?major:"None") + "; ");
    info += "GPA:" + calculateGpa();
    return info;
  }

  private double calculateGpa() {
    double total = 0.0;
    for (double grade : grades) {
      total += grade;
    }
    System.out.println(total);
    System.out.println(grades.length);
    return total / grades.length;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public Major getMajor() {
    return major;
  }

  public void setMajor(Major major) {
    this.major = major;
  }

  public double[] getGrades() {
    return grades;
  }

  public void setGrades(double[] grades) {
    this.grades = grades;
  }
}


public class Main {

  public static void main(String[] args) {
    double grades1[] = {4.0, 3.5, 2.5, 4.0, 3.0};
    Student student1 =
      new Student("Barry", 111111, Major.COMPUTER_SCIENCE, grades1);

    double grades2[] = {3.5, 3.3, 2.5, 4.0, 3.1};
    Student student2 =
      new Student("Harriet", 222222, Major.MATHEMATICS, grades2);

    double grades3[] = {3.0, 2.0, 2.5, 4.0, 4.0};
    Student student3 =
      new Student("Jennie", 333333, Major.LITERATURE, grades3);

    double grades4[] = {2.8, 3.8, 4.0, 3.5, 3.5};
    Student student4 = new Student("Sam", 444444, Major.PHYSICS, grades4);

    System.out.println(student1.getInfo());
    System.out.println(student2.getInfo());
    System.out.println(student3.getInfo());
    System.out.println(student4.getInfo());
  }

}

Mine is the Biggest

public class Main {

  public static void main(String[] args) {
    if (args.length < 3) {
      System.out.println("Usage: java Main intValue1 intValue2 intValue3");
    } else {
      int largest = Integer.parseInt(args[0]);
      int next = Integer.parseInt(args[1]);
      if (next > largest) {
        largest = next;
      }
      next = Integer.parseInt(args[2]);
      if (next > largest) {
        largest = next;
      }
      System.out.println(largest);
    }
  }

}