This lesson is being piloted (Beta version)

Data Structures

Overview

Teaching: 60 min
Exercises: 60 min
Questions
  • How do we use arrays, ArrayLists and HashaMaps to represent collections in Java?

Objectives
  • Build on the example Rectangle class with arrays, ArrayLists and HashMaps.

Pi j3.4 data-structures from mcollison

Array handling

Write a method called emailAddresses that takes two array arguments called first and last and an String argument called domain. The method should be declared in a class in a class called EmailFormatter.java.

Implement the method to use inputs from the corresponding arguments first and last to return a list of suggested email addresses that follow the convention of containing the first name, followed by a full stop and then the last name and finally the domain.

ArrayList handling

Write a method called fiveASideSelector that takes an ArrayList argument containing names of players for 5-a-side football. The method should be declared in a class saved as TeamSelection.java. The method should split the names into teams for 5-a-side football, each team should be represented by a list of names and the method should return a list of teams (a list of lists). If there are less than 20 names, make sure that no team has any subs and make the teams as equal as possible. If there are more than 20 names create as many full teams as possible and allow for 1 sub per team. The names that are adjacent in the list should not be assigned to the same team.

ArrayList inserts

Imagine you are designing a program to help organise a queue of people arriving and leaving a dental clinic waiting room. People should be called up to see dentist in the order that they arrive. Write a method called arrival that takes an argument containing the new arrival’s name and adds their name to the queue (a list of names). Also write a function called next patient that takes an integer argument called position. This function should return the name of the person in the given position in the queue or return the name of the person that is next in the queue if no argument is provided for position. You should make sure that names of people that have been returned by the next patient function are removed from the queue. The method should be declared in a class called DentistWaitingRoom.java.

Create array of objects for the project

  1. Initialise a static ArrayList of shapes in a class called GameLogic. Declare four Wall objects that form the shape of a square when ‘painted’ and add them to the list.
  2. Create two a Paddle objects that are placed on each side and create a Ball object that is placed in the middle. Add these Shape subclasses to the list in GameLogic.
  3. Update the paint method to iterate over all objects in GameLogic.

Key Points

  • Object-oriented programming is a programming paradigm that uses abstraction (in the form of classes and objects) to create models based on the real world environment