Transcript Class Notes
ISYE 7210--Simulation of Real-Time Systems
Fall 2005
Classes & Objects
Note: See zip file on class Web page for examples used in class
www.chmsr.gatech.edu/ISyE7210/
www.horstmann.com/corejava.html
Q: 5.day.5 9/20/2005 9:13 PM
Christine M. Mitchell
Center for Human-Machine Systems Research (chmsr)
School of Industrial and Systems Engineering
(chmsr lab) ISyE Main, Room 426, 404 385-0363
(office) ISyE Groseclose, Room 334, 404 894-4321
{cm}@chmsr.gatech.edu
(9.7.05)
CHMSR
1
StringOverload API
Class Name
StringOverload()
Class Body
Class Declaration
public class StringOverload
Variables
private long quantity
{
private long quantity = 0;
public StringOverload ()
{
quantity = 0;
}
public StringOverload (long newQuant)
{
quantity = newQuant;
}
public long getQuantity()
{
return quantity;
}
public void setQuantity(long newQuantity)
{
quantity = newQuantity;
return; //optional
}
public String toString()
{
String msg = new String("Local toString(): Current value of
quantity is " + getQuantity()+ "\n")
return msg;
}
Variable
Constructor
Constructor
public class StringOverload()
Methods
Methods
public long getQuantity()
--returns value of quantity
public long setQuantity(long newQuant)
--set private variable, quantity,
public String toString()
--returns string representation of the
object
(9.7.05)
}
2
Variable Scope
Scope
Region of a program within which the variable can be referred to by
simple name
Determines if variable can be used from outside of the class
Location of the variable declaration establishes its scope & category
Local variable
Data item known within a block
Inaccessible to code outside the block.
Member variable
Field of a class
Default is not static
Method parameter
Exception-handler parameter
Distinct from visibility
Set
(9.7.05)
with an access modifier
3
Basic Java Syntax & Classes Plus our Coding Conventions (cont’d)
(Example 1: Part.java)
A Java class has two components
1. A class declaration
2. A class body
A Java class declaration includes specification of scope & class name
public Part (for class Part) //first line of class definition
Class scope (access modifier): public, private, protected, …
Note: An access modifier controls which other parts of a Java program
can see the Java class
(9.7.05)
A public scope means any other part of the Java program can “see” the
class
4
Overview: Classes
A Java class is a template that describes the form of a variable: an object
(instance) of that class
Class objects (instances): name & yourName
String name; String yourName;
Class template
1. Class declaration
a. Class scope
b. Attributes of the class
access level: public, private, protected
abstract: cannot be instantiated
final: cannot have a subclass
extends: descends from a superclass
implements: includes specification of variable and method details
defined in an interface
c.
Class name
d. Class defaults: nonpublic, nonabstract, nonfinal subclass of Object that
implements no interfaces.
(9.7.05)
5
Overview: Classes (cont’d)
(9.7.05)
A Java class template (cont’d)
2. Class body
a. Variables of the class: may not be defined within the body of a method
Types of variables in a class:
Instance variables: one for every instance of a class
class variables: exactly one for each class
Access: public, private, protected
Type: variable (int, float, ….) or an instance of another class
static: a class variable
final: a constant
static final: a class constant
static final double INTEREST_RATE = .04;
6
Class Definition Summary (cont’d)
A Java class template (cont’d)
2. class body
b. Constructors
Special “functions” that create an instance (an object) of the
class
Must have the same name as the class
Do not have a return type
Qualifiers
access level: public, private, protected
arguments (type anArgumentName, …..)
Default constructor
Java creates a default constructor that sets all the variables
of the class to their “natural” defaults, if the user does not
create ANY constructor!!!!!
(9.7.05)
If the user creates one or more constructors, then Java does
NOT create a default constructor; the user may create a
default constructor
7
Class Definition Summary (cont’d)
A Java class template (cont’d)
2. class body
b. constructors
Use of constructors
One step method: declare & initialize an instance of
ClassName, objectName, simultaneously
ClassName objectName = new ClassName(…);
Two step method
(1) Declare a reference variable of type ClassName,
objectName
(2) Initialize (and create memory) for the instance
(1) ClassName objectName = null; //initialization is optional
(2) objectName = new ClassName(…); //allocate memory
(1) Integer anInt = 0; //declare an instance
(2) anInt = new Integer (700); //initialize
(9.7.05)
8
Class Definition Summary (cont’d)
A Java class template (cont’d)
2.
class body
c.
(9.7.05)
Destructors
Java does not have destructor methods for a class
Java reclaims memory with garbage collection
Garbage collection happens in the background
Users are rarely aware of garbage collection
If speed is an issue (almost never!!), the programmer can
force garbage collection
Do not do without thinking
In ten years, I never had to explicitly call Java garbage
collection explicitly
9
Example: Part (a more sophisticated class)
Part.2a—Test a Java-Created Default Constructor
//Part.java (Part.2a--with Java created default constructor)
/**
A more complicated Part class.
*/
public class Part
{
/**
instance variables
*/
private int numParts = 25; //good to initialize
/**
Constructors (special class members)
Create an instance (object) of a class
Constructors must have the same name as the class
*/
/**
Default constructor: automatically created by Java
IF no other constructor exists.
Good style to create a default constructor
*/
(9.7.05)
//public Part()//created by Java IF no other constructor exists
//{
//numParts = 0;
//}
10
Example: Part (a more sophisticated class)
Part.2a—Test a Java-Created Default Constructor
//Part.java (Part.2a--with Java created default constructor)
/**
Constructor sets initial value of numParts
*/
//public Part (int newParts) //must have the same name as the class
//{
//numParts = numParts; //sets the instance variable to numParts
//}
/**
Accessor methods return the values of variables in a class
Accessor methods are typically called, getVariableName()
<p>
Returns the value of private instance variable numParts
*/
public int getNumParts()
{
return numParts;
}
(9.7.05)
11
Example: Part (a more sophisticated class)
Part.2a—Test a Java-Created Default Constructor
//Part.java (Part.2a--with Java created default constructor)
/**
Mutator methods change the values of variables in a class
Mutator methods are typically called, setVariableName(int aNumber.)
<p>
Changes the value of private instance variable _numParts
*/
public void setNumParts(int newNum)
{
numParts = newNum;
return; //optional
}
/**
Other instance methods change the values of variables in a class
<p>
Increments the value of numParts by 10
*/
public void incrementNumParts()
{
numParts = numParts + 10;
}
(9.7.05)
12
Example: Part.2a (a more sophisticated class)
Test a Java-Created Default Constructor
//Part.java (Part.2a--with Java created default constructor)
/**
Increments the value of numParts by anIncrease
*/
public void incrementNumParts(int anIncrease)
{
numParts = numParts + anIncrease;
}
/**
Overload toString() method of class Object
*/
public String toString()
{
String s = new String(numParts + " parts in this batch" );
return s;
}
(9.7.05)
13
Example: Part (a more sophisticated class)
Part.2a—Test a Java-Created Default Constructor
//Part.java (Part.2a--with Java created default constructor)
public static void main (String[ ] args)
{
//create two instance of Part
//test default constructor
Part testPart = new Part(); //the one line method of creating an instance of Part
System.out.println ("testPart: Has " + testPart.toString() + ".");
//Part myPart; //reference to a Part object
//myPart = new Part(85000); //now give it memory & a value
//Part yourPart = new Part(2000); //the one line method of creating an instance of Part
//System.out.println ("myPart: Has " + myPart.toString() + ".");
//System.out.println ("yourPart: Has " + yourPart.toString() + ".");
//System.out.println ("yourPart Has " + yourPart.toString() + ".")
//yourPart.setNumParts(2500);
//System.out.println ("yourPart has " + yourPart.toString() + ".");
//yourPart.incrementNumParts(1000);
//System.out.println ("Now yourPart has " + yourPart.toString() + ".");
}//end main
} //end of class Part
Output
testPart: Has 25 parts in this batch.
(9.7.05)
14
Example: Part (a more sophisticated class)
Part.2b: Overloaded & Default Constructors & Other Methods
//Part.java (Part.2b--constuctors: default & overloaded)
/**
A second, more complicated Part class.
*/
public class Part
{
/**
instance variables
*/
private int numParts = 25; //good to initialize
/**
Constructors (special class members)
Create an instance (object) of a class
Constructors must have the same name as the class
*/
/**
Default constructor: automatically created by Java
IF no other constructor exists.
Good style to create a default constructor
*/
(9.7.05)
15
Example: Part (a more sophisticated class)
Part.2b: Overloaded & Default Constructors & Other Methods
//Part.java (Part.2b--constuctors: default & overloaded)
public Part ( ) //user-created default constructor no argument
{
numParts = 25; //sets a default instance variable to four for numPart
}
/**
Overloaded constructor sets initial value of numParts
*/
public Part (int newParts) //must have the same name as the class
{
numParts = newParts; //sets the instance variable numParts
}
/**
Accessor methods return the values of variables in a class
Accessor methods are typically called, getVariableName()
<p>
Returns the value of private instance variable numParts
*/
public int getNumParts()
{
return numParts;
}
(9.7.05)
16
Example: Part (a more sophisticated class)
Part.2b: Overloaded & Default Constructors & Other Methods
//Part.java (Part.2b--constuctors: default & overloaded)
/**
Mutator methods change the values of variables in a class
Mutator methods are typically called, setVariableName(int aNumber.)
<p>
Changes the value of private instance variable numParts
*/
public void setNumParts(int newNum)
{
numParts = newNum;
return; //optional
}
/**
Other instance methods change the values of variables in a class
<p>
Increments the value of numParts by 10
*/
public void incrementNumParts()
{
numParts = numParts + 10;
}
(9.7.05)
17
Example: Part (a more sophisticated class)
Part.2b: Overloaded & Default Constructors & Other Methods
//Part.java (Part.2b--constuctors: default & overloaded)
/**
Increments the value of numParts by anIncrease
*/
public void incrementNumParts(int anIncrease)
{
numParts = numParts + anIncrease;
}
/**
Overload toString() method of class Object
*/
public String toString()
{
String s = new String(numParts + " parts in this batch" );
return s;
}
(9.7.05)
18
Example: Part (a more sophisticated class)
Part.2b: Overloaded & Default Constructors & Other Methods
//Part.java (Part.2b--constuctors: default & overloaded)
public static void main (String[ ] args)
{
//create two instance of Part
//test default constructor
//Part testPart = new Part(); //the one line method of creating an instance of Part
//System.out.println ("testPart: Has " + testPart.toString() + ".");
Part myPart; //reference to a Part object, myPart
myPart = new Part(85000); //now give it memory & a value
System.out.println ("myPart: Has " + myPart.toString() + ".");
Part yourPart = new Part(2000); //the one line method of creating an instance of Part
System.out.println ("yourPart: Has " + yourPart.toString() + ".");
yourPart.setNumParts(2500);
System.out.println ("yourPart now has " + yourPart.toString() + ".");
yourPart.incrementNumParts(1000);
System.out.println ("Now yourPart has " + yourPart.toString() + ".");
}//end main
} //end of class Part
Output
myPart: Has 85000 parts in this batch.
yourPart: Has 2000 parts in this batch.
yourPart now has 2500 parts in this batch.
Now yourPart has 3500 parts in this batch.
(9.7.05)
19
Example: Part.2c (multiple Java files)
Part.2c contains two Java source files
Part.java // same file as in 2b
TestApp.java //new
TestApp.java
TestApp tests the functionality of Part class
TestApp has one method, main(…),
Alternative to Example 2b in which the main() contained in Part was
used to test Part
Compiling multiple files in Java
Remove all class files in folder Part.2c
Compile TestApp
Look in folder, Part.2c
You will see a Part.class as well as a TestPart.class
Java compiler is smart enough to see it needs a Part class & looks
for one & compiles it as well
Like a “make” command in Unix
(9.7.05)
20
Example: Part.2c (multiple Java files)
//TestApp.java (Part.2c: multiple Java files)
/**
This is a standard testing program class.
<p>
It can be used with any class or set
of classes.
<p>
Testing Part from with two files: Part.java & TestApp.java
and the main() is in class TestPart
*/
public class TestPart
{
public static void main (String[ ] args)
{
//create two instances of Part with default constructor
Part myPart = new Part( ); //the one line method of creating an instance
System.out.println ("yourPart: " + myPart.toString() + ".");
Part yourPart = new Part(350);
System.out.println ("yourPart: " + yourPart.toString() + ".");
//use accessor methods (required for private instance variables)
System.out.println ("I have " + myPart.getNumParts( ) + " parts.");
System.out.println ("You have " + yourPart.getNumParts( ) + " parts.");
(9.7.05)
21
Example: Part.2c (multiple Java files)
//TestApp.java (Part.2c: multiple Java files)
//use mutator methods to change the instance variables for both objects
myPart.setNumParts(1000);
yourPart.setNumParts(-50);
System.out.println ("I have " + myPart.getNumParts( ) + " parts.");
System.out.println ("You have " + yourPart.getNumParts( ) + " parts.");
//increment the value of private instance variable, numParts in myPart
myPart.incrementNumParts();
yourPart.incrementNumParts(2000);
System.out.println ("I have " + myPart.getNumParts( ) + " parts.");
System.out.println ("You have " + yourPart.getNumParts( ) + " parts.");
Part hisPart; //create a reference (2) line method of construction
hisPart = new Part(85000); //overloaded constructor that initialize instance var.
Part herPart = new Part(2000); // (1) line method of creating an instance of Part
System.out.println ("herPart:" + herPart.toString() + ".");
System.out.println ("hisPart: " + hisPart.toString() + ".");
}//end of main method
} //class TestApp
(9.7.05)
22
Example: Part.2c (multiple Java files)
//TestApp.java (Part.2c: multiple Java files)
Output
yourPart: 25 parts in this batch.
yourPart: 350 parts in this batch.
I have 25 parts.
You have 350 parts.
I have 1000 parts.
You have -50 parts.
I have 1010 parts.
You have 1950 parts.
herPart:2000 parts in this batch.
hisPart: 85000 parts in this batch.
(9.7.05)
23