Source Code Files for Classes1 Examples ======================================================================== /** * Project Kid1 * Class Main * * Show how to use objects created from * the Kid class. Use the default noarg * constructor */ public class Main { public static void main(String[] args) { // Declare reference variable. Kid x; // Create object using default // noarg constructor. x = new Kid(); // Declare reference variable and // create one object in one step. Kid y = new Kid(); // Set instance variables in x. x.name = "Jane"; x.gender = 'F'; x.age = 29; // Set instance variables in y. y.name = "Tom"; y.gender = 'M'; y.age = 31; // Invoke display methods of x and y. x.display(); y.display(); // Invoke toString methods of x and y. System.out.println(x); System.out.println(y); } } // Output: Name: Jane Gender: F Age: 29 Name: Tom Gender: M Age: 31 Jane F 29 Tom M 31 ------------------------------------------------------------------------ /** * Project Kid1 * Class Kid */ public class Kid { // Instance variables. public String name; public char gender; public int age; // When no constructor is defined, the // default constructor is the noarg // constructor that initializes // (1) numeric variables to zero, // (2) char variables to '\0', // (3) String variables to null, // (4) boolean variables to false. // display method for printing. public void display() { System.out.println("Name: " + name); System.out.println("Gender: " + gender); System.out.println("Age: " + age); System.out.println(); } // toString method returns object // information as a string. public String toString() { return name + " " + gender + " " + age; } } ======================================================================== /** * Project Kid2 * Class Main * * Show how to use objects created from * the Kid class. Use the default noarg * constructor */ public class Main { public static void main(String[] args) { // Declare and create object x with // parameterized constructor. Kid x = new Kid("Jane", 'F', 29); // Declare and create object y with // parameterized constructor. Kid y = new Kid("Tom", 'M', 31); // Declare and create object z with // noarg constructor. Kid z = new Kid(); // Test the getName method System.out.println( "getName method for x: " + x.getName()); // Test the getGender method System.out.println( "getGender method for x: " + x.getGender()); // Test the getAge method System.out.println( "getAge method for x: " + x.getAge()); System.out.println(); // Invoke toString methods of x and y. System.out.println(x); System.out.println(y); System.out.println(z); System.out.println(); // Test the hasBirthday method. y.hasBirthday(); // Text the setGender method. y.setGender('F'); System.out.println(y); } } // Output: getName method for x: Jane getGender method for x: F getAge method for x: 29 Jane F 29 Tom M 31 Unknown U -1 Tom F 32 ------------------------------------------------------------------------ /** * Project Kid2 * Class Kid */ public class Kid { // Private instance variables. private String name; private char gender; private int age; // Noarg constructor; public Kid() { name = "Unknown"; gender = 'U'; age = -1; } // Parameterized constructor, which // performs data validation. public Kid(String aName, char aGender, int anAge) { // Don't allow empty string as a name. if (aName.equals("")) name = "Unknown"; else name = aName; // Gender must be 'F' or 'M'. if (aGender == 'F' || aGender == 'M') gender = aGender; else gender = 'U'; // Age must be nonnegative. if (anAge < 0) age = -1; else age = anAge; } // Accessor method for name. public String getName() { return name; } // Accessor method for gender. public char getGender() { return gender; } // Accessor method for name. public int getAge() { return age; } // Mutator method for gender. public void setGender(char aGender) { // Gender must be 'F' or 'M'. if (aGender == 'F' || aGender == 'M') gender = aGender; else gender = 'U'; } // hasBirthday method adds 1 to age. public void hasBirthday() { age++; } // toString method returns object // information as a string. public String toString() { return name + " " + gender + " " + age; } } ======================================================================== /** * Project Kid3 * Class Main * * Show how reference variables can * be reassigned. */ public class Main { public static void main(String[] args) { // Declare and create object x with // parameterized constructor. Kid x = new Kid("Jane", 'F', 29); // Declare and create object y with // parameterized constructor. Kid y = new Kid("Tom", 'M', 31); // Check original values of x and y. System.out.println(x); System.out.println(y); System.out.println(); // Reassign y. The object "Tom" can be // reclaimed by the garbage collector. y = x; // Check new values of x and y. System.out.println(x); System.out.println(y); System.out.println(); // Add one to age of x. x.hasBirthday(); // Check that y is an alias for x. System.out.println(x); System.out.println(y); // Object "Jane" can be reclaimed by // garbage collector. x = null; y = null; } } // Output: Jane F 29 Tom M 31 Jane F 29 Jane F 29 Jane F 30 Jane F 30 ------------------------------------------------------------------------ /** * Project Kid3 * Class Kid */ public class Kid { // Private instance variables. private String name; private char gender; private int age; // Noarg constructor; public Kid() { name = "Unknown"; gender = 'U'; age = -1; } // Parameterized constructor, which // performs data validation. public Kid(String aName, char aGender, int anAge) { // Don't allow empty string as a name. if (aName.equals("")) name = "Unknown"; else name = aName; // Gender must be 'F' or 'M'. if (aGender == 'F' || aGender == 'M') gender = aGender; else gender = 'U'; // Age must be nonnegative. if (anAge < 0) age = -1; else age = anAge; } // Accessor method for name. public String getName() { return name; } // Accessor method for gender. public char getGender() { return gender; } // Accessor method for name. public int getAge() { return age; } // Mutator method for gender. public void setGender(char aGender) { // Gender must be 'F' or 'M'. if (aGender == 'F' || aGender == 'M') gender = aGender; else gender = 'U'; } // hasBirthday method adds 1 to age. public void hasBirthday() { age++; } // toString method returns object // information as a string. public String toString() { return name + " " + gender + " " + age; } } ======================================================================== /** * Project Pair1 * Class Main * * Illustrate how to create Pair objects with * overloaded constructors. */ public class Main { public static void main(String[] args) { // Declare and create Pair objects. Pair u = new Pair(); Pair v = new Pair(3); Pair w = new Pair(5, 8); System.out.println(u + " " + v + " " + w); } } // Output: (0,0) (3,3) (5,8) ------------------------------------------------------------------------ /** * Project Pair1 * Class Pair * */ public class Pair { // Instance variables are x and y coordinates. private int x, y; // Noarg constructor. public Pair() { x = 0; y = 0; } // Constructor with one parameter. public Pair(int value) { x = value; y = value; } // Constructor with two arguments. public Pair(int xValue, int yValue) { x = xValue; y = yValue; } // Accessor method for x. public int getX() { return x; } // Accessor method for y. public int getY() { return y; } // toString method. public String toString() { return "(" + x + "," + y + ")"; } } ======================================================================== /** * Project Pair2 * Class Points * * Demonstrate the Pair display method that * plots points on an Applet. */ import java.applet.Applet; import java.awt.*; public class Points extends Applet { public void init() { setBackground(Color.white); setSize(400, 400); } public void paint(Graphics g) { int i, x, y; Pair p; // Set color of Pair objects to red. g.setColor(Color.red); // Create and display 500 Pair objects. for(i = 1; i <= 500; i++) { // Pick random x value. x = (int) (400 * Math.random()); // Pick random x value. y = (int) (400 * Math.random()); // Create new Pair with random // x, y values. p = new Pair(x, y); // Display point on applet. p.display(g); } } } // Display 500 random points in applet. ------------------------------------------------------------------------ /** * Project Pair2 * Class Points * * Demonstrate the Pair display method that * plots points on an Applet. */ import java.applet.Applet; import java.awt.*; public class Points extends Applet { public void init() { setBackground(Color.white); setSize(400, 400); } public void paint(Graphics g) { int i, x, y; Pair p; // Set color of Pair objects to red. g.setColor(Color.red); // Create and display 500 Pair objects. for(i = 1; i <= 500; i++) { // Pick random x value. x = (int) (400 * Math.random()); // Pick random x value. y = (int) (400 * Math.random()); // Create new Pair with random // x, y values. p = new Pair(x, y); // Display point on applet. p.display(g); } } } ======================================================================== /** * Project Student * Class Main * * Illustrate methods that compute averages * of instance variables. */ import java.text.DecimalFormat; public class Main { public static void main(String[] args) { DecimalFormat df = new DecimalFormat("#0"); Student x = new Student( "Jane", 100, 95, 100, 90, 85, 86, 92); System.out.println("x project average: " + df.format(x.getProjectAve())); System.out.println("x exam average: " + df.format(x.getExamAve())); System.out.println("x course average: " + df.format(x.getCourseAve())); System.out.println("x course grade: " + x.getCourseGrade()); } } // Output: x project average: 94 x exam average: 89 x course average: 91 x course grade: A ------------------------------------------------------------------------ /** * Project Student * Class Student * * The student class contains name, project scores, * midterm score, and final score. The methods * compute averages of the instance variables. */ public class Student { // Instance Variables. private String name; private int proj1, proj2, proj3, proj4, proj5; private int midterm, fin; // Parameterized constructor. public Student(String n, int p1, int p2, int p3, int p4, int p5, int m, int f) { proj1 = p1; proj2 = p2; proj3 = p3; proj4 = p4; proj5 = p5; midterm = m; fin = f; } // Methods to compute averages. public double getProjectAve() { return (proj1+proj2+proj3+proj4+proj5)/5; } public double getExamAve() { return (midterm + fin) / 2; } public double getCourseAve() { return (getProjectAve() + 2 * getExamAve()) / 3; } public char getCourseGrade() { double grade; grade = getCourseAve(); if (grade >= 89.5) return 'A'; else if (grade >= 79.5) return 'B'; else if (grade >= 69.5) return 'C'; else if (grade >= 59.5) return 'D'; else return 'F'; } } ========================================================================