Title: ISYE 7210--Simulation of Real-Time Systems Fall 2005 Classes
1ISYE 7210--Simulation of Real-Time Systems Fall
2005 Classes ObjectsNote See zip file on
class Web page for examples used in
classwww.chmsr.gatech.edu/ISyE7210/
www.horstmann.com/corejava.html Q 5.day.5
9/20/2005 913 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_at_chmsr.gatech.edu
2StringOverload API
- Class Name
- StringOverload()
- Variables
- private long quantity
- Constructor
- public class StringOverload()
- 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
3Variable 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 with an access modifier
4Basic Java Syntax Classes Plus our Coding
Conventions (contd)(Example 1 Part.java)
- A Java class has two components
- A class declaration
- 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 - A public scope means any other part of the Java
program can see the class
5Overview 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
- Class declaration
- Class scope
- 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 - Class name
- Class defaults nonpublic, nonabstract, nonfinal
subclass of Object that implements no interfaces.
6Overview Classes (contd)
- A Java class template (contd)
- 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
7Class Definition Summary (contd)
- A Java class template (contd)
- 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!!!!! - If the user creates one or more constructors,
then Java does NOT create a default constructor
the user may create a default constructor
8Class Definition Summary (contd)
- A Java class template (contd)
- 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
9Class Definition Summary (contd)
- A Java class template (contd)
- class body
- c. 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
10Example Part (a more sophisticated class)
Part.2aTest 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
11Example Part (a more sophisticated class)
Part.2aTest 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() - ltpgt
- Returns the value of private instance
variable numParts - /
- public int getNumParts()
-
- return numParts
12Example Part (a more sophisticated class)
Part.2aTest 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.) - ltpgt
- 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 - ltpgt
- Increments the value of numParts by 10
- /
- public void incrementNumParts()
13Example 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
-
-
-
14Example Part (a more sophisticated class)
Part.2aTest 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)
15Example 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 - /
16Example 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() - ltpgt
- Returns the value of private instance
variable numParts
17Example 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.) - ltpgt
- 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 - ltpgt
- Increments the value of numParts by 10
- /
18Example 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
-
19Example 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)
20Example 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
21Example Part.2c (multiple Java files)
- //TestApp.java (Part.2c multiple Java files)
- /
- This is a standard testing program class.
- ltpgt
- It can be used with any class or set
- of classes.
- ltpgt
- 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
22Example 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() ".")
23Example 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.
- herPart2000 parts in this batch.
- hisPart 85000 parts in this batch.