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?
-
An object has state (a set of data values, sometimes called fields
or attributes) and behavior (a set of methods). The methods
act on the data values, which changes the overall state.
-
The data values are usually declared to be private, so that a calling
program must call methods to modify them and can't modify them directly.
-
The methods are usually declared to be public, so a calling program
can use them. Sometimes there are also private methods; why would we want
these?
Common kinds of methods
-
Constructor: creates and returns object from a class. Usually has
a list of parameters, and uses these to set the object's data values. The
constructor's name is always that of the class; it has no return statement.
-
"Setter": usually has parameters, which it uses to modify one or
more of an existing object's data values.
-
"Getter": usually has no parameters; it just returns one of the
object's data values.
-
input: reads an objects data values from the keyboard or from a
file.
-
toString: collects (some of) an object's data values together into a string,
suitable for displaying or writing to a file.
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
-
Sets of reusable classes which provide generally usable methods are the
basis for all modern software development, regardless of language or operating
system.
-
There are a number of kinds of methods:
-
A public method is available
to any program that imports the class; a private
method is available only to other methods within the class. At this time
we'll consider only public ones.
-
A static method is associated
with the class; a non-static
method is associated with each object. At this time we'll consider only
static ones.
-
A value-returning method
is one that returns a value to the calling program; a void
method is one that does not return a value. At this time we'll consider
both value-returning and void methods.
See these files in programs53:Screen.java, Smiley.java, MinMax.java, MinMaxThree.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
-
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.
-
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:
-
count-controlled
-
sentinel-controlled
-
flag-controlled
-
count-controlled: before starting the repetition, calculate (or
query the user about) how many iterations there will be
-
sentintel-controlled: start, and continue, the repetition until
a problem-specific value appears in the input data
-
flag-controlled: after each iteration, ask the user whether to quit
or continue
How could we use each of these in solving the pizza problem?
How can we initialize the minimum value?
-
set it to 0 before beginning the repetition
-
make the first pizza a special case: read its data before beginning the
repetition; this is called a priming read
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)