University of Toronto - Fall 2000
Department of Computer Science

Week 11 - Overloading

Overloaded Constructor - without super

public class School {
	public static void main (String[] args) {
		Student one = new Student("Bob","0101");
		Student two = new Student("Sue","0102",5,3.10);
		...
	}
}

/*
* Students have a name, student number, number of  
* course credits, and a grade point average. If the
* student has no academic history, the number of
* credits and grade point average are both zero.
*/
class Student {
	private String name;
	private String stunum;
	private int credits;
	private double gpa;

	// Create this student with no academic history.
	// This Student has name 'n' & student num 's'.
	public Student (String n, String s) {
		this.name = n;
		this.stunum = s;
	}

	// Create this Student with name 'n', student
	// number 's', 'c' credits, and CGA 'g'.
	public Student (String n,String s,int c,double g){
		this.name = n;
		this.stunum = s;
		this.credits = c;
		this.gpa = g;
	}
	...
}

Overloaded Constructor - with super

class Student {
	private String name;
	private String stunum;
	private int credits;
	private double gpa;

	public Student (String n, String s) {
		this (n, s, 0, 0.0);
	}

	public Student (String n,String s,int c,double g){
		this.name = n;
		this.stunum = s;
		this.credits = c;
		this.gpa = g;
	}
	...
}

Method overloading

public class School {
	public static void main (String[] args) {

		final int PASS_NUMBER = 50;
		final String HIGHEST_FAIL_LETTER = "E";

		System.out.print("letter grade? (y/n) ");

		if (in.readLine().equals("y")) {
			String mark = in.readLine();
			if (isPass(mark)) {
				System.out.println("pass");
			}
		}

		else { // numerical
			int mark = Integer.parseInt(in.readLine());
			if (isPass(mark)) {
				System.out.println("pass");
			}
		}

	private static boolean isPass (int mark) {
		return mark >= PASS_NUMBER;
	}

	private static boolean isPass (String mark) {
		return mark.compareTo(HIGHEST_FAIL_LETTER) < 0;
	}
}