14b - University of North Carolina at Chapel Hill

Download Report

Transcript 14b - University of North Carolina at Chapel Hill

COMP 14
Introduction to Programming
Adrian Ilie
July 13, 2005
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Today
• Defining classes, using objects
• public, private, static
• Assignment operator and objects
• jGRASP projects, multiple files,
Java archives
2
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Multiple Classes
• So far, we have added methods
to the main class
• Classes extend the concept of
data types: data + operations
• Define new classes and
instantiate objects
3
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Multiple Classes
• Concept: Basketball team
♦
♦
♦
♦
players (objects of class Player)
coach (object of class Coach)
games
…
• Define each class in one file, and
instantiate objects in other
classes or in the main method
4
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Basketball.java
• Create main class: Basketball
• Define Player class
♦ player name
♦ year
♦ + operations
5
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Multiple files
• Good style: 1 class per file
• Store all files in 1 directory
• Create jGRASP project (.gpj)
6
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
BasketballProject.gpj
• Example of project creation
7
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Multiple Objects
• A class has 1 single definition
• We can instantiate multiple objects of
1 class
• Each object keeps its own copy of the
data members
• Objects are accessed using reference
variables
• To perform operations on objects:
referenceVariable.method (parameter1, parameter2, …)
8
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Basketball.java
• Instantiate multiple Player objects
• Call methods of the Player objects
from Basketball.main()
• Demonstrate flow through different
methods with the debugger
9
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Visibility Modifiers
private and public
• Public
♦ For members that should be accessible outside the
class (i.e. from other classes or the main method)
♦ Usually employed with methods
• Private
♦ For members that should not be accessed outside
the class
♦ Usually employed with data members
♦ Also for methods that are only used internally
• Typically, data members are declared as
private, so we need methods to set and get
data values
10
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Player.java
• Make data members private
• Make methods public
• Demonstrate accessibility from
Basketball.main()
11
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Assignment Operator
• Assignment expressions that
involve reference variables do
not copy object data!!
Player player1 = new Player(“Felton”);
Player player2 = new Player();
player2 = player1;
//player2 points to the same obj.
• In order to copy object data, we
need additional methods
public void Player.copy(Player otherPlayer)
12
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Player.java
• Write Player.copy()
13
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Visibility Modifiers
static
• static data members
♦ Data related to a class that does not need
to be defined for each object
public class Player
{
static final int FRESHMAN = 1;
static final int SOPHOMORE = 2;
private int year;
…
}
14
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Visibility Modifiers
static
• static methods
♦ Operations that do not use object-dependent data
public static int factorial(int x)
{
…
}
♦ They can only use static data members!! (besides
local variables)
static BufferedReader keyboard = …
public static void main(…)
{
String str=keyboard.readLine();
…
}
15
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Visibility Modifiers
static
• To access static data members and methods
from outside the class:
double res = Math.pow(2.0, 3.0);
//ClassName.method();
• To access non-static data members and
methods from outside the class:
player1.printInfo();
//referenceVariable.method();
• In general, from inside the class, no need to
write class name or reference variable.
16
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Player.java
• Add static data members and
methods
17
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
The toString Method
• Special method in Java classes
• Produces a String object based on the
current object, suitable for printing
• Mapped to the '+' operator
(concatenation)
• Also called when the object is a
parameter in a print() or println()
method
• There is a default toString method, but
it's better if we write our own
18
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Player.java
• Write Player.toString()
19
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
To do
• Homework 5 (due Wednesday
night)
♦ Don’t wait till Tuesday!!
♦ Homework 6 is going to be rather long
• Read ch. 9
20
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL