1 / 25

Object Arrays

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.

galya
Télécharger la présentation

Object Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Arrays

  2. 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

  3. Review on arrays of primitive types • How to declare (allocate memory) a double array of 10 elements?

  4. Review on arrays of primitive types • How to declare (allocate memory) a double array of 10 elements? • double[] rainfall = new double[10];

  5. 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?

  6. 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];

  7. Review on arrays of primitive types • Given the rainfall 1D array, how to find # of elements?

  8. 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?

  9. 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

  10. Array of objects • Consider the Person class • How to declare an array of 10 Person objects?

  11. 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)

  12. 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?

  13. 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

  14. How about a 2D array of objects? Consider we have 20 basketball teams in a tournament, each has 10 players

  15. 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 ?

  16. 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?

  17. 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

  18. 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

  19. Design • Consider we want to also know their team name, city, and mascot • How would you design the program? • Classes:

  20. 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

  21. 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

  22. 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

  23. 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?

  24. 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?)

  25. 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

More Related