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

Chapter 9: Constructing New Objects

In this chapter:

School Days

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

public class Student {
  private String name;
  private int id;
  private double gpa = 0.0;
  private Major major = null;

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

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

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

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

  public void display() {
    String info = "Name:" + name + "; ";
    info += "ID:" + id + "; ";
    info += "GPA:" + ((gpa > 0.0)?gpa:"None") + "; ";
    info += "Major:" + ((major != null)?major:"None");
    System.out.println(info);
  }

  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 double getGpa() {
    return gpa;
  }

  public void setGpa(double gpa) {
    this.gpa = gpa;
  }

  public Major getMajor() {
    return major;
  }

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


public class Main {

  public static void main(String[] args) {
    var student1 = new Student("Barry", 111111, 4.0, Major.COMPUTER_SCIENCE);
    var student2 = new Student("Harriet", 222222, 3.5);
    var student3 = new Student("Jennie", 333333, Major.LITERATURE);
    var student4 = new Student("Sam", 444444, 3.7);

    student1.display();
    student2.display();
    student3.display();
    student4.display();
  }

}

Flight of Fancy

public enum Airport {
  LHR, LAX, EWR, JFK, SFO
}

import java.time.LocalTime;

public class AirplaneFlight {
  private int flightNumber;
  private Airport departureAirport;
  private LocalTime departureTime;
  private Airport arrivalAirport;
  private LocalTime arrivalTime;

  public AirplaneFlight(int flightNumber,
      Airport departureAirport, LocalTime departureTime,
      Airport arrivalAirport, LocalTime arrivalTime) {
    this.flightNumber = flightNumber;
    this.departureAirport = departureAirport;
    this.departureTime = departureTime;
    this.arrivalAirport = arrivalAirport;
    this.arrivalTime = arrivalTime;
  }

  public AirplaneFlight(int flightNumber,
      Airport departureAirport, Airport arrivalAirport) {
    this.flightNumber = flightNumber;
    this.departureAirport = departureAirport;
    this.arrivalAirport = arrivalAirport;
  }

  public void display() {
    String info = "Flight:" + flightNumber + "; ";
    info += "From:" + departureAirport;
    if (departureTime != null) {
      info += " at " + departureTime;
    }
    info += "; ";
    info += "To:" + arrivalAirport;
    if (arrivalTime != null) {
      info += " at " + arrivalTime;
    }
    System.out.println(info);
  }

  public int getFlightNumber() {
    return flightNumber;
  }

  public void setFlightNumber(int flightNumber) {
    this.flightNumber = flightNumber;
  }

  public Airport getDepartureAirport() {
    return departureAirport;
  }

  public void setDepartureAirport(Airport departureAirport) {
    this.departureAirport = departureAirport;
  }

  public LocalTime getDepartureTime() {
    return departureTime;
  }

  public void setDepartureTime(LocalTime departureTime) {
    this.departureTime = departureTime;
  }

  public Airport getArrivalAirport() {
    return arrivalAirport;
  }

  public void setArrivalAirport(Airport arrivalAirport) {
    this.arrivalAirport = arrivalAirport;
  }

  public LocalTime getArrivalTime() {
    return arrivalTime;
  }

  public void setArrivalTime(LocalTime arrivalTime) {
    this.arrivalTime = arrivalTime;
  }
}

import java.time.LocalTime;

public class Main {

