Class Notes (807,350)
Canada (492,715)
COMP 1405 (38)


8 Pages
Unlock Document

Carleton University
Computer Science
COMP 1405
Christine Laurendeau

Lecture 4 Program 1 //The following program shows the usage of String data type class Ch2_0String3 { public static void main (String args[]) { String firstName1, lastName1; String firstName2, lastName2; String firstName3, lastName3; //Declares String names firstName1 = "Hank"; lastName1 = "Urchif"; firstName2 = "Holly"; lastName2 = "Day"; firstName3 = "Bobby"; lastName3 = "Socks"; //Set string values System.out.println(lastName1 + ", " + firstName1); System.out.println(lastName2 + ", " + firstName2); System.out.println(lastName3 + ", " + firstName3); //print Strings in format (lastName, firstName) } } Lecture 4 Class 1 //Class object required for the Ch2_1Person and Ch2_1Person2 program class Ch2_1PersonObject { //creates an object file String firstName; String lastName; //declares 2 string names in the object file //Other Data types such as int, char and boolean etc can also be used here } Lecture 4 Program 2 //The following Program demonstrates the usage of class objects class Ch2_1Person { public static void main (String args[]) { Ch2_1PersonObject aPerson; //Declares an object name aPerson = new Ch2_1PersonObject(); //creates a new Ch2_1PersonObject aPerson.firstName = "Hank"; aPerson.lastName = "Urchif"; //sets values to the firstName and lastName String in the //Ch2_1PersonObject class // "." means an component (such as variable, function etc) with an class System.out.println(aPerson.firstName + " " + aPerson.lastName); System.out.println(aPerson.lastName + ", " + aPerson.firstName); //outputs the same result as the previous program aPerson.firstName = "Max"; aPerson.lastName = "Power"; //changes the string values to max power System.out.println(aPerson.firstName + " " + aPerson.lastName); //Outputs new name Max Power } } Lecture 4 Program 3 //The following Program demonstrates the usage of class objects class Ch2_1Person2 { public static void main (String args[]) { Ch2_1PersonObject p1, p2, p3; //declare 3 Ch2_1PersonObject names p1 = new Ch2_1PersonObject(); p2 = new Ch2_1PersonObject(); p3 = new Ch2_1PersonObject(); //creates new Ch2_1PersonObject objects for each name p1.firstName = "Hank"; p1.lastName = "Urchif";
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.