Class Notes (806,856)
Canada (492,490)
COMP 1405 (38)


7 Pages
Unlock Document

Carleton University
Computer Science
COMP 1405
Christine Laurendeau

Lecture 5 Class 1 //class below is required for the Ch2_3InitializingObject program class Ch2_3Person { String firstName; String lastName; //Defines String names } Lecture 5 Program 1 //Program requires Ch2_3Person class to run, demonstrates the usage of //class object without constructors class Ch2_3InitializingObject { public static void main (String args[]) { Ch2_3Person p1, p2; //defines Ch2_3Person objects named p1 and p2 p1 = new Ch2_3Person(); //creates a new Ch2_3Person object for p1 p1.firstName = "Hank"; p1.lastName = "Urchif"; //set values to variables in p1 p2 = new Ch2_3Person(); //creates a new Ch2_3Person object for p2 p2.firstName = "Holly"; p2.lastName = "Day"; //set values to variables in p2 } } Lecture 5 Class 2 //The following class demonstrates the use of constructor and will //be required for the Ch2_3InitializingObject2 program class Ch2_3Person2 { String firstName; String lastName; //Defines String names Ch2_3Person2(String s1, String s2) { /*This is a constructor * The constructor name must be same as the class name. * The content in the brackets of the constructor are used to set * the values of variables */ this.firstName = s1; this.lastName = s2; //This is how the constructor set variable values //It takes parameter values and set them to the names by order //The word "this" means this object } } Lecture 5 Program 2 //The following class demonstrates the use of constructor and will //require the Ch2_3Person2 class class Ch2_3InitializingObject2 { public static void main (String aqrgs[]) { Ch2_3Person2 p1, p2; //defines object names p1 = new Ch2_3Person2("Hank", "Urchif"); p2 = new Ch2_3Person2("Holly", "Day"); //creates a new object class and sets parameter /*The constructor Ch2_3Person2(String s1, String s2) * takes in 2 parameters, and they are both String (s1 and s2) * In this example, "Hank" is s1 and will be set as firstName, * and "Urchif" is s2 and will be set as lastName * Same concept with Holly Day */ } } Lecture 5 Class 3 //below is a more advanced example of constructor //will be required for the Ch2_3InitializingObject3 program
More Less

Related notes for COMP 1405

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.