  public static void main(String[] args) {
    var flight1 = new AirplaneFlight(111, Airport.EWR, Airport.SFO);
    var flight2 = new AirplaneFlight(222, Airport.JFK, LocalTime.of(2, 14),
            Airport.LAX, LocalTime.of(6, 19));

    flight1.display();
    flight2.display();
  }
}

Student Showcase

(Rmember to copy the Student and Major classes from this chapter's earlier exercise.)
public class StudentWithGetString extends Student {

    public StudentWithGetString(String name, int id) {
        super(name, id);
    }

    public StudentWithGetString(String name, int id, double gpa) {
        super(name, id, gpa);
    }

    public StudentWithGetString(String name, int id, Major major) {
        super(name, id, major);
    }

    public StudentWithGetString(String name, int id, double gpa, Major major) {
        super(name, id, gpa, major);
    }


    public String getString() {
        String info = "Name:" + getName() + "; ";

        info += "ID:" + getId() + "; ";

        double gpa = getGpa();
        info += "GPA:" + ((gpa > 0.0)?gpa:"None") + "; ";

        Major major = getMajor();
        info += "Major:" + ((major != null)?major:"None");

        return info;
    }
}

public class Main {

    public static void main(String[] args) {
        var student1 = new StudentWithGetString(
                "Barry", 111111, 4.0, Major.COMPUTER_SCIENCE);
        var student2 = new StudentWithGetString(
                "Harriet", 222222, 3.5);
        var student3 = new StudentWithGetString(
                "Jennie", 333333, Major.LITERATURE);
        var student4 = new StudentWithGetString(
                "Sam", 444444, 3.7);

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

The Waiting Game

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class AirplaneFlightWithDuration extends AirplaneFlight {

  public AirplaneFlightWithDuration(int flightNumber,
      Airport departureAirport, LocalTime departureTime,
      Airport arrivalAirport, LocalTime arrivalTime) {
    super(flightNumber, departureAirport, departureTime,
        arrivalAirport, arrivalTime);
  }

  public AirplaneFlightWithDuration(int flightNumber,
      Airport departureAirport, Airport arrivalAirport) {
    super(flightNumber, departureAirport, arrivalAirport);
  }

  public long duration() {
    if (getArrivalTime() != null && getDepartureTime() != null) {
      return ChronoUnit.MINUTES.between
                          (getDepartureTime(), getArrivalTime());
    } else {
      return 0L;
    }

  }
}

import java.time.LocalTime;

public class Main {

  public static void main(String[] args) {
    var flight1 =
     new AirplaneFlightWithDuration(111, Airport.EWR, Airport.SFO);
    var flight2 =
     new AirplaneFlightWithDuration(222, Airport.JFK, LocalTime.of(2, 14),
        Airport.LAX, LocalTime.of(6, 19));

    flight1.display();
    long duration = flight1.duration();
    if (duration != 0L) {
      System.out.println("  Duration: " + duration + " minutes.");
    }

    duration = flight2.duration();
    flight2.display();
    if (duration != 0L) {
      System.out.println("  Duration: " + duration + " minutes.");
    }
  }
}

A Conversion Diversion

public enum TempScale {
    CELSIUS, FAHRENHEIT, KELVIN, RANKINE,
    NEWTON, DELISLE, REAUMUR, ROMER, LEIDEN
};

public class Temperature {
    private double number;

    private TempScale scale;

    public Temperature() {
        number = 0.0;
        scale = TempScale.FAHRENHEIT;
    }

    public Temperature(double number) {
        this.number = number;
        scale = TempScale.FAHRENHEIT;
    }

    public Temperature(TempScale scale) {
        number = 0.0;
        this.scale = scale;
    }

    public Temperature(double number, TempScale scale) {
        this.number = number;
        this.scale = scale;
    }

    public void setNumber(double number) {
        this.number = number;
    }

    public double getNumber() {
        return number;
    }

    public void setScale(TempScale scale) {
        this.scale = scale;
    }

    public TempScale getScale() {
        return scale;
    }
}

import static java.lang.System.out;

public class TemperatureNice extends Temperature {

    public TemperatureNice() {
        super();
    }

    public TemperatureNice(double number) {
        super(number);
    }

    public TemperatureNice(TempScale scale) {
        super(scale);
    }

    public TemperatureNice(double number, TempScale scale)
    {
        super(number, scale);
    }

    public void display() {
        out.printf("%5.2f degrees %s\n",
                              getNumber(), getScale());
    }
}


public class TemperatureEvenNicer extends TemperatureNice {

    public TemperatureEvenNicer() {
        super();
    }

    public TemperatureEvenNicer(double number) {
        super(number);
    }

    public TemperatureEvenNicer(TempScale scale) {
        super(scale);
    }

    public TemperatureEvenNicer(double number, TempScale scale) {
        super(number, scale);
    }


    public void convertTo(TempScale newScale) {
        double number = getNumber();
        TempScale scale = getScale();

        if (scale == TempScale.FAHRENHEIT &&
                               newScale == TempScale.CELSIUS) {
            setNumber((number - 32) * 5.0 / 9.0);
        } else if (scale == TempScale.CELSIUS &&
                               newScale == TempScale.FAHRENHEIT) {
            setNumber(number * 9.0 / 5.0 + 32);
        }
        setScale(newScale);
    }
}



public class UseTemperatureEvenNicer {

    public static void main(String[] args) {

        var temp = new TemperatureEvenNicer();

        temp.setNumber(70.0);
        temp.setScale(TempScale.FAHRENHEIT);
        temp.convertTo(TempScale.CELSIUS);
        temp.display();

        temp = new TemperatureEvenNicer(32.0);
        temp.convertTo(TempScale.CELSIUS);
        temp.display();

        temp = new TemperatureEvenNicer(TempScale.CELSIUS);
        temp.convertTo(TempScale.FAHRENHEIT);
        temp.display();

        temp = new TemperatureEvenNicer(2.73, TempScale.KELVIN);
        temp.display();
    }
}

The Null Hypothesis

jshell> import javax.swing.JFrame

jshell> JFrame frame
frame ==> null

jshell> frame.setSize(100, 100)
|  java.lang.NullPointerException thrown:
|        at (#5:1)

jshell> frame = new JFrame()
frame ==> javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden, ... tPaneCheckingEnabled=true]

jshell> frame.setSize(100, 100)

jshell> frame.setVisible(true)
At this point, the frame appears on your computer screen.

In the middle of the session, you get a NullPointerException because you're trying to call the frame.setSize method, but you haven't yet set the frame variable to anything. So the frame variable still has the value null, and you can't call a method on a null value.

Widespread Panic

The panic button expands to fill the entire frame. Java has several different layout managers and they each behave differently. In a FlowLayout, components shrink to the size of whatever content is inside them. In a BorderLayout, components expand to fill the containing component. There are other differences among Java's layout managers. For example, in a FlowLayout, objects line up one after another horizontally. In a BorderLayout, objects appear either in the center or on one of the four edges of the layout.