Powerpoint Slides - Purdue University
Download
Report
Transcript Powerpoint Slides - Purdue University
Recitation 09/12/2007
CS 180
Department of Computer Science,
Purdue University
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!
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
2
Template for class definition
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;
}
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
4
Using objects of your class
//file: Terminator.java
//file: RunTerminator.java
class Terminator {
class RunTerminator {
public static void main( String [] arg ) {
private String name;
Terminator T1;
public Terminator( ) {
T1 = new Terminator( );
T1.setName(“T101”);
name = “T1000”;
}
Terminator T2 = new Terminator();
public String getName( ) {
Terminator T3 = new Terminator( );
T3.setName(“TX”);
return name;
}
System.out.println(T1.getName());
public void setName(String newName)
{
System.out.println(T2.getName());
System.out.println(T3.getName());
name = newName;
}
}
}
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
5
Components of a class
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) {
name = newName;
//a method for that takes one String parameter
//this method doesn’t return a value.
}
}
//the ending of the definition of our class
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
6
Public and Private members
//file: TestTerminator2.java
//file: Terminator2.java
class Terminator2{
private String name;
class TestTerminator2 {
private String getAClue( ) {
public static void main( String [] arg ) {
return “the 3rd movie was weak”;
}
Terminator2 T = new Terminator2( );
public Terminator2( ) {
T.setName(“Cyberdyne Systems”);
name = “T1000”;
}
System.out.println(T.getName());
public String getName( ) {
System.out.println(T.getAClue());
return name + “:“ + getAClue( );
}
}
}
public void setName(String newName) {
name = newName;
}
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
7
Constructor
Specifies how the class should be initialized
class Terminator {
private String name;
If not specified, default
constructor will be used
public Terminator( ) {
name = “T1000”;
}
// A constructor that takes parameters
public Terminator( int a) {
System.out.prinln(a);
}
//other stuff
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
8
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());
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
String S = "S Name";
messup(S);
System.out.println(S);
}
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
9
Defining class constants: the static keyword
class Terminator3{
private final int A = 10;
A is an instance constant,
while B is a class constant.
private static final int B = 20;
public Terminator3( ) {
}
public int getNumbers( ) {
return A + B;
}
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.
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
10
Calling methods
class Terminator5 {
public void didIt5( ) {System.exit(0);}
public void doIt5( ) {
didIt5( );
When you call a method that’s
within the same class, you can call
the method by just using its name.
}
}
Class Terminator6{
public void didIt6( ) {System.exit(0);}
public void doIt6( ) {
Terminator5 T = new Terminator5( );
T.didIt5( );
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.
didIt6( );
}
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
11
The main method
//file: TerminatorA.java
class TerminatorA{
A main( ) method must be defined in the
file you run.
public int A;
public static void main( String [ ] arg ) {
TerminatorB T = new TerminatorB( );
}
Only the main( ) method of the file you
call explicitly with “java” will be run.
}
//file: TerminatorB.java
Eg:- See which command executes which
main( ) method based on color coding:
Class TerminatorB{
Java TerminatorA
public int B;
public static void main( String [ ] arg ) {
Java TerminatorB
TerminatorA T = new TerminatorA( );
}
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction
or display.
12
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
Sample matching
Sample matching
Notice how one can hide data members by
declaring a local variable with the same name
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.