.Tutorial # 11.

Program Debugging



No matter how cautious, and how well trained you are in programming, not all your programs will work the first time you try to run them. It is natural. So I have come up some simple techniques, to get your program working with less hastle.

COMPILER ERROR:
     To start, your program must compile. Meaning that when you run the javac command (or click make, run, etc), to compile, the compiler will try to make a program to be run. Sometimes it will give you details about why the program won't compile, or let you know that there is some wierdness in your code. Try to figure out what you have done wrong. This usually is something in the form of syntax (missing ';'s, or typos). If you can't find the problem, try looking through your code, or removing some of your code (temporarily), to locate the source of the problem. The output the compiler sends as its error message is usually very informative, with line numbers of where the problem originated, and what methods it was traced through.

RUN-TIME ERROR:
     If your code has made it past the initial compilation, but it creates unexpected output, or if it crashes, you will have to track down the source of the headache. To do this, try putting in extra System.out.println()'s. Put them at the beginning of each method, and various places where you are unsure about. So if the program crashes after it prints out a 4, then you know it made it past the spot where it printed 4, and did not make it to the 5, hence it has an error (or at least the error was noticed) between 4 and 5.

OTHER NOTES:
     When you are programming, add one method at a time, and check after each major addition if possible. This will allow you to debug as you program, and thus saves you from trying to track down the error when you have many more lines to look through.
Inheritance


/* In this example you begin by defining a more general MotorVehicle class.  */
public class MotorVehicle {

  protected String licensePlate;     // e.g. "New York A456 324"
  protected double speed;            // kilometers per hour
  protected double maxSpeed;         // kilometers per hour
  protected String make;             // e.g. "Harley-Davidson", "Ford"
  protected String model;            // e.g. "Fatboy", "Taurus"
  protected int    year;             // e.g. 1998, 1999, 2000, 2001
  protected int    numberPassengers; // e.g. 4
  
  
  // constructors
  public MotorVehicle(String licensePlate, double maxSpeed,
   String make, String model, int year, int numberOfPassengers) {

    this(licensePlate, maxSpeed, make, model, year,
 	numberOfPassengers);    
  }

  public MotorVehicle(String licensePlate, double speed, double
 	maxSpeed, String make, String model, int year, int
 	numberOfPassengers) {

	this(licensePlate, speed, maxSpeed, make, model, year, 
		numberOfPassengers);    
  }

  public MotorVehicle(String licensePlate, double speed, double
 	maxSpeed, String make, String model, int year, int
 	numberOfPassengers) {

    // I could add some more constraints like the
    // number of doors being positive but I won't
    // so that this example doesn't get too big.
    this.licensePlate = licensePlate; 
    this.make = make; 
    this.model = model; 
    this.year = year; 
    this.numberPassengers = numberOfPassengers; 

    if (maxSpeed >= 0.0) {
	this.maxSpeed = maxSpeed;
    } else {
	maxSpeed = 0.0;
    }
    
    if (speed < 0.0) {
	speed = 0.0;
    }
    
    if (speed <= maxSpeed) {
	this.speed = speed;
    } else {
	this.speed = maxSpeed;
    }
  }
  
  
  // get methods
  public String getLicensePlate() {
	return this.licensePlate;
  }

  public String getMake() {
	return this.make;
  }

  public String getModel() {
	return this.model;
  }

  public int getYear() {
	return this.year;
  }
  
  public int getNumberOfPassengers() {
	return this.numberPassengers;
  }
  
  public int getNumberOfPassengers() {
	return this.numberWheels;
  }
  
  public double getMaxSpeed() {
	return this.speed;
  }

  public double getSpeed() {
	return this.maxSpeed;
  }

  // set method for the license plate property
  protected void setLicensePlate(String licensePlate) {
    this.licensePlate = licensePlate;
  }

  // accelerate to maximum speed
  // put the pedal to the metal
  public void floorIt() {
	this.speed = this.maxSpeed;  
  }
  
  public void accelerate(double deltaV) {
	this.speed = this.speed + deltaV;
	if (this.speed > this.maxSpeed) {
		this.speed = this.maxSpeed; 
	}
	if (this.speed < 0.0) {
		this.speed = 0.0; 
	}     
  }
}

/* 
The MotorVehicle class has all the characteristics shared by motorcycles
and cars, but it leaves the number of wheels unspecified, and it doesn't have
a numberDoors field since not all motor vehicles have doors. It also makes
the fields and the setLicensePlate() method protected instead of private 
and public.  
Next you define two subclasses of MotorVehicle, one for cars and one for 
motorcycles. To do this you use the keyword extends. 
*/

public class Motorcycle extends MotorVehicle {
  protected int numberWheels = 2;  
  
  // constructors
  public Motorcycle(String licensePlate, double maxSpeed,
   String make, String model, int year, int numberOfPassengers) {
	this(licensePlate, 0.0, maxSpeed, make, model, year,
 	numberOfPassengers);    
  }

  public Motorcycle(String licensePlate, double speed, double maxSpeed,
   String make, String model, int year, int numberOfPassengers) {
	// invoke superclass constructor
	super(licensePlate, speed, maxSpeed, make, model, year, 
		numberOfPassengers);   
  }
  
  public int getNumberOfWheels() {
	return this.numberWheels;
  }
}

/* k++ vs. ++k */
class AddInts {
  public static void main (String args[]) {
    int k = 10;
    int f = k;
    System.out.println(++k);	// 11
    System.out.println(k++);	// 11
    System.out.println(k);	// 12
    f = k++;  
    System.out.println(f);	// 12 
    System.out.println(k);	// 13
  }
} 



Java Memory Model Links


From downtown:
PDF Format
PostScript Format

From Scarabourgh:
PostScript Format

sitemap