Unformatted text preview:

COMP 14 Introduction to ProgrammingReview Homework 5AlgorithmWithout objectsAdd methodWith objectsWhat’s going on?What is different?Structure of a Team objectStructure of a Game objectClassesWriting classesWriting the main()This is wrong!!Review ArraysArrays and AssignmentCopying ArraysReferences and AssignmentReferences and nullArrays as ParametersSlide 21Arrays of ObjectsExampleTo doThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14Introduction to ProgrammingAdrian IlieJuly 18, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2Review Homework 5•Algorithm•How to solve the problem without objects♦not totally true, Strings are objects too•How to solve the problem with objectsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Algorithm•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 winnerThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie4Without objectsString UNC=“UNC”;String Duke=“Duke”;String semi1=UNC + “ Vs. ” + Duke;String finalist1;…if(score1 > score2)finalist1=UNC;elsefinalist1=Duke;The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5Add methodString UNC=“UNC”;String Duke=“Duke”;String semi1=UNC + “ Vs. ” + Duke;String finalist1;…finalist1=gameWinner(score1, score2, UNC, Duke);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie6With objectsTeam UNC=new Team(“UNC”);Team Duke=new Team(“Duke”);Game semi1=new Game(UNC, Duke);Team finalist1;…finalist1=semi1.gameWinner(score1, score2);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7What’s going on?String name=“UNC”String name=“Duke”Team objectsTeam team1Team team2Game objectTeam UNCTeam DukeGame semi1Team finalist1Reference variablesThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie8What 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 StringsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie9Structure of a Team object•Data♦String name•Methods♦Constructors♦toString()The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie10Structure 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.The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11Classes•Objects are collections of data and operations (methods)•We need to define the pattern/footprint of objects  classes!!!The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12Writing 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}}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13Writing 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}}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie14This 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 methodException:initialization of a static variable (e.g. keyboard)The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie15ReviewArrays•Declarationint[] 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 values0 12 3The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16Arrays and Assignmentcounter01234int[] counter = new int[5];int[] temp;temptemp = counter; doesn't make a copy of the array!temp == counter is true sincethe reference variables containthe same addressThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie17Copying Arrayscounter01234int[] counter = {1, 2, 3, 4, 5};12345int[] temp = new int[counter.length];01234tempfor (int i=0; i<counter.length; i++) {temp[i] = counter[i];}12345i012345The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18References and Assignmentcounter012341234501234temptemp = counter;12345Remember that arrays use reference variables just like objects.The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie19References and nulltemp = null;null is a reserved word that means "empty/nothing”counter012341234501234temptemp = null;12345Remember that arrays use reference variables just like objects.The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie20Arrays 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…The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie21public 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]: 20scores[2]: 20scores[3]: 30temp : 20scores[2]: 30scores[3]: 30temp : 20scores[2]: 30scores[3]: 20temp : 20The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie22Arrays of Objects•Can use arrays


View Full Document

UNC-Chapel Hill COMP 14 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?