



|
|
CS1 Course Site
|
Class Notes
Monday October 23 , 2000
|
|
Incremental Programming
PROBLEM:
Read three integers and display the difference between the largest and the smallest values.
The sequence of programs will be:
- P0: Read and echo three integers.
- P1: Same as P0, plus determine and display the maximum value.
- P2: Same as P1, plus determine and display the minimum value.
- P3: Same as P2, plus compute and display the difference between the max and min values.
- P4: Same as P3, but delete all ``extra'' output.
P0:
static public void main (String args [])
{
/ /Read and echo two integers.
int a = IO.read_int ();
int b = IO.read_int ();
IO.println (``a = ``+ a);
IO.println (``b = ``+ b);
|
P1:
static public void main (String args [])
{
/ /Read and echo two integers.
int a = IO.read_int ();
int b = IO.read_int ();
IO.println (``a = ``+ a);
IO.println (``b = ``+ b);
/ /Compute and print the max value.
int max = max (a, b);
IO.println (max);
}
static private int max (int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
|
P2
static public void main (String args [])
{
/ /Read and echo two integers.
int a = IO.read_int ();
int b = IO.read_int ();
IO.println (``a = ``+ a);
IO.println (``b = ``+ b);
/ /Compute and print the max value.
int max = max (a, b);
IO.println (max);
}
static private int max (int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
/ /Compute and print the min value.
int min = min (a, b);
IO.println (``min = ``+ min);
}
static private int min (int a, int b)
{
if (a > b)
{
return b;
}
\ttline
{
return a;
}
}
|
P3
static public void main (String args [])
{
/ /Read and echo two integers.
int a = IO.read_int ();
int b = IO.read_int ();
IO.println (``a = ``+ a);
IO.println (``b = ``+ b);
/ /Compute and print the max value.
int max = max (a, b);
IO.println (max);
}
static private int max (int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
/ /Compute and print the min value.
int min = min (a, b);
IO.println (``min = ``+ min);
}
static private int min (int a, int b)
{
if (a > b)
{
return b;
}
else
{
return a;
}
}
/ /Compute and display difference.
int diff = max - min;
IO.println (``diff = ``+ diff);
}
|
P4
static public void main (String args [])
{
/ /Read and echo two integers.
int a = IO.read_int ();
int b = IO.read_int ();
/ /Compute and print the max value.
int max = max (a, b);
}
static private int max (int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
/ /Compute and print the min value.
int min = min (a, b);
}
static private int min (int a, int b)
{
if (a > b)
{
return b;
}
else
{
return a;
}
}
/ /Compute and display difference.
int diff = max - min;
IO.println (``diff = ``+ diff);
}
|
PROBLEM
Read several (at least one) non negative integers and compute their real average.
? Program
{
/ /Establish an ``accumulator.''
int sum = 0;
/ /Establish a counter to count data.
int count = 0;
/ /Sum the integers.
int num = IO.read_int ();
while (num > = 0)
{
sum = sum + num;
count = count + 1;
num = IO.read_int ();
}
/ /Compute and display the average.
double ave = (double) sum / (double) count;
IO.println (``ave = ``+ ave);
}
|
|
|