Recitation 09152006 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Recitation 09152006

Description:

class Terminator { //the beginning of our class's ... ( So, the Terminator class can be used even after removing the method in blue) In ... public Terminator ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 14
Provided by: webIcs
Category:

less

Transcript and Presenter's Notes

Title: Recitation 09152006


1
Recitation 09/15/2006
  • CS 180
  • Department of Computer Science,
  • Purdue University

2
Announcements
  • First Exam
  • Date September 27th Wednesday
  • Feel free to ask the TAs any questions
  • (Consultation hours are great for this)
  • Project 3 released
  • Due on September 28th _at_ 1030pm
  • Start early (you have an exam on the 27th)!!
  • Make use of TA consultation hours
  • Make sure your code compiles

3
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

4
DEFINING YOUR OWN CLASSES How would you use a
class you defined?(How would you create and use
objects that are of your classs type?)
  • //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 Term
inator( ) Terminator T3 new Terminator( )
T3.setName(TX) System.out.printl
n(T1.getName()) System.out.println(T2.getName
()) System.out.println(T3.getName())
5
DEFINING YOUR OWN CLASSES Lets look at the
components of a class..
  • class Terminator //the beginning of our
    classs definition
  • private String name //an instance variable,
    name, of type String
  • public Terminator( ) //the constructor for
    this class (constructors share the class name)
  • name T1000 //(A constructor is a
    method with no return value not even void)
  • //(constructors can take parameters, but
    this one doesnt.)
  • 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 this class that takes one String parameter
  • name newName //this method doesnt
    return a value.
  • //(but unlike the constructor, we must
    explicitly state this with void)
  • //the ending of the definition of our
    class

6
DEFINING YOUR OWN CLASSES What does public
and private mean?Illegal usage is in red
  • //file Terminator2.java
  • class Terminator2
  • private String name
  • private String getAClue( )
  • return the 3rd movie was weak
  • //private this item can only be accessed from
    //within this class
  • public Terminator2( )
  • name T1000
  • //public this item can be accessed from within
    //this class and from outside this class
  • public String getName( )
  • return name getAClue( )
  • public void setName(String newName)
  • name newName

//file TestTerminator2.java class TestTerminat
or2 public static void main( String arg )
Terminator2 T new Terminator2( )
T.setName(Cyberdyne Systems)
System.out.println(T.getName())
System.out.println(T.getAClue())
By defining members as private, you can
control how those members are modified. This is
useful for ensuring data integrity. Also, by
forcing users to access variables through
methods, the implementation of the methods can be
modified without changing any code that uses
those methods. This concept is known as informa
tion hiding
7
DEFINING YOUR OWN CLASSES What is this
constructor (Does a Terminator really need a
constructor?)
  • class Terminator
  • private String name
  • public Terminator( )
  • name T1000
  • //other stuff

The constructor is a function that specifies ho
w the class should be initialized. Initialization
is what takes place after using the keyword new.
A constructor does not have to be defined. (So,
the Terminator class can be used even after
removing the method in blue) In such a case, a
default constructor that takes no arguments and
which has an empty body, is put into your class
public Terminator( ) () It is possible
to define a constructor that takes parameters
public Terminator( int a) System.out.prinln
(a)
8
DEFINING YOUR OWN CLASSES 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")
  • Terminator T2 new Terminator( )
  • T2.setName("T2 Name")
  • messup(T1)
  • System.out.println(T1.getName())
  • String S "S Name"
  • messup(S)
  • System.out.println(S)
  • Terminator T4 new Terminator()

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 (The assignment operator (
) when used with objects (except Strings),
results in a reference copy, while when used with
basic data types, results in a value copy.)
Passing by reference modifications to the
parameter result in modifications to the original
item Passing by value modifications to the para
meter dont result in modifications to the
original item program output defunct Termi
nator S Name T2 Name modified T2 Name
9
DEFINING YOUR OWN CLASSES 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

Both A and B are constants because of the final
qualifier. However, A is an instance constant,
while B is a class constant. Here, each individ
ual Terminator3 object will have its own copy of
the int A. However, the static modifier indicates
that there will be only one copy of int B, that
will be shared among all Terminator3 objects.
So, the presence of the static modifier essentia
lly dictates if an item is duplicated, or if one
copy is shared among all instances of a
particular class object. Side Note Just like t
he movie , the Terminator3 class doesnt seem to
serve any real purpose.
10
DEFINING YOUR OWN CLASSES local variables
  • class Terminator4
  • private int A
  • private int B
  • private int C
  • public Terminator4( )
  • A 1
  • B 2
  • C 3
  • public void process( int A )
  • int B
  • A 4
  • B 5
  • C 6

Let us define a block of code as a segment of
code that begins with a and ends with a .
Furthermore, when it comes to methods, the names
of the method parameters also belong to the
segment of code within the method body.
Rule Within any particular segment of code, you
are not allowed to have duplicate identifier
names (methods or variables).
However, if you have a segment of code C2 within
another segment of code C1, then you are allowed
to reuse identifier names in declarations. In
such a case, the duplicated identifier names will
always refer to their closest definition.
Eg- See the color codes on the left to see whic
h statement corresponds to which variable
11
DEFINING YOUR OWN CLASSES calling methods
  • class Terminator5
  • public void didIt5( ) System.exit(0)
  • public void doIt5( )
  • System.out.print(5)
  • didIt5( )
  • Class Terminator6
  • public void didIt6( ) System.exit(0)
  • public void doIt6( )
  • Terminator5 T new Terminator5( )
  • System.out.print(6)
  • T.didIt5( )
  • didIt6( )

When you call a method thats within the same
class, you can call the method by just using its
name. However, 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.
Eg- see the code on the left () Notice, w
e havent defined any constructors
12
DEFINING YOUR OWN CLASSES The main class
  • //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( )

When you run your program using java, program
execution starts by following the instructions in
a main( ) method. So, in order to do this, a
main( ) method must be defined in the file you
run. Furthermore, even if you are referencing m
any different files that have multiple main( )
methods, only the main( ) method of the file you
call explicitly with java will be run.
Eg- See which command executes which main( ) me
thod based on color coding Java TerminatorA
Java TerminatorB
13
DEFINING YOUR OWN CLASSES QUIZ
  • QUIZ FORMAT
  • Name
  • Date
  • Recitation Time Slot
  • Answer to Quiz
Write a Comment
User Comments (0)
About PowerShow.com