1 Object-Oriented Software Engineering CS288. 2 Multiple Classes Contents Lists of objects Vectors...

Post on 11-Jan-2016

216 views 0 download

transcript

1

Object-Oriented Software Engineering

CS288

2

Multiple Classes

Contents

• Lists of objects

• Vectors

• Growing and shrinking Vectors

• Iteration over Vector Object

• Generic Objects

3

Back to first version of SimpleClass

public class SimpleClass { private String uselessField; public SimpleClass (String newFieldVal) { setUselessField (newFieldVal); } public String getUselessField () { return uselessField; } public void setUselessField (String newUselessField) { uselessField = newUselessField; } public static void main (String[ ] args) {

// Add Code Here } }

4

The FooClass class

public class FooClass { private String fieldOfFoo; public FooClass (String newField) { setFieldOfFoo(newField); }

public String getFieldOfFoo ( ) { return fieldOfFoo; }

public void setFieldOfFoo (String fieldOfFoo) { this.fieldOfFoo = fieldOfFoo; }

}

5

Lists of Classes

Add new field to SimpleClass

private Vector listFoo = new Vector ( );

The Vector class implements a growable array of objects.

Vector V:

v0 v1 v2 v3

v0 v1 v2 v3

6

Vectors have arbitrary size

Vectors are declared with no fixed length

private Vector listFoo = new Vector ( );

Whereas an array must have a fixed size givenat initialisation:

private String[ ] stArray = new String[20];

7

Methods for adding and removing objects

public void addFoo (FooClass newFoo) { listFoo.addElement (newFoo); } public void removeFoo (FooClass newFoo) { listFoo.remove(newFoo); }

Vector class methodfor increasing size ofa vector object andinserting new object

Vector class methodfor decreasing size ofa vector object andremoving object

New methods for SimpleClass to add and remove objects from thelistFoo field.

8

Vectors hold any object

• Vectors can hold any objects, whereas an array has fixed types for the objects in the array.

• Do not have to declare what type of objects will be stored in a vector when declaring them.

• Java compiler warns you of possible problems when compiling code:warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector

listFoo.addElement (newFoo);1 warning

9

Experiment with main method public static void main (String[ ] args) { SimpleClass s1 = new SimpleClass("test object"); FooClass f1 = new FooClass("apple"); FooClass f2 = new FooClass("orange"); FooClass f3 = new FooClass("pear"); FooClass f4 = new FooClass("banana"); FooClass f5 = new FooClass("grape"); s1.addFoo (f1); s1.addFoo (f2); s1.addFoo (f3); s1.addFoo (f4); s1.addFoo (f5); }

10

2nd Experiment public static void main (String[ ] args) { SimpleClass s1 = new SimpleClass("test object"); FooClass f1 = new FooClass("apple"); FooClass f2 = new FooClass("orange"); FooClass f3 = new FooClass("pear"); s1.addFoo (f1); s1.addFoo (f2); s1.addFoo (f3); s1.removeFoo (f1); }

s1 before final statement

s1 after final statement

11

3rd Experiment public static void main (String[ ] args) { SimpleClass s1 = new SimpleClass("test object"); FooClass f1 = new FooClass("apple"); FooClass fx1 = new FooClass("apple"); FooClass f2 = new FooClass("orange"); FooClass f3 = new FooClass("pear"); s1.addFoo (f1); s1.addFoo (f2); s1.addFoo (f3); s1.removeFoo (fx1); }

s1 before and afterfinal statement.f1 not removed

12

public static void main (String[ ] args) { SimpleClass s1 = new SimpleClass("test object"); FooClass f1 = new FooClass("apple"); FooClass fx1 = f1; FooClass f2 = new FooClass("orange"); FooClass f3 = new FooClass("pear"); s1.addFoo (f1); s1.addFoo (f2); s1.addFoo (f3); s1.removeFoo (fx1); }

4th Experiment

s1 before final statement

s1 after final statement

fx1 refers to same object as f1,and removing it means removingthe object it refers to. Hence f1is removed from the Vector.

13

Iteration with Vectors public static void main (String[ ] args) { SimpleClass s1 = new SimpleClass("test object"); FooClass f1 = new FooClass("apple"); FooClass f2 = new FooClass("orange"); FooClass f3 = new FooClass("pear"); s1.addFoo (f1); s1.addFoo (f2); s1.addFoo (f3); for (Enumeration e = s1.listFoo.elements () ; e.hasMoreElements () ;) { FooClass fx = (FooClass)e.nextElement (); System.out.println(fx.getFieldOfFoo ()); } }

• An Enumeration object e contains a set of objects that can be generated one at a time.

• Note the generated objects are of type Object, and they have to beexplicitly cast to the correct type in the loop.

• s1.listFoo is a Vector object, and e = s1.listFoo.elements () returns an enumeration object for the Vector containing all the elements in the Vector.

14

Iteration in for loop

for (Enumeration e = s1.listFoo.elements () ; e.hasMoreElements () ;) { FooClass fx = (FooClass)e.nextElement (); System.out.println(fx.getFieldOfFoo ());} On first iteration, e has this value.

The integer count is used to keeptrack of which objects have been generated so far.

15

Iteration with standard for loop

for (Enumeration e = ob1.listFoo.elements () ; e.hasMoreElements () ;) { FooClass fx = (FooClass)e.nextElement (); System.out.println(fx.getFieldOfFoo ()); }

for (int i =0 ; i < ob1.listFoo.size ( ) ; i++) { FooClass fx = (FooClass)s1.listFoo.elementAt(i); System.out.println (fx.getFieldOfFoo ()); }

Iteration with Enumeration class:

Can also be achieved with a more standard style of for loop:

16

Iteration with variable placeholders

Vector vec = s1.listFoo;int size = vec.size( );for (int i =0 ; i < size ; i++) { FooClass fx = (FooClass)vec.elementAt(i); System.out.println (fx.getFieldOfFoo ()); }

To highlight the structure of the standard for loop we canintroduce some placeholder variables:

The additional variables do not make the code ‘better’,they merely simplify the syntax so that the loop structure is easier to see.

17

Making Vectors Safe

• SimpleClass is not accessing Vector in safe manner. • Compiler is flagging problem:warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector listFoo.addElement (newFoo);1 warning

18

Vectors are Raw Types

When we declare a new variable as a Vector object we do not declare what type of objects will be stored in the Vector:private Vector listFoo = new Vector ( );

To see why this can be a problem, change the field listFoo to be public:public Vector listFoo = new Vector ( );

Change main method for SimpleClass to public static void main (String[ ] args) { SimpleClass s1 = new SimpleClass ("test object"); s1.listFoo.addElement ("Flip Flop"); /* other code */}

19

Vectors are Raw Types

• Despite the fact that the listFoo Vector is only ‘meant’ to store FooClass objects we have added a String object. This does not cause a compiler error, however at run time we get this error:

• Exception in thread "main" java.lang.ClassCastException: java.lang.String at vectorexample.SimpleClass.main(SimpleClass.java:42) Java Result: 1

20

Generics

• A class that can contain classes of arbitrary type, without having to explicitly declare what the types are, is known as a generic class.

• Java 1.5 provides a mechanism for telling the compiler what type of object is meant to be stored in a generic class.

• In our example we would use this syntax to instruct the compiler that only FooClass objects are allowed in the listFoo Vector.

public Vector<FooClass> listFoo = new Vector <FooClass> ( );

The compiler will now pick up on our attempt to add a String object to listFoo:

21

Building Class Example

As an elementary example consider a class to represent the concept of a building.

Buildings have rooms.

We will need a Room class.Rooms have a size (floor area), doors and windows.

As a starting point we will think of a Building as a class that contains a Vector of rooms.

22

Initial Room Class

/* start of class */public class Room { public Room() { }

/* fields */ private Double area; private int doors; private int windows;

/* then methods */public Double getArea() {return area; }

public void setArea(Double area) { this.area = area;}

public int getDoors() { return doors;}

public void setDoors(int doors) { this.doors = doors;}

public int getWindows() {return windows;}

public void setWindows(int windows) { this.windows = windows;}} /* end of class */

23

Extra methods for Room class

/* extra constructor */ public Room(Double newArea, int newDoors, int newWindows) { setArea(newArea); setDoors(newDoors); setWindows(newWindows); }

/* Later we want to display room data in tabular form, this will be useful for that */ public String[ ] toStringArray() { String areaSt = area.toString(); String doorsSt = Integer.toString(doors); String windowsSt = Integer.toString(windows); String[ ] room_fields = {areaSt, doorsSt, windowsSt}; return room_fields; }

24

Initial Building Class

public class Building { public Building() { rooms = new Vector<Room>(); } private Vector<Room> rooms;

public void setRoom(Vector<Room> newRooms) { rooms = newRooms; } public void addRoom(Room newRoom) { rooms.addElement(newRoom); } public Vector getRooms() { return rooms; } }

Note Vector is constrainedto be of type Room

25

Presenting Rooms Data

Given a building where we have lots of rooms it may beuseful to be able to gather all the relevant data for thoserooms into a single table (array of arrays).

It should not be the job of the Building class to displaythat data in any particular form. It is reasonable to supposethe Building class should extract the relevant data fromthe rooms Vector and present it as an array of arrays.

How do we do that?

Need a new method:

public String[ ][ ] roomsToArrayArray() { /* code goes here */

}

26

Presenting Rooms Data

public String[ ][ ] roomsToArrayArray() { Room rm; int rooms_length = rooms.size(); String[ ][ ] return_array = new String[rooms_length][3]; for (int i = 0; i < rooms_length; i++) { rm = rooms.elementAt(i); return_array[i] = rm.toStringArray(); } return return_array; }

Note, its not up to the Buildingclass to decide how a room isconverted to an array.That job is done by the Roomobject

27

Rooms Values (as shown in NetBeans Debugger)

28

Output for roomsToArrayArray

29

Summing Up

This lecture discussed

• Storing objects within a list structure.

• The Vector class

• Adding and deleting elements from a Vector object.

• Iteration over a Vector object.

• Problems with raw types, and safely using generic classes.