COMP 14
Introduction to Programming
Adrian Ilie
July 18, 2005
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Homework 5
• Algorithm
• How to solve the problem without
objects
♦ not totally true, Strings are objects too
• How to solve the problem with
objects
2
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Algorithm
•
•
•
•
•
•
•
•
•
•
3
Create teams
Create semifinal games
Output initial message
Get score of semifinal 1
Determine finalist 1
Get score of semifinal 2
Determine finalist 2
Create final game
Get score of final
Determine winner
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Without objects
String UNC=“UNC”;
String Duke=“Duke”;
String semi1=UNC + “ Vs. ” + Duke;
String finalist1;
…
if(score1 > score2)
finalist1=UNC;
else
finalist1=Duke;
4
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Add method
String UNC=“UNC”;
String Duke=“Duke”;
String semi1=UNC + “ Vs. ” + Duke;
String finalist1;
…
finalist1=gameWinner(score1, score2, UNC,
Duke);
5
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
With objects
Team UNC=new Team(“UNC”);
Team Duke=new Team(“Duke”);
Game semi1=new Game(UNC, Duke);
Team finalist1;
…
finalist1=semi1.gameWinner(score1, score2);
6
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
What’s going on?
String name=“UNC”
Team UNC
Team Duke
Team objects
String name=“Duke”
Game semi1
Team finalist1
Reference variables
7
Adrian Ilie
Team team1
Team team2
Game object
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
What is different?
• We declare data inside objects, not directly in the
main() method
♦ The Strings become data members of the Team objects
• We no longer need Strings for the games
♦ If we don’t use objects, we duplicate the data.
♦ The String for the game can be generated using the ones stored
in the Teams.
• The method gameWinner is a method associated
to a particular game object
♦ No need to pass the game as a parameter.
♦ No need to pass the teams as parameters
• Why does the code look almost the same?
♦ Because Strings and Teams and Games are ALL classes/objects
♦ Our classes are simply wrappers of Strings
8
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Structure of a Team object
• Data
♦ String name
• Methods
♦ Constructors
♦ toString()
9
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Structure of a Game object
• Data
♦ Pointer to team1 (Team team1)
♦ Pointer to team2 (Team team2)
• Methods
♦ Constructors
♦ toString()
♦ gameWinner()
receives as parameter the score, and returns the
pointer to the winner team.
10
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Classes
• Objects are collections of data and
operations (methods)
• We need to define the
pattern/footprint of objects
classes!!!
11
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Writing classes
public class ClassName
{
//Data members
private int a;
…
//Constructor(s)
public ClassName
{
//Code of the method
}
//other methods
public return_type method1(type param1, type param2, …)
{
//Code of the method
}
}
12
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Writing the main()
public class MainClassName
{
//Data members
Static BufferedReader keyboard=…;
//main method
public static void main(…)
{
//Code of the main method
}
//other methods
public static return_type method1(type param1, …)
{
//Code of the method
}
}
13
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
This is wrong!!
public class ClassName
{
//Data members
private int a;
…
No instructions directly in the body of a class!!!
They have to be inside a method
//Constructor(s)
…
Exception:
initialization of a static variable (e.g. keyboard)
OtherClass obj=new OtherClass();
…
//other methods
public return_type method1(type param1, …)
{
//Code of the method
}
}
14
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review
0
1
2
3
Arrays
• Declaration
can use variables and
expressions as initial values
int[] counts;
• Instantiation
counts = new int[50];
• Initialization / Access
for (int i=0; i<counts.length; i++) {
counts[i] = 0;
}
• Initializer List
♦ declaration, instantiation, and initialization
double[] grades = {98.7, 72.4, 87.5};
int[] numbers = {num, num+1, num+2, num+3};
15
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Arrays and Assignment
int[] counter = new int[5];
temp
counter
int[] temp;
0
1
temp = counter;
doesn't make a copy of
the array!
temp == counter is true since
the reference variables contain
the same address
16
Adrian Ilie
2
3
4
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Copying Arrays
counter
int[] counter = {1, 2, 3, 4, 5};
int[] temp = new int[counter.length];
temp
0
1
2
3
4
17
1
2
3
2
3
2
3
4
4
4
5
5
for (int i=0; i<counter.length; i++) {
temp[i] = counter[i];
}
Adrian Ilie
1
0
1
i
1
4
2
5
0
3
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
References and Assignment
counter
temp = counter;
temp
0
1
2
3
4
1
2
3
4
0
1
2
3
4
1
2
3
4
5
5
Remember that arrays use reference variables just like objects.
18
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
References and null
temp = null;
null is a reserved word that means "empty/nothing”
temp = null;
counter
temp
0
1
2
3
4
1
2
3
4
0
1
2
3
4
1
2
3
4
5
5
Remember that arrays use reference variables just like objects.
19
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Arrays as Parameters
• Entire array can be passed as a
parameter
♦ method can change elements of the array
permanently
♦ since we're passing a reference
• Elements of an array can be
passed as parameters, too
♦ normal rules apply…
20
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
public class Tester
{
public static void swap (int[] scores, int x, int y)
{
scores[2]: 20
30
int temp = scores[x];
scores[3]: 30
20
scores[x] = scores[y];
temp
: 20
scores[y] = temp;
}
public static void main (String[] args)
{
int[] grades = new int[4];
for (int i=0; i<grades.length; i++) {
grades[i] = i*10;
grades[2]: 30
20
}
grades[3]: 20
30
swap (grades, 2, 3);
}
}
21
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Arrays of Objects
• Can use arrays to manipulate objects
• Create array of objects
classname[] array = new classname[size];
• Must instantiate each object in array
for(int j=0; j <array.length; j++) {
array[j] = new classname();
}
22
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Example
• 16 students
• Each student:
String name
int age
String major
int year
• Create arrays of all the data
• Problem: print the names according to
increasing age
• Analyze: without objects or with objects? Why?
23
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
To do
• Homework 6
♦ Start with requirements 1 and 4.
• Objects/classes. Exercise:
♦ Create classes House and Roommate.
♦ House will have an array of Roommates.
♦ Create methods to set number of Roommates, etc
(whatever you come up with)
♦ Test the classes by writing calls to the methods in
the main() method
♦ Come to discuss problems
24
Adrian Ilie
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
© Copyright 2026 Paperzz