P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
 
Class Notes

Monday October 23 , 2000
 
Synopsis
This lectured covers the powerful idea of Incremental Programming and how to use if effectively.   We also were introduced to the card class.
  We were notified that the answers to the first CS1 exam will be on the CS1 site.  

From the October 18 problem of the Incremental Programming section we are going to solve it using a functional app architecture.  

P0
  static etc
  / /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 etc
  / /same as P0
  P0 code
  / /compute & 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 etc
  / /P0 stuff
  / /P1 stuff
 
  / /P2 stuff compute min
  int min = min (a,  b);
  IO.println (min);
 
}  
  private int min (int a,  int b)
  {
  --- code
 
}
 

P3
  static etc
  / /P0 stuff
  / /P1 stuff
  / /P2 stuff
  / /P3 stuff compute & display diff
  int diff = max - min;
  IO.println ( ``diff =  ``+ diff);
 
}  
  private int min (int a,  int b)
  {
  --- code
 
}   private int max (int a,  int b)
  {
  -- code
 
}
 

P4 Final Version
Simply delete all output statements except the final output statement which solves the problem  

Problem
Read several (at least one) non negative integers and compute their real average.  

- >
  / /standard java header stuff goes here
  {
  / /establish an accumulator
  int sum = 0;
  / /establish a counter to count data
  int count = 0;
  / /sum their integers
  int num = IO.read_int ();
  while (num > = 0)
  {
  sum = sum + num;
  count = count + 1;  / /or count + +
  num = IO.read_int ();
 
}   / /compute & display the average (cast as a double)
  double ave = (double) sum / (double) count;
  IO.println ( ``ave =  ``+ ave);
 
}