School of Engineering and Applied Science
Department of Computer Science
CSci 53 -- Introduction to Software Development 
http://www.seas.gwu.edu/~csci53/fall03
Prof. Michael B. Feldman
mfeldman@gwu.edu

Occasional Lecture Notes

last change: Saturday, Nov. 15, 2003

Watch this space for occasional postings of lecture notes; the most recent notes will appear at the top of the page.


Thursday, Nov. 13, 2003

More on Classes and Methods

Let's look at classes which are instantiated to produce objects. What are some of the common characteristics of such classes and objects?

Common kinds of methods


Aggregation

An aggregate object is an object that contains references to other objects
For example, an Account object contains a reference to a String object (the owner's name)
An aggregate object represents a has-a relationship
A bank account has a name
Likewise, a student may have one or more addresses


Tuesday, Nov. 11, 2003

Writing Your Own Classes and Methods


See these files in programs53:Screen.javaSmiley.javaMinMax.javaMinMaxThree.Java.

Decision (IF) Constructs

Problem: Prof. Feldman is sometimes a bit absentminded and could use a bit of help in remembering his schedule on the days he's at GW this semester. This schedule is:
Develop a program to provide this help


Basic Algorithm

  1. Prompt user to enter a day of the week.
  2. Display the schedule items for that day
Step 2 refinement

If day is Monday, display "work at home"
If day is Tuesday, display Tuesday's items
If day is Wednesday, display Wednesday's items
If day is Thursday, display Thursday's items
If day is Friday, display "work at home"
If day is Saturday, display "have a nice weekend"
If day is Sunday, display "have a nice weekend"


if (day == 1) // Monday
{
  System.out.println ("Work at home today");
}

if (day == 2) // Tuesday
{
  System.out.println ("CSci 53, 11 AM - 12:15 PM");
  System.out.println ("CSci 133, 2 - 3:15 PM");
  System.out.println ("Office hours, 4 - 5:30 PM");
}

if (day == 3) // Wednesday
{
  System.out.println ("Office hours, 2 - 5:30 PM");
}

if (day == 4) // Thursday
{
  System.out.println ("CSci 53, 11 AM - 12:15 PM");
  System.out.println ("CSci 133, 2 - 3:15 PM");
  System.out.println ("Office hours, 4 - 5:30 PM");
}

if (day == 5) // Friday
{
  System.out.println ("Work at home today");
}

if (day == 6) // Saturday
{
  System.out.println ("Have a nice weekend");
}

if (day == 7) // Sunday
{
  System.out.println("Have a nice weekend");
}


Combining conditions

if ((day == 1) || (day == 5)) // Monday, Friday
{
  System.out.println ("Work at home today");
}

if ((day == 2) || (day == 4)) // Tuesday, Thursday
{
  System.out.println ("CSci 53, 11 AM - 12:15 PM");
  System.out.println ("CSci 133, 2 - 3:15 PM");
  System.out.println ("Office hours, 4 - 5:30 PM");
}

if (day == 3) // Wednesday
{
  System.out.println ("Office hours, 2 - 5:30 PM");
}

if ((day == 6) || (day == 7)) // Saturday, Sunday
{
  System.out.println ("Have a nice weekend");
}


Nested if Statements

if ((day == 1) || (day = 5)) // Monday, Friday
{
  System.out.println ("Work at home today");
}
else
{
  if ((day == 2) || (day == 4)) // Tuesday, Thursday
  {
    System.out.println ("CSci 53, 11 AM - 12:15 PM");
    System.out.println ("CSci 133, 2 - 3:15 PM");
    System.out.println ("Office hours, 4 - 5:30 PM");
  }
  else
  {
    if (day == 3) // Wednesday
    {
      System.out.println ("Office hours, 2 - 5:30 PM");
    }
    else
    {
      if ((day == 6) || (day == 7)) // Saturday, Sunday
      {
        System.out.println ("Have a nice weekend");
      }
      else // none of the above
      {
        System.out.println ("Out of range input; try again")
      }
    }
  }
}


Simplifying Nested if Statements
a Different Formatting Style


if ((day == 1) || (day == 5)) // Monday, Friday
{
  System.out.println ("Work at home today");
}
else if ((day == 2) || (day == 4)) // Tuesday, Thursday
{
  System.out.println ("CSci 53, 11 AM - 12:15 PM");
  System.out.println ("CSci 133, 2 - 3:15 PM");
  System.out.println ("Office hours, 4 - 5:30 PM");
}
else if (day == 3) // Wednesday
{
  System.out.println ("Office hours, 2 - 5:30 PM");
}
else if ((day == 6) || (day == 7)) // Saturday, Sunday
{
  System.out.println ("Have a nice weekend");
}
else // none of the above
{
  System.out.println ("Out of range input; try again")
}


Switch Statements

switch(day)
{
  case 1:
  case 5: // Monday, Friday
    System.out.println ("Work at home today");
    break; // jump to bottom of switch statement

  case 2:
  case 4: // Tuesday, Thursday
    System.out.println ("CSci 53, 11 AM - 12:15 PM");
    System.out.println ("CSci 133, 2 - 3:15 PM");
    System.out.println ("Office hours, 4 - 5:30 PM");
    break;

  case 3: // Wednesday
    System.out.println ("Office hours, 2 - 5:30 PM");
    break;

  case 6:
  case 7: // Saturday, Sunday
    System.out.println ("Have a nice weekend");
    break;

  default: // anything else
    System.out.println("Bad input");
}


Use Constants, Not "Magic Numbers"

final int MON = 1, TUE = 2, WED = 3, THU = 4;
final int FRI = 5, SAT = 6, SUN = 7;
...
switch(day)
{
case MON:
case FRI: // Monday, Friday
System.out.println ("Work at home today");
break; // jump to bottom of switch statement

case TUE:
case THU: // Tuesday, Thursday
System.out.println ("CSci 53, 11 AM - 12:15 PM");
System.out.println ("CSci 133, 2 - 3:15 PM");
System.out.println ("Office hours, 4 - 5:30 PM");
break;

case WED: // Wednesday
System.out.println ("Office hours, 2 - 5:30 PM");
break;

case SAT:
case SUN: // Saturday, Sunday
System.out.println ("Have a nice weekend");
break;

default: // anything else
System.out.println("Bad input");
}


Sept. 9-11, 2003

Example: Average Age of Professors

Problem Specification
You're a very curious person. One thing you're curious about is the ages of three of your professors. But how can you find out how old they are? Some people are offended if you ask their age.
Luckily, the GW Undergraduate Bulletin has a list of (most of) the professors in the back of the book. Each professor's listing gives his/her degrees, showing the college or university and year of each degree.
If we assume the typical student is 21 when (s)he earns a bachelors degree, we can estimate a professor's age just by knowing the current calendar year and that professor's graduation year.
You're also curious about the mean (average) age of the profs.


Analysis
To solve this problem, we must be given the current year, and the graduation years of the three profs. The estimated age (to the nearest year) of a prof can be computed from these values. The mean age will be the sum of the ages divided by the number of profs (3).
Data Requirements and Formulas
Problem Inputs:
thisYear - the current calendar year, an integer
year - the graduation year of each prof in turn, an integer
Problem Outputs:
age1, age2, age3 - the ages of the three professors (integers)
meanAge - the mean age of the professors
Relevant Formulas
Estimated age = current year - graduation year + 21
Initial Algorithm
  1. Prompt the user and read in each professor's graduation year, then calculate and display that professor's estimated age.
  2. Prompt the user and read in the current year. 
  3. Compute the mean age.
  4. Display the mean age.


Algorithm Refinements
Step 2 can be refined:

2.1 Prompt the user and read in 1st professor's graduation year, then calculate and display that professor's estimated age.
2.2 Prompt the user and read in 2nd professor's graduation year, then calculate and display that professor's estimated age.
2.3 Prompt the user and read in 3rd professor's graduation year, then calculate and display that professor's estimated age.


Test Plan
Let's develop one right here!
Implementation
Start with the template - it's unnecessary to start with a blank page

mfeldman@hobbes.seas.gwu.edu:15:
cp programs53/ProjectTemplate.java .
mfeldman@hobbes.seas.gwu.edu:16: cat ProjectTemplate.java

//--------------------------------------------------------------
//| ProjectTemplate.java <-- change to your project filename
//| Use this as a basis for your own programs
//| Author: <your name>, The George Washington University
//| Last Modified: <date>
//--------------------------------------------------------------
import cs1.Keyboard;
public class ProjectTemplate <-- change to your project name
{
  public static void main (String[] args)
  {
  // put your code here
  }
}

Declare the variables and copy the main algorithm steps as comments: framework file.

//--------------------------------------------------------------
//| MeanAge.java
//| Finds mean estimated age of three professors
//| Author: M.B. Feldman, The George Washington University
//| Last Modified: September 9, 2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class MeanAge
{
  public static void main (String[] args)
  {

    int thisYear; // input - the current calendar year
    int year; // input - year of a prof's bachelors deg.
    int age1; // output - 1st prof's estimated age
    int age2; // output - 1st prof's estimated age
    int age3; // output - 1st prof's estimated age
    int meanAge; // output - mean of 3 ages

    // Prompt the user and read in the current calendar year

    // Prompt the user and read in 1st prof's degree year
    // and compute and display that prof's estimated age

    // Prompt the user and read in 2nd prof's degree year
    // and compute and display that prof's estimated age

    // Prompt the user and read in 3rd prof's degree year
    // and compute and display that prof's estimated age

    // Compute the mean age

    // Display the mean age

  }
}

Compile the framework; fix any errors. Then fill in the rest of the program statements.

See MeanAge.java in the program distribution.

Improved Algorithm

We can improve the algorithm a bit by realizing that we don't need 3 separate age variables. We can use one variable over and over, as long as we accumulate the sum of the ages as we go along. We need a sum variable; let's call it ageSum.
This gives a new refinement of Step 2:

2.1 Prompt the user and read in 1st professor's graduation year, then calculate and display that professor's estimated age, and add it into the running age sum.
2.2 Prompt the user and read in 2nd professor's graduation year, then calculate and display that professor's estimated age, and add it into the running age sum.
2.3 Prompt the user and read in 3rd professor's graduation year, then calculate and display that professor's estimated age, and add it into the running age sum.

See MeanAge2.java in the program distribution.


Sept. 4, 2003

Example: Average Speed on Trip

Problem Specification

Next week, you're driving to Long Island to visit your family. When you drive, you'd like to know what your average end-to-end speed, in miles per hour, was for the trip.

You're not very good at arithmetic. Luckily, you'll have your trusty laptop with you, so you can develop a little program that will compute your average speed from the distance you've driven and the number of hours the trip took.
Unfortunately, your laptop is a bit lame-brained, so it's able to caculate only with "whole numbers" (integers).

Analysis

To solve this problem, we must be given the number of miles (to the nearest mile) and the number of hours (to the nearest hour). The average speed (to the nearest MPH) will be the quotient of the number of miles divided by the number of hours.

Data Requirements and Formulas
Problem Inputs:

miles  - the number of miles, an integer
hours - the number of hours, an integer

Problem Outputs:

mph - the average speed, to the nearest integer value
Relevant Formulas
mph = miles ÷ hours

Initial Algorithm

  1. Prompt the user and read in the number of miles.
  2. Prompt the user and read in the number of hours.
  3. Compute the average speed.
  4. Display the average speed.

Algorithm Refinements

This is a very simple algorithm and needs no refinements.

Test Plan

Let's develop one right here! (Here is what we developed in class.)

Test # Miles Hours Reason Expected Result Actual Result
1 15 3 normal 5
2 10 0 bad hours error
3 10 15 hours > miles 0
4 A 5 nonnumeric value error
5 0 5 miles = 0 0
6 -10 5 miles < 0 -2

Implementation

Start with the template - it's unnecessary to start with a blank page

mfeldman@hobbes.seas.gwu.edu:15:
mfeldman@hobbes.seas.gwu.edu:16: cat ProjectTemplate.java

//--------------------------------------------------------------
//| ProjectTemplate.java <-- change to your project filename
//| Use this as a basis for your own programs
//| Author: <your name> The George Washington University
//| Last Modified: <date>
//--------------------------------------------------------------
import cs1.Keyboard;
public class ProjectTemplate <-- change to your project name
{
  public static void main (String[] args)
  {
    // put your code here
  }
}

Declare the variables and copy the main algorithm steps as comments

//--------------------------------------------------------------
//| AverageSpeed.java
//| Finds average speed on a trip.java
//| Author: M.B. Feldman The George Washington University
//| Last Modified: September 5 2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class AverageSpeed
{
  public static void main (String[] args)
  {

    int miles;           // input  - number of miles driven
    int hours;           // input  - number of hours driven
    int mph;             // output - miles per hour

    // Prompt the user and read in the number of miles
    // Prompt the user and read in the number of hours
    // Compute the average speed
    // Display the average speed
  }
}

Compile the framework; fix any errors. Then fill in the rest of the program statements.

//--------------------------------------------------------------
//| AverageSpeed.java
//| Finds average speed on a trip.java
//| Author: M.B. Feldman The George Washington University
//| Last Modified: September 5 2002
//--------------------------------------------------------------
import cs1.Keyboard;
public class AverageSpeed
{
  public static void main (String[] args)
  {

    int miles;           // input  - number of miles driven
    int hours;           // input  - number of hours driven
    int mph;             // output - miles per hour


    // Prompt the user and read in the number of miles
    System.out.print
      ("How many miles did you drive (to the nearest mile)? ");
    miles = Keyboard.readInt();

    // Prompt the user and read in the number of hours
    System.out.print
      ("How many hours did you drive (to the nearest hour)? ");
    hours = Keyboard.readInt();

    // Compute the average speed
    mph = miles / hours;

    // Display the average speed
    System.out.print   ("Your average speed was ");
    System.out.println (mph + " miles per hour.");

  }
}

(end of document)