Understanding Arrays of Objects and Primitive Types

Slide Note
Embed
Share

Arrays of objects provide a way to store and manage multiple instances of complex data types, similar to arrays of primitive types. This content explores the concept of arrays of objects, how to declare and allocate memory for them, and how to work with arrays of primitive types like double and int. It also touches on important topics like finding the number of elements in an array and working with 2D arrays. Additionally, it explains how to declare an array of objects using a class like Person.


Uploaded on Oct 01, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

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); Given basketballTeams, 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); Given basketballTeams, 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

Related