Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

17
Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

Page 1: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

Recitation 09/12/2007

CS 180

Department of Computer Science,

Purdue University

Page 2: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2

Announcements & Reminders

Project 1 grades out Remember to collect them at the end of the recitation Solution up on the web

Project 2 was due last night Project 3 due on September 17th, 10 PM Students must sign the academic integrity policy.

CS 180 students will not be allowed to take Exam 1 if they have not "signed" this policy!

Page 3: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

Template for class definition

Page 4: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

4

Defining your own classes :

What would a class look like? (Remember that a class is a blueprint for creating objects)

class Terminator {

private String name;

public Terminator( ) {

name = “T1000”;

}

public String getName( ) {

return name;

}

public void setName(String newName) {

name = newName;

}

}

Page 5: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

5

//file: Terminator.java

class Terminator {

private String name;

public Terminator( ) {

name = “T1000”;

}

public String getName( ) {

return name;

}

public void setName(String newName) {

name = newName;

}

}

//file: RunTerminator.java

class RunTerminator {

public static void main( String [] arg ) {

Terminator T1;

T1 = new Terminator( );

T1.setName(“T101”);

Terminator T2 = new Terminator();

Terminator T3 = new Terminator( );

T3.setName(“TX”);

System.out.println(T1.getName());

System.out.println(T2.getName());

System.out.println(T3.getName());

}

}

Using objects of your class

Page 6: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

6

class Terminator { //the beginning of our class’s definition

private String name; //an instance variable, name, of type String

public Terminator( ) { //the constructor for this class

name = “T1000”; //(A constructor has no return value)

} //(constructors can take parameters, but this one doesn’t.)

public String getName( ) { //a method for this class that takes no parameters

return name; //this method returns a String variable

}

public void setName(String newName) { //a method for that takes one String parameter

name = newName; //this method doesn’t return a value.

}

} //the ending of the definition of our class

Components of a class

Page 7: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

7

//file: Terminator2.java

class Terminator2{

private String name;

private String getAClue( ) {

return “the 3rd movie was weak”;

}

public Terminator2( ) {

name = “T1000”;

}

public String getName( ) {

return name + “:“ + getAClue( );

}

public void setName(String newName) {

name = newName;

}

}

//file: TestTerminator2.java

class TestTerminator2 {

public static void main( String [] arg ) {

Terminator2 T = new Terminator2( );

T.setName(“Cyberdyne Systems”);

System.out.println(T.getName());

System.out.println(T.getAClue());

}

}

Public and Private members

Page 8: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

8

Constructor

Specifies how the class should be initialized

class Terminator {

private String name; If not specified, defaultconstructor will be used

public Terminator( ) {name = “T1000”;

}

// A constructor that takes parameterspublic Terminator( int a) {

System.out.prinln(a);}

//other stuff}

Page 9: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

9

Passing class objects as parameters: (Passing by reference vs. passing by value)

//file: RunTerminator.java

class RunTerminator {public static void messup(Terminator t) {

t.setName("defunct Terminator");}public static void messup(String s) {

s = "defunct String";}public static void main( String [] arg ) {

Terminator T1 = new Terminator( );T1.setName("T1 Name");messup(T1);System.out.println(T1.getName());

String S = "S Name";messup(S);System.out.println(S);

}}

class objects (except Strings) are transferred as references when they are passed as parameters to a method.In contrast, basic data types like int and double are passed by value.(*) Even though it is a class object, String objects behave similarly to basic data types and are passed by value

Program output :

defunct Terminator

S Name

Page 10: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

10

Defining class constants: the static keyword

class Terminator3{

private final int A = 10;

private static final int B = 20;

public Terminator3( ) {

}

public int getNumbers( ) {

return A + B;

}

}

A is an instance constant, while B is a class constant.

Here, each individual Terminator3 object will have its own copy of the int A.

There will be only one copy of int B, that will be shared among all Terminator3 objects.

Page 11: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

11

Calling methods

class Terminator5 {

public void didIt5( ) {System.exit(0);}

public void doIt5( ) {

didIt5( );

}

}

Class Terminator6{

public void didIt6( ) {System.exit(0);}

public void doIt6( ) {

Terminator5 T = new Terminator5( );

T.didIt5( );

didIt6( );

}

}

When you call a method that’s within the same class, you can call the method by just using its name.

If you call a method that is in a different class, then you must refer to that method using a . (dot) notation that first references the separate class object.

Page 12: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

12

The main method

//file: TerminatorA.java

class TerminatorA{

public int A;

public static void main( String [ ] arg ) {

TerminatorB T = new TerminatorB( );

}

}

//file: TerminatorB.java

Class TerminatorB{

public int B;

public static void main( String [ ] arg ) {

TerminatorA T = new TerminatorA( );

}

}

A main( ) method must be defined in the file you run.

Only the main( ) method of the file you call explicitly with “java” will be run.

Eg:- See which command executes which main( ) method based on color coding:

Java TerminatorA

Java TerminatorB

Page 13: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

Identifier types

Identifiers can be declared almost anywhere in a program.

There are three main types of declarations: Data members of a class

Declared outside any method Usually at the beginning of the class definition

As formal parameters of a method Within a method -- local variables

Page 14: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.
Page 15: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

Sample matching

Page 16: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

Sample matching

Notice how one can hide data members bydeclaring a local variable with the same name

Page 17: Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.

Things to remember

A local variable can be declared just about anywhere! Its scope (the area of code from where it is

visible) is limited to the enclosing braces. Statements within a pair of braces are

called a block. Local variables are destroyed when the

block finishes execution. Data members of a class are declared

outside any method. Their scope is determined by public and private modifiers.