250 likes | 399 Views
Object Arrays. Why an array of objects?. Similar to an array of integers we might want a list of objects For example A school has a number of courses If each course is an object The school can have an array of courses. Review on arrays of primitive types.
E N D
Why an array of objects? • Similar to an array of integers • we might want a list of objects • For example • A school has a number of courses • If each course is an object • The school can have an array of courses
Review on arrays of primitive types • How to declare (allocate memory) a double array of 10 elements?
Review on arrays of primitive types • How to declare (allocate memory) a double array of 10 elements? • double[] rainfall = new double[10];
Review on arrays of primitive types • How to declare (allocate memory) a double array of 10 elements? • double[] rainfall = new double[10]; • How about a 10 x 20 2D int array?
Review on arrays of primitive types • How to declare (allocate memory) a double array of 10 elements? • double[] rainfall = new double[10]; • How about a 10 x 20 2D int array? • int[][] scores = new int[10][20];
Review on arrays of primitive types • Given the rainfall 1D array, how to find # of elements?
Review on arrays of primitive types • Given the rainfall 1D array, how to find # of elements? • rainfall.length • Given the scores 2D array, how to find # of rows and columns?
Review on arrays of primitive types • Given the rainfall 1D array, how to find # of elements? • rainfall.length • Given the scores 2D array, how to find # of rows and columns? • scores.length • scores[0].length
Array of objects • Consider the Person class • How to declare an array of 10 Person objects?
Array of objects • Consider the Person class • How to declare an array of 10 Person objects? • Person[] team = new Person[10]; • This is not complete, because ? (a common mistake)
Array of objects • Consider the Person class • How to declare an array of 10 Person objects? • Person[] team = new Person[10]; • This is not complete, because ? (a common mistake) • Merely allocate space for the array • Think of it as placeholders for objects • We also need to put objects into the array • Similar to populating an int array with desirable values • What to do?
Array of objects • Consider the Person class • How to declare an array of 10 Person objects? • Person[] team = new Person[10]; • This is not complete, because ? (a common mistake) • Merely allocate space for the array • Think of it as placeholders for objects • We also need to put objects into the array • Similar to populating an int array with desirable values • What to do? • team[0] = new Person(“Mary”, 10); • team[0] = mary; // mary was created elsewhere
How about a 2D array of objects? Consider we have 20 basketball teams in a tournament, each has 10 players
How about a 2D array of objects? • Consider we have 20 basketball teams in a tournament, each has 10 players • Person[][] basketballTeams = new Person[20][10]; • This is not complete, because ?
How about a 2D array of objects? • Consider we have 20 basketball teams in a tournament, each has 10 players • Person[][] basketballTeams = new Person[20][10]; • This is not complete, because ? • basketballTeams [0][0] = new Person(“Mary”, 10); • GivenbasketballTeams, how to find the number of teams and # of players on each team?
How about a 2D array of objects? • Consider we have 20 basketball teams in a tournament, each has 10 players • Person[][] basketballTeams = new Person[20][10]; • This is not complete, because ? • basketballTeams [0][0] = new Person(“Mary”, 10); • GivenbasketballTeams, how to find the number of teams and # of players on each team? • basketballTeams.length • basketballTeams[0].length
Exercise 1 • Person class • Attributes: name, height (in inches) • Constructors: • Person(name, heightInches) • Person(name, heightFeet, heighInches) • Methods: • getName(), getHeightInches(), getHeightFeetInches() • BasketballTest class • Main method: • 2D array with 3 teams of 5 players • print the tallest player: name and height in feet and inches • print the shortest player: name and height in feet and inches • print the average height in feet and inches, and the player closest to the average height
Design • Consider we want to also know their team name, city, and mascot • How would you design the program? • Classes:
Design • Consider we want to also know their team name, city, and mascot • How would you design the program? • Classes: • Person: • Team: • Attributes? • Constructors? • Methods? • BasketballTest2
Design • Consider we want to also know their team name, city, and mascot • How would you design the program? • Classes: • Person: • Team: • Attributes: name, city, mascot, roster • Constructor: Team(name, city, mascot, roster) • Why not Team(name, city, mascot, name1, height1, …)? • Methods: getName, getCity, getMascot, getMember(index) • BasketballTest2
Exercise 2 • Consider we want to also know their team name, city, and mascot • How would you design the program? • Classes: • Person: • Team: • Attributes: name, city, mascot, roster • Constructor: Team(name, city, mascot, roster) • Methods: getName, getCity, getMascot, getMember(index) • BasketballTest2 • Create an array of 3 teams, each has 5 members • Print the tallest, shortest, closest to average height member and their team name, city, and mascot
Design • Consider • Basketball players have jersey numbers • Programming contest participants have user names • How would you design the program to allow different types of teams? • Classes?
Design • Consider • Basketball players have jersey numbers • Programming contest participants have user names • How would you design the program to allow different types of teams? • Classes: • Person (do we need to change the Person class?) • BasketballPlayer (subclass of Person with jersey number) • Programmer (subclass of Person with user name) • Team (do we need to change the Team class?)
Exercise 3 • Classes • Person • BasketballPlayer (subclass of Person with jersey number) • Constructor: BasketballPlayer(name, height, jerseyNumber) • Programmer (subclass of Person with user name) • Constructor: Programmer(name, height, username) • Team • BasketballProgrammerTest • Create 3 basketball teams, 5 players each • Create 3 programming teams, 3 programmers each • Find the shortest basketball player and the tallest programmer • print their names, height (feet and inches), jersey/username, and team name