0% found this document useful (0 votes)
10 views

Java Generics & Collections-fillable (2024) Complete

The document outlines learning objectives for understanding Java Generics and Collections, focusing on recognizing and utilizing type parameters and generic classes. It includes team roles for a collaborative activity, code examples illustrating the use of classes and methods, and questions to guide students in analyzing the code. Additionally, it discusses the importance of generic types in reducing duplicate code and the efficiency of using Java Collections like ArrayList for array operations.

Uploaded by

garrettfoltyn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Generics & Collections-fillable (2024) Complete

The document outlines learning objectives for understanding Java Generics and Collections, focusing on recognizing and utilizing type parameters and generic classes. It includes team roles for a collaborative activity, code examples illustrating the use of classes and methods, and questions to guide students in analyzing the code. Additionally, it discusses the importance of generic types in reducing duplicate code and the efficiency of using Java Collections like ArrayList for array operations.

Uploaded by

garrettfoltyn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Generics & Collections

Content Learning Objectives


After completing this activity, students should be able to:
• Recognize methods and classes that perform similar operations with different types.
• Recognize, describe, and use type parameters and generic classes in Java.
• Describe the key differences between Java arrays and ArrayLists.

Before you start, complete the form below to assign a role to each member.
If you have 3 people, combine Speaker & Reflector.

Team Roles Team Member


Recorder: records all answers & questions,
Garrett
and provides copies to the team & facilitator.
Speaker: talks to the facilitator and other teams.

Manager: keeps track of time and makes sure


Aidan
everyone contributes appropriately.
Reflector: considers how the team could
Armaan
work and learn more effectively.

Reminders:
1. Note the time whenever your team starts a new section or question.
2. Write legibly & neatly so that everyone can read & understand your responses.

start
(15 min) A. Lists of Objects time:
Our procress got ereased

Large software systems often need to do similar things with different classes. For example, a
learning management system like Canvas might include:
● classes for School, Course, Room, and Teacher
● lists of different schools, courses, rooms, and teachers
● methods to add, change, remove, or search for specific entries in these lists.
However, we would like to avoid writing similar methods for many different classes. This activity
will explore some techniques that can help.
2

Java Generics & Collections


// Partial Code Listing I for Java Generics & Collections
public class School {
// each school has a name and lists of courses, rooms, & teachers
private String schoolName;
private int numCourses = 0, numRooms = 0, numTeachers = 0;
private Course[] courseList = new Course[100];
private Room[] roomList = new Room[100];
private Teacher[] teacherList = new Teacher[100];

public School (String name) { setName(name); }

public String getName() { ... }


public void setName(String s) { ... }

public void addCourse(Course c) { ... }


public void deleteCourse(Course c) { ... }
public Course getCourse(int i) { return this.courseList[i]; }
public int numCourses () { return this.numCourses; }

public void addRoom (Room r) { ... }


public void deleteRoom (Room r) { ... }
public Room getRoom (int i) { return this.rorrmList[i]; }
public int numRooms () { return this.numRooms; }

public void addTeacher (Teacher t) { ... }


public void deleteTeacher (Teacher t) { ... }
public Teacher getTeacher (int i) { return this.teacherList[i]; }
public int numTeachers () { return this.numTeachers; }
...
}
1. Study Code Listing I and list the instance variable(s) in class School that contain:
a. Strings

b. integers
c. arrays
3

Java Generics & Collections

2. List the instance methods in class School that:

a. involve or use class Room?

b. involve or use class Teacher?

c. involve or use class Course?

d. return the value of an integer instance


variable?

e. return an object from an array


instance variable?

3. Decide which class (Course, Room, Teacher) would likely have these instance
variables:

a. location, numberSeats

b. name, department

c. name, number of students, teacher, room

4. For each set below, describe how the 3 methods in the set might be similar:

a. addCourse()
addRoom()
addTeacher()

b. deleteCourse()
deleteRoom()
deleteTeacher()

5. What is the key difference among the 3 methods in each set for question 4? In other
words, how is deleteCourse() different from deleteRoom() and
deleteTeacher()?
4

Java Generics & Collections


6. In large software systems, we often have sets of similar methods. For example, we might
include methods to find out if a School has a given Course, Room, or Teacher.
boolean hasCourse (Course c) { ... }
boolean hasRoom (Room r) { ... }
boolean hasTeacher(Teacher t) { ... }

How might the 3 methods in this set be similar?

7. What is the key difference among the 3 methods in the previous question?

8. If we create class Vehicle and give School a Vehicle list, what instance variables,
(aka fields) and methods might we add to class School?
5

Java Generics & Collections

(10 min) B. Classes for Lists start


Our procress got ereased
time:

Professional developers often rewrite code to make it easier to understand and modify -
this process is called refactoring. Code Listing II is a refactored version of Code Listing I.
// Code Listing II for Java Generics & Collections
public class School {

private String name;


private CourseList cList = new CourseList();
private RoomList rList = new RoomList();
private TeacherList tLIst = new TeacherList();

public School(String name) { setName(name); }

public String getName() { ... }


public void setName(String s) { ... }

public CourseList getCourses() { return this.cList; }


public RoomList getRooms() { return this.rList; }
public TeacherList getTeachers() { return this.tList; }

...
}
public class CourseList {
private int num = 0;
private Course [] list = new Course[100];

public void add(Course c) { ... }


public void del(Course c) { ... }
public Course get(int i) { return this.list[i]; }
public int num() { return this.num; }
...
}
6

Java Generics & Collections


9. In Code Listing II, study class School and answer the questions below.

a. How many fields have been removed?

b. How many fields have been added?

c. How many methods have been removed?

d. How many methods have been added?

10. In Code Listing II, study class CourseList and answer the questions below.

a. How many fields are defined?

b. How many methods are defined?

11. In complete sentences, describe the changes from Listing I to II.

12. Code Listing II shows CourseList, but not RoomList or TeacherList. In


complete sentences, describe what your team thinks the major differences are between
the three List classes.
7

Java Generics & Collections

(10 min) C. Generic Classes start


Our procress got ereased
time:

// Code Listing III for Java Generics & Collections


public class ListOf<T> {
private int num = 0;
// cast Object array to T array, since Java can’t do "new T[100]"
private T[] list = (T[]) new Object[100];

public void add(T c) { ... }


public void del(T c) { ... }
public int num() { return this.num; }
public T get(int i) { return this.list[i]; }
...
}

public class School {


private String name;
private ListOf<Course> cList = new ListOf<Course> ();
private ListOf<Room> rList = new ListOf<Room> ();
private ListOf<Teacher> tList = new ListOf<Teacher>();

public School (String name) { setName(name); }

public String getName() { ... }


public void setName(String s) { ... }
public ListOf<Course> getCourses () { return this.cList; }
public ListOf<Room> getRooms () { return this.rList; }
public ListOf<Teacher> getTeachers() { return this.tList; }

...
}
8

Java Generics & Collections


13. In Code Listing III, study class ListOf and answer the questions below.

a. In the first line, what symbol is between angle brackets (<>)?

b. How many other times does this symbol appear in ListOf?

c. What is the return type for method num()?

d. What is the return type for method get()?

e. What is the input parameter type for get()?

f. What is the input parameter type for add() and del()?

g. Is the symbol in angle brackets used like


a data type, a field, a method, or a variable?

14. In Code Listing III, study class School and answer the questions below.

a. How many times do angle brackets (<>) appear in School?

b. What 3 different symbols appear between angle brackets?

c. Do these symbols represent classes, fields, or methods?

d. Which instance field refers to a list of courses?

15. List 2 or 3 main differences between class CourseList (in Code Listing II)
and class ListOf (in Code Listing III).
9

Java Generics & Collections


16. Methods often have value parameters to specify values used in the method. Classes can
have type parameters to specify types used in the class.
A class with type parameters is called a generic class or generic type because it
provides general capabilities that can be used with any type given as a type parameter.
(Most generic classes have one type parameter, but some have 2 or more.)

a. What is the type parameter used in class ListOf?

b. What types are passed from School to ListOf?

17. In complete sentences, explain how generic types can reduce duplicate code.
THey can be used to reduce duplicate code by having one piece of generic code that can be
intailized with multiple data types allowing it to be used for a varitey of things, saving us from
having to write the same code multiple times with the only difference being the data type used.

(5 min) D. Java Collections start


125
time:

18. Arrays are a very useful part of nearly every programming language. As we have seen,
we often need to add, delete, or search for elements in the array, or resize the array.
Explain why it is inefficient to write (and rewrite) our own code to perform these operations.
It's a waste of time to repeatily write the same code over and over when we can create a
generitc function that we can reuse over and over for lots of different purposes.

Thus, many languages provide libraries to simplify array operations. For example, the Java
API includes class ArrayList<T>, a generic class which stores data in an array and
provides a variety of useful methods
10

Java Generics & Collections


(https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html).
The code below shows how we could define a set of ArrayLists in School.

public class School {


...
private ArrayList<Course> cList = new ArrayList<Course> ();
private ArrayList<Room> rList = new ArrayList<Room> ();
private ArrayList<Teacher> tList = new ArrayList<Teacher>();
...
} // end class School

19. Look at the ArrayList Java API documentation and decide which method would be the best
way to:

a. find the number of elements in the list size()

b. find the index of an element in the list indexOf(Object o)

c. add an element to the end of the list add(E e)

d. add an element at a specific position within the list add(int index, E element)

e. remove all elements from the list clear()

You might also like