Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the.

Similar presentations


Presentation on theme: "CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the."— Presentation transcript:

1 CS 2430 Day 14

2 Agenda for today The Bag class

3 No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the DateList class to store any other kind of Object, such as String !

4 A StringList class? Should we make a new StringList class specifically for String s? No!

5 General-purpose Bag We can convert DateList to a Bag class that can hold any kind of Object It will have an Object array The rest is pretty much the same

6 public class GrowableDateList { public static final int GROWBY = 10; private Date[] dates; private int size; public GrowableDateList() {... } public GrowableDateList(int inSize) {... } public int size() {... } public boolean add(Date addDate) {... } private int find(Date findDate) {... } public boolean remove(Date removeDate) {... } public boolean contains(Date findDate) {... } public Date itemAt(int index) {... } private void grow() {... }... }

7 public class GrowableDateList { public static final int GROWBY = 10; private Date[] dates; private int size; public GrowableDateList() {... } public GrowableDateList(int inSize) {... } public int size() {... } public boolean add(Date addDate) {... } private int find(Date findDate) {... } public boolean remove(Date removeDate) {... } public boolean contains(Date findDate) {... } public Date itemAt(int index) {... } private void grow() {... }... }

8 public class Bag { public static final int GROWBY = 10; private Object[] items; private int size; public Bag() {... } public Bag(int inSize) {... } public int size() {... } public boolean add(Object obj) {... } private int find(Object obj) {... } public boolean remove(Object obj) {... } public boolean contains(Object obj) {... } public Object itemAt(int index) {... } private void grow() {... }... }

9 Implementing Bag Methods Simple! Replace references to Date with Object Works the same as before! Now it can hold any kind of Object, e.g., Card, String, Point2D, etc.

10 public class Bag {... public Object locate(Object obj) {... } @Override public boolean equals(Object obj) {... } public int removeAll(Object obj) {... } } New methods for Bag

11 public class Bag {... public Object locate(Object obj) { int index = find(obj); if (index < 0) // what to do? return null; return items[index]; }... } Implementing locate()

12 Why do we need a locate() method?

13 public class Student { private String id, name; private float gradePointAverage; public Student(String inId, String inName, float inGPA) {... } public Student(String inId) {...} public float getGPA() { return gradePointAverage; }... } A Student class

14 public class Student { private String id, name; private float gradePointAverage; public Student(String inId, String inName, float inGPA) { id = inId; name = inName; gradePointAverage = inGPA; } public Student(String inId) // create a "dummy" Student { this(inId, null, -1.0F); // calls constructor! }... } Student constructors

15 What if you want the GPA of a student but you only know their ID?

16 public static void main(String args[]) { Bag allStudents = new Bag(5000);... Student dummy = new Student("123456789"); // Want the GPA of student "123456789" System.out.println("GPA: " + dummy.getGPA()); // meaningless Student real = allStudents.locate(dummy); if (real != null) System.out.println("GPA: " + real.getGPA()); // meaningful else System.out.println("Student not found"); } Make a dummy Student

17 public static void main(String args[]) { Bag allStudents = new Bag(5000);... Student dummy = new Student("123456789"); // Want the GPA of student "123456789" System.out.println("GPA: " + dummy.getGPA()); // meaningless Student real = allStudents.locate(dummy); if (real != null) System.out.println("GPA: " + real.getGPA()); // meaningful else System.out.println("Student not found"); } Do you know why? This code does not work!

18 public static void main(String args[]) { Bag allStudents = new Bag(5000);... Student dummy = new Student("123456789"); // Want the GPA of student "123456789" System.out.println("GPA: " + dummy.getGPA()); // meaningless Student real = allStudents.locate(dummy); if (real != null) System.out.println("GPA: " + real.getGPA()); // meaningful else System.out.println("Student not found"); } The return value of locate() is an Object The problem!

19 public static void main(String args[]) { Bag allStudents = new Bag(5000);... Student dummy = new Student("123456789"); // Want the GPA of student "123456789" System.out.println("GPA: " + dummy.getGPA()); // meaningless Object obj = allStudents.locate(dummy); if (obj != null) { Student real = (Student)obj; // explicit cast System.out.println("GPA: " + real.getGPA()); // meaningful } else System.out.println("Student not found"); } The solution!

20 Children can’t point at parents Invalid: Date date = new Object(); Valid: Object obj = new Date(2013, 2, 22); Why? Every Date is an Object (by default) But Object is not necessarily a Date


Download ppt "CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the."

Similar presentations


Ads by Google