Assignment 1: The Solution (Last updated Jan 20)

A model solution for homework 1 is presented below. Please make sure you check out either

Sean's Guide to Programming Style
or Cay Horstmann's more general but equally good Style Guide and think about them and follow them. The (minor) difference between the two, I think, is that Cay will not be grading for A202 this semester.

Here's the solution to assignment 1:

-------( HomeoworkOne.java )-------
import java.awt.*;
import BreezyGUI.*;
 
/* Class HomeworkOne creates a window which converts kilometers to
 * nautical miles.
 */ 
 
public class HomeworkOne extends GBFrame { // or other class name
  Label        kmLabel       = addLabel        ("Kilometers"    , 1, 1, 1, 1); 
  IntegerField kmIntField    = addIntegerField (0               , 1, 2, 1, 1); 
  Label        nmLabel       = addLabel        ("Nautical Miles", 2, 1, 1, 1); 
  DoubleField  nmDblField    = addDoubleField  (0               , 2, 2, 1, 1); 
  Button       computeButton = addButton       ("Compute"       , 3, 1, 2, 1); 
  int kilometers; double nauticalMiles; 
 
/* When the computeButton is pressed, update the nauticalMiles field. */
 
  public void buttonClicked(Button buttonObj) {
    kilometers = kmIntField.getNumber(); 
    nauticalMiles = 0.54 * km; /* 90 * 60 minutes = 90 * 60 nautical miles
      between the ecuator and north pole (10,000 kilometers) so 
      1km is about 90 * 60 / 10000 = 0.54, correct? */ 
    nmDblField.setNumber(nauticalMiles);     
  } 
 
/* The main() method creates a new HomeworkOne object when this program
 * is run.
 */
 
  public static void main(String[] args) {
    Frame converter = new HomeworkOne();   // this creates the object 
    converter.setSize(200, 150);          
    converter.setVisible(true); 
  }   
}
 
----------------------------
Problem 14/58: in file HomeworkTwo.java:
 
-------( HomeworkTwo.java )-------
import java.awt.*;
import BreezyGUI.*;
 
/* HomeworkTwo creates a frame which asks for data about a student, and
 * echos this data to a messageBox.
 */
 
public class HomeworkTwo extends GBFrame {
  Label        lastNameLabel  = addLabel     ("Last Name:" , 1, 1, 1, 1); 
  TextField    lastNameField  = addTextField (""           , 1, 2, 1, 1); 
  Label        firstNameLabel = addLabel     ("First Name:", 2, 1, 1, 1); 
  TextField    firstNameField = addTextField (""           , 2, 2, 1, 1); 
  Label        initialLabel   = addLabel     ("Middle In.:", 3, 1, 1, 1); 
  TextField    initialField   = addTextField (""           , 3, 2, 1, 1); 
  Label        yearLabel      = addLabel     ("Class Year" , 4, 1, 1, 1); 
  IntegerField yearField      = addIntegerField (         0, 4, 2, 1, 1); 
  Label        phoneLabel     = addLabel   ("Campus Phone:", 5, 1, 1, 1); 
  TextField    phoneField     = addTextField (""           , 5, 2, 1, 1);
  Button       displayButton  = addButton  ("Display"      , 6, 1, 2, 1); 
  String output; 
 
/* When the displayButton is clicked, pop up a messageBox echoing the 
 * personal data that's been entered. 
 */
  public void buttonClicked (Button buttonObj) {
    output =   "Last Name: "      + lastNameField.getText() + 
             "\nFirstName: "      + firstNameField.getText() + 
             "\nMiddle Initial: " + initialField.getText() + 
             "\nClass year: "     + yearField.getText() + 
             "\nCampus Phone: "   + phoneField.getText() + "\n"; 
    messageBox(output); 
  } 
 
/* The main() method creates a new HomeworkTwo object when this program
 * is run.
 */
 
  public static void main(String[] args) {
    Frame personData = new HomeworkTwo(); 
    personData.setSize(200, 150); 
    personData.setVisible(true); 
  } 
}
----------------------------