Tutorial 3      Lecturer: Craig MacDonald  Tutorial: Mon@10:10am    970929
==========

CSC 108, Fall 1997                                     Tutorial notes,  T3
==========================================================================

Tutorial Notes from Instructor Craig MacDonald:

Invite questions about the assignment.  Answer specific questions,
especially about issues of style, commenting and so on.  Tell them
what you are looking for when marking, such as "You will lose marks if
you don't do such and such".  Discourage "bells and whisles".

Here are 3 tested programs that cover class material.  I don't expect
to you get through all of this material.  Let me know how much is left
over.

EXAMPLE OF USE OF IF-ELSE-IF: extends last weeks example

import java.io.*;
class FahrToCelsius {
  public static void main (String[] args)  {
    final String F = " Fahrenheit";
    final String C = " Celcius";
    double fahrenheit, celsius;
    DataInputStream stdin = new DataInputStream(System.in);
    System.out.println ("Please enter temperature.");
    try 
      {
	String temperature = stdin.readLine();
	System.out.println ("Please enter C (Celcius) or F (Fahrenheit).");
	String measure = stdin.readLine();

        if ("F".equals(measure)) {
	  fahrenheit = new Double(temperature).doubleValue();
	  celsius = (fahrenheit-32)*5.0/9.0; // ensure floating point
	  System.out.println (fahrenheit +  F + " is " + celsius + C);
      }
	else if ("C".equals(measure)) {           
	  celsius = new Double(temperature).doubleValue();
	  fahrenheit = celsius*9.0/5.0+32;
	  System.out.println (celsius + C + " is " + fahrenheit + F);
	}
      }
    catch (IOException e)
      {
	System.out.println ("Input error: " + e);
      }
  }
}
/* java FahrToCelsius
Please enter temperature in temperature.
212
Please enter C (Celcius) or F (Fahrenheit).
F
212.0 Fahrenheit is 100.0 Celsius
*/

Ask what a representative minimum number of tests might be to be
confident in this program's correctness.

EXAMPLE OF WHILE LOOP:  they have seen a while loop.

public class MilesToKilometers {

  /**
   Command line arguments are
   Start and Stop Values and positive increment
   for conversion from Miles to Kilometers
   Fix the infinite loop when inc <= 0
   */

  public static void main (String[] args)  {
    final double factor = 1.6093;
    if (args.length != 3) 
      System.out.println ("Command line arguments are\nStart and Stop
Values and positive increment\nfor conversion from Miles to Kilometers");
    else {
      double miles = new Double(args[0]).doubleValue();
      double miles_end   = new Double(args[1]).doubleValue();
      double increment = new Double(args[2]).doubleValue();
      double kilometers;
      System.out.println ("Miles\tKilometers");
      while (miles <= miles_end) {
	kilometers = Math.round(miles * factor * 100.00)/100.00;
	System.out.println (miles + "\t" + kilometers);
	miles += increment;
      }
    }
  }
}
/*
> java MilesToKilometers 0 100 10

Miles	Kilometers
0.0	0.0
10.0	16.09
20.0	32.19
30.0	48.28
...     ...
90.0	144.84
100.0	160.93
*/

ONE CLASS HAS A FIELD WHICH IS AN OBJECT OF ANOTHER CLASS.

The Circle class HAS a Center (Point object) and HAS a radius (primitive).

class Point {
    private double x;
    private double y;

    Point(double xvalue, double yvalue) {
      x = xvalue;
      y = yvalue;
    }

    public void shift (double x, double y) {
      this.x = x;
      this.y = y;
    }
    public String toString() {
      return "(" + x + "," + y + ")";
    }
    public double getX() { return x; }
    public double getY() { return y; }
}

class Circle {
    private Point center;  // A circle HAS A special point called the center
    private double radius; //   a circle HAS A radius.

    Circle (Point center, double radius) {
      this.center = center;
      this.radius = radius;
    }
    public void deltaShiftCenter (double dx, double dy) {
      center.shift (center.getX() + dx, center.getY() + dy);
    }
    public void deltaShiftRadius (double radius) {
      this.radius = this.radius + radius;
    }
    public String toString() {
      return "center = " + center + "\nradius = " + radius;
	}
    public static void main (String[] args) {
      Point center = new Point(1,2);
      double radius = 10.0;
      Circle c = new Circle (center,radius);
      System.out.println (c); // before shifting
      c.deltaShiftCenter(0,10); // shift center to center plus (dx,dy)
      System.out.println (c);  // after shifting center.
      c.deltaShiftRadius(5.0); // Add 5.0 to radius.
      System.out.println (c);  // after shifting radius.
    }
}
/*
java Circle
center = (1.0,2.0)
radius = 10.0
center = (1.0,12.0)
radius = 10.0
center = (1.0,12.0)
radius = 15.0
*/

Craig Mac Donald <craig@cs.toronto.edu>