Java Generics & Collections-fillable (2024) Complete
Java Generics & Collections-fillable (2024) Complete
Before you start, complete the form below to assign a role to each member.
If you have 3 people, combine Speaker & Reflector.
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
b. integers
c. arrays
3
3. Decide which class (Course, Room, Teacher) would likely have these instance
variables:
a. location, numberSeats
b. name, department
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
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
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 {
...
}
public class CourseList {
private int num = 0;
private Course [] list = new Course[100];
10. In Code Listing II, study class CourseList and answer the questions below.
...
}
8
14. In Code Listing III, study class School and answer the questions below.
15. List 2 or 3 main differences between class CourseList (in Code Listing II)
and class ListOf (in Code Listing III).
9
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.
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
19. Look at the ArrayList Java API documentation and decide which method would be the best
way to:
d. add an element at a specific position within the list add(int index, E element)