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

Occasional Lecture Notes

last change: Thursday, April 24, 2003

Thursday, April 24, 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


Thursday, March 6, 2003

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, March 4, 2003

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.


(end of document)