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

Occasional Lecture Notes

last change: Friday, November 8, 2002

Normally, lecture notes are not formally distributed in this course. On the other hand, viewgraphs that present material not covered in the course textbook will appear, from time to time, in this file. New notes will simply be added to the top of the file. Watch the date at the top to see if anything has been changed.


Thursday, Nov. 7, 2002

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


Thursday, Oct. 24, 2002

Objectives: robust input; writing your own methods

Robust Input

If a user enters an invalid input, the program does not quit, but continues to let the user try again.
Use loops and boolean flag variables to implement this.

See programs53/ShowInputLoop.java.


Writing Your Own Classes and Methods


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


Tuesday, Oct. 15, 2002

Objectives

Continue general discussion of algorithms

Let's revisit the pizza problem


Find the best value among an arbitrary number of pizzas (not just 3 anymore).

Initial Algorithm for Improved Solution to Pizza Problem
 

  1. For each size of pizza, read in the pizza size and price and compute unit cost. Compare the unit cost just computed with the previous unit costs and save the size and price of the pizza whose unit cost is the smallest so far.
  2. Display the size and price of the pizza with the smallest unit cost.
The purpose of step 1 of the algorithm is to perform the cost computation for each individual pizza and somehow save the size and price of the pizza whose unit cost was the smallest. After all costs are computed, step 2 displays the size and price of the pizza that is the best buy.

Step 1 Refinement

1 Repeat the following steps for each size of pizza:
 1.1. Read in the next pizza size and price.
 1.2. Compute the unit price.
 1.3. If the new unit price is the smallest one so far, then save this pizza’s size, price, and unit price.

Step 1 specifies the repetition of a group of steps: step 1.1 (read), step 1.2 (compute), and step 1.3 (compare). We will repeat these steps as many times as necessary until all unit prices are computed.

Each time we compute a new unit price, step 1.3 compares it to the others, and the current pizza’s size and price are saved if its unit price is smaller than any others computed so far. If the unit price is not the smallest so far, the current pizza’s size and price are not saved.

Step 1.3 is a selection step because it selects between the two possible outcomes: (a) save the pizza’s data and (b) do not save the pizza’s data.


Algorithms: Repetitive Control Structures (loops)

There are 3 main kinds of loops:

How could we use each of these in solving the pizza problem?

How can we initialize the minimum value? Let's generalize the pizza problem so the range of values can be negative (say, minimum and maximum temperatures)

How do we initialize the min and max?
priming read?
set the min to the maximum possible value; set the max to the minimum possible value.
Java provides Integer.MIN_VALUE, Integer.MAX_VALUE, Float.MIN_VALUE,Float.MAX_VALUE, etc.



Thursday, Oct. 10, 2002

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 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 ("Office hours, 2-5:30 PM");
  System.out.println ("CSci 53, 7:30-8:45 PM");
}

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

if (day == 4)          // Thursday
{
  System.out.println ("Office hours, 2-5:30 PM");
  System.out.println ("CSci 53, 7:30-8:45 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");
}



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

if ((day == 2) || (day == 4))       // Tuesday, Thursday
{
  System.out.println ("Office hours, 2-5:30 PM");
  System.out.println ("CSci 53, 7:30-8:45 PM");
}

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

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


Nested if Statement

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("Office hours, 2-5:30 PM");
    System.out.println("CSci 53, 7:30-8:45 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                            // out of range
      {
        System.out.println("Bad input");
      }
    }
  }
}

Simplifying 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("Office hours, 2-5:30 PM");
  System.out.println("CSci 53, 7:30-8:45 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                            // out of range
{
  System.out.println("Bad input");
}

Switch Statements

switch(day)
{
  case 1:
  case 5:         // Monday, Friday
    System.out.println ("Work at home today");
    break;
  case 2:
  case 4:         // Tuesday, Thursday
    System.out.println("Office hours, 2-5:30 PM");
    System.out.println("CSci 53, 7:30-8:45 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:
    System.out.println("Bad input");
}



Thursday, Sept. 26, 2002

Algorithms: the Foundation of Systematic Problem-Solving

"algorithm, a finite collection of simple instructions that can be performed by a computer and is guaranteed to halt in a finite amount of time."
source: the Analytical Engine, p. 26

QUESTION: Suppose a program is designed not to halt in a finite amount of time? Is it implementing an algorithm?


Algorithms:the Foundation of Systematic Problem-Solving

"algorithm A rigid mathematical (or logical) relationship which has only one starting point and one finishing point. ... Each algorithm must have a finite list of executable instructions which must eventually come to an end.

"The British mathematician Alan Turing proved [1936] that an algorithmic approach can be used to solve any mathematical or logical problem for which a solution is known to exist. This means that one can deduce that any solvable problem can be handled by a computer, provided the correct algorithm is used.

"In computing, an algorithm is the plan, routine, or process of defining a set of instructions so that a computer program can be written to find the answer / solution to a given problem or task."

source: Prentice-Hall's Illustrated Dictionary of Computing (3rd ed.) (Prentice-Hall, 1998), p. 19


The Power of Algorithms

Algorithms can be written to solve a multitude of (sometimes very complex) problems

Yet they are made up of careful combinations of very simple operations

Simple operations are combined in the following ways

Believe it or not, that's all! All algorithms can be constructed this way! That this is so was proved in 1966 (Böhm-Jacopini).


Thursday, Sept. 19, 2002

How are programming languages different from natural (human) languages?


Syntactic and semantic analysis

(end of document)