comp 14 introduction to programming miguel a. otaduy june 3, 2004

24
COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Post on 22-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

COMP 14Introduction to Programming

Miguel A. Otaduy

June 3, 2004

Page 2: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

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

Page 3: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Algorithm• 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

Page 4: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Without objects

String UNC=“UNC”;String Duke=“Duke”;String semi1=UNC + “ Vs. ” + Duke;String finalist1;…if(score1 > score2)

finalist1=UNC;else

finalist1=Duke;

Page 5: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Add method

String UNC=“UNC”;String Duke=“Duke”;String semi1=UNC + “ Vs. ” + Duke;String finalist1;…finalist1=gameWinner(semi1,

score1, score2, UNC, Duke);

Page 6: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

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);

Page 7: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

What’s going on?

String name=“UNC”

String name=“Duke”

Team objects

Team team1Team team2

Game object

Team UNCTeam Duke

Game semi1

Team finalist1

Reference variables

Page 8: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

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

Page 9: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Structure of a Team object• Data

– String name

• Methods– Constructors– toString()

Page 10: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

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.

Page 11: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Classes

• Objects are collections of data and operations (methods)

• We need to define the pattern/footprint of objects classes!!!

Page 12: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Writing classespublic class ClassName{

//Data membersprivate int a;…

//Constructor(s)public ClassName{

//Code of the method}

//other methodspublic return_type method1(type param1, type param2, …){

//Code of the method}

}

Page 13: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Writing the main()public class MainClassName{

//Data membersStatic BufferedReader keyboard=…;

//main methodpublic static void main(…){

//Code of the main method}

//other methodspublic static return_type method1(type param1, …){

//Code of the method}

}

Page 14: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

This is wrong!!public class ClassName{

//Data membersprivate int a;…

//Constructor(s)…

OtherClass obj=new OtherClass();

//other methodspublic return_type method1(type param1, …){

//Code of the method}

}

No instructions directly in the body of a class!!!They have to be inside a method

Exception:initialization of a static variable (e.g. keyboard)

Page 15: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

ReviewArrays• Declaration

int[] counts;

• Instantiationcounts = new int[50];

• Initialization / Accessfor (int i=0; i<counts.length; i++) {counts[i] = 0;

}

• Initializer List– declaration, instantiation, and initializationdouble[] grades = {98.7, 72.4, 87.5};int[] numbers = {num, num+1, num+2, num+3};

can use variables andexpressions as initial values

0 1 2 3

Page 16: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Arrays and Assignment

counter

01

23

4

int[] counter = new int[5];

int[] temp;

temp

temp = counter;

doesn't make a copy of the array!

temp == counter is true sincethe reference variables containthe same address

Page 17: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Copying Arrayscounter

01

23

4

int[] counter = {1, 2, 3, 4, 5};

1

23

4

5

int[] temp = new int[counter.length];

01

23

4

temp

for (int i=0; i<counter.length; i++) {temp[i] = counter[i];

}

1

23

4

5

i

012345

Page 18: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

References and Assignment

counter

01

23

4

1

23

4

5

01

23

4

temp

temp = counter;

1

23

4

5

Remember that arrays use reference variables just like objects.

Page 19: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

References and nullcounter

01

23

4

1

23

4

5

01

23

4

temp

temp = null;

1

23

4

5

Remember that arrays use reference variables just like objects.

null is a reserved wordthat means "empty/nothing”

Page 20: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

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…

Page 21: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

public class Tester{ public static void swap (int[] scores, int x, int y) {

int temp = scores[x];scores[x] = scores[y];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;

}

swap (grades, 2, 3); }

}

grades[2]: 20grades[3]: 30grades[2]: 30grades[3]: 20

scores[2]: 20scores[3]: 30temp : 20

scores[2]: 30scores[3]: 30temp : 20

scores[2]: 30scores[3]: 20temp : 20

Page 22: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Arrays of Objects

• Can use arrays to manipulate objects• Create array of objects

• Must instantiate each object in array

classname[] array = new classname[size];

for(int j=0; j <array.length; j++) {array[j] = new classname();

}

Page 23: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

Example• 16 students• Each student:

String nameint ageString majorint year

• Create arrays of all the data

• Problem: print the names according toincreasing age

• Analyze: without objects or with objects? Why?

Page 24: COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004

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