



|
|
CS1 Course Site
|
Wait , WAIT !!What's that word ? ( Class Notes ( without definitions ) )
Monday October 16 , 2000
|
|
O-O Architecture
LineOfSquares
{
/ /instance variable
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 = areaOfFigure /nrSquares;
edgeLength = Math.sqrt (areaOfSquare);
}
public double perimeter ()
{
return edgeLength * eeCount ();
}
private int eeCount ()
{
return (2 * nrSquares) + 2;
}
}
Remark
Characteristics of the imperative style are...
- Binding Commands (Assignment statements)
- conditional commands (selectors)
- repetition commands
- IO commands
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.print (b);
}
Program to read two inegers and display the word ``ok'' if they are not equal.
int a = IO.read_int ();
int b = IO.read_int ();
if (a! = b)
{
IO.println ( ``Ok'');
}
multiway if
read one integer and display
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'');
}
Read 3 integers and display them in order, low to high. Way 1: nested if.
/ /Read the integers
int a = IO.read_int ();
int b = IO.read_int ();
int c = IO.read_int ();
if (a < b)
{
if (a < c)
{
IO.println (a);
if (b < c)
{
IO.println (b);
IO.println (c);
}
else
{
IO.println (c);
IO.println (b);
}
}
}
Way 2: Abstarction
if (less (a, b, c))
{
IO.println (a + ````+ b + ````+ c);
}
else if (less (a, c, b))
{
IO.println (a + ````+ c + ````+ b);
}
else if (less (b, a, c))
{
IO.println (b + ````+ a + ````+ c);
}
... 3 more cases
static private boolean less (int, int y, int z)
{
if (x < y)
{
if (y < z)
{
return true
}
}
return false;
}
The boolean data type
name: boolean
values: true false
Operators
& & (AND)
| | (OR)
! (NOT)
|
|