+ All Categories
Home > Documents > Object Arrays

Object Arrays

Date post: 22-Feb-2016
Category:
Upload: galya
View: 48 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
25
Object Arrays
Transcript
Page 1: Object Arrays

Object Arrays

Page 2: 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

Page 3: Object Arrays

Review on arrays of primitive types

How to declare (allocate memory) a double array of 10 elements?

Page 4: Object Arrays

Review on arrays of primitive types

How to declare (allocate memory) a double array of 10 elements? double[] rainfall = new double[10];

Page 5: Object Arrays

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?

Page 6: Object Arrays

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

Page 7: Object Arrays

Review on arrays of primitive types

Given the rainfall 1D array, how to find # of elements?

Page 8: Object Arrays

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?

Page 9: Object Arrays

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

Page 10: Object Arrays

Array of objects

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

Page 11: Object Arrays

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)

Page 12: Object Arrays

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 valuesWhat to do?

Page 13: Object Arrays

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 valuesWhat to do?

team[0] = new Person(“Mary”, 10); team[0] = mary; // mary was created elsewhere

Page 14: Object Arrays

How about a 2D array of objects?

Consider we have 20 basketball teams in a tournament, each has 10 players

Page 15: Object Arrays

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 ?

Page 16: Object Arrays

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?

Page 17: Object Arrays

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

Page 18: Object Arrays

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

Page 19: Object Arrays

Design

Consider we want to also know their team name, city, and mascot

How would you design the program? Classes:

Page 20: Object Arrays

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

Page 21: Object Arrays

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

Page 22: Object Arrays

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

Page 23: Object Arrays

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?

Page 24: Object Arrays

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

Page 25: Object Arrays

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


Recommended