Tonight's lecture began with a discussion of the ' Objective Application Architecture'. The
objective application architecture
is the application architecture in which the ``perfect'' object for solving the problem at hand is created from a specially tailored class.
Here is the ' objective' solution to the line of squares problem:
A line of n-congruent squares collectively has area a. What is the perimeter of the line of squares?
class LineOfSquaresOAA
static public void main (~ ~ ~ ~ ~ ~)
{
LineOfSquares los = new LineOfSquares ();
IO.println (``Perimeter = '' + los.perimeter ());
}
class LineOfSquares
{
/ /Instance variables
private int nrSquares;
private double edgeLength;
/ /Constructor
public LineOfSquares ()
{
IO.print (``How many squares? ``);
nrSquares = IO.read_int ();
IO.print (``Area of figure? '');
double areaOfFigure = IO.read_double ();
double areaOfSquare = areaOfSquare /nrSquares;
edgeLength = Math.sqrt (areaOfSquare);
}
public double perimeter ()
{
return edgeLength * eeCount ();
}
private int eeCount ()
{
return (nrSquares * 2) + 2;
}
}
|
Following the example of the ojective application architecture, CG introduced us to ' imperative programming'. After defining ' imperative programming', CG provided some examples of various if-statements. Here are some examples:
Here is an example of an ``if'' statement:
Program to read two integers and display the word ``OK'' if they are not equal:
int a = IO.read_int ();
int b = IO.read int ();
if (a! = b)
{
IO.print (``OK'');
IO.println ();
}
|
Here is an example of an ``if-else'' statement:
Program to read two integers and display the larger of the two:
int a = IO.read_int ();
int b = IO.read int ();
if (a > b)
{
IO.println (a);
}
else
{
IO.println (b);
}
|
Here is an example of a ``multiway-if'' statement:
Read an integer and display ``zero'', ``negative'', or ``positive'' depending on the integer:
int a = IO.read_int ();
if (a > 0)
{
IO.println (``Positive'');
}
else if (a < 0)
{
IO.println (``Negative'');
}
else
{
IO.println (``Zero'');
}
|