Week 4 & 5 – Arrays, Array Lists, UML

 Arrays

  • Created to form a large amount of uniform data
  • Arrays are Mutable, but they length cannot be changed
  • The method to create an Array looks like the following:

int[] a= {1, 2, 3, 4, 5};

  • Java basically creates large box of 5 sub boxes and stores each value in one of the boxes, so a[0]= 1 etc.
  • To print of particular value, use it like list and list indices are used in python
  • So System.out.println(a[2] + ” ” + a[3]); –> we get –> 3  4
  • In order to loop through all the numbers in the array, we could use the enhanced for statement

for (int temp: a){

System.out.println(temp);

}

All values from 1 – 5 will be printed one by one just as we iterate using the for loop in python.

Arrays do not necessarily have to be created in this manner. An empty Array can be designed placed values into it later. This is done in the following way:

int[] MyArray = new int[5]; –> (This indicates that array will hold 5 values in it all-together)

MyArray[2] = 42 –> (So we have an Array for 5 integers, the third being 42, the rest are 0’s)

for (int i=0; i<MyArray.length, i++){ (For as long as the value of i is less than the length of MyArray keep looping and each time a loop is completed add 1 to i; i is an int variable that only exists within the loop)

System.out.println(MyArray[i] + ” “);

}

The result given is–> 0 0 42 0 0 each starting at a new line as expected. “i++” can be written in the parenthesis of the for loop of after the print line. Either way it prints the same result

String[] StringArray = {“one”, “two”, “three”}

for (int i=0, i < StringArray.length; i++) {

System.out.println(StringArray[i] + “”);

}

The result is–> one, two, three each starting at a new line as expected

for (String str: StringArray){

System.out.print(str + “”);

}

By writing “String str: ” we are created a new defined variable of type string and by the name of str. This variable again only exists inside the for loop.

This method too prints –> one, two, three each starting at a new line. So we could print one, two, three either using the Indicies method shown in the previous example or through the str variable method shown above.

And Follow through the next example and try to figure out on your own.

for (Integer i: MyArray){

System.out.print( i + ” “);

}

As you may have guessed already. This also prints 0 0 42 0 0 each starting at a new line and likewise it uses the same method of a new variable instead of following through the index.


Array List

  • Their size is changeable unlike Arrays
  • Array Lists are declared in the following manner

ArrayList <String>  b07team = new ArrayList<string>();

  • We have just declared an ArrayList of type String
  • Array Lists append/add in the following manner

b07team.add(“anya”);

b07tem.add(“eric”);

  • The method to iterate, or in other words go through each element one by one using a for loop is done in the following manner:

for (String member: b07team){

System.out.println(member);

}

  • ArrayList <E> – E is where the type for the ArrayList is indicated
  • ArrayLists only exist for objects, hence no ArrayLists for int, but only for Integers
  • We could still include int in ArrayLists under the Integer list, what happens here is called auto-boxing
  • for all E’s <T> – parametric polymorphism- an array of multiple different objects

UML

UML is the abbreviation of Unified Modelling Language, the language which is created for modelling purposes to present a software design in the manner that is easy to understand for others as well as yourself.

  • It is known to be a standard way to visualize the design of a class/structure
  • Developed by Grady Booch, Ivar Jacobson and James Rumbaugh

The diagram for  a particular class is presented in a box, with the class name at the top in the middle, in Itlaic if the class is an abstract class otherwise written regularly. However if the class is in interface, in that case it should clearly be indicated as <<interface>>. This is then separated by a line.

The second rectangle within the box of that class, informs users about the variables used in that particular class, their types and whether they are public, private, protected or package protected.

First goes the sign “-“, “+”,”#” where “+” indicated that the variable is a public variable, “-” indicated that the variable is a private variable and “#” indicated that the variable is protected. Next goes the variable name, and followed by the name we have a semi-Colin which is followed by the variable type.

Example:

If I have an array of strings that stores the name and if I want to make that variable protected this is what I write on my UML diagram:

# name : String[]

If the variable is a static variable, then it is underlined in the UML diagram.

The variable column is also separated by a line.

The third rectangle within the box of our class consists of the constructor and the method names of the class. We again first indicate with the sign whether the method is private, public or protected, then we write the method name or the constructor name depending on what we are writing, we then clearly indicate the parameter this function takes by first writing the parameter name followed by a semi-colin followed by the variable name all in brackets. This is not where it ends. Whether my method is a void (outputs nothing)  or it outputs something I am supposed to indicate that. I would do that by putting a semi-colin after the brackets and writing the output type that I am expecting the function to release.

I want you to have a look at this class and interpret based on what you have read/learned just right now:

Person(UML)Although we so wish, this was all we needed to know, but unfortunately or fortunately for others there’s some that this. Since we are now learning about Object Oriented Design we cannot be limited presenting just one class and making it. We got to know about the real designs which includes the use of inheritance and interface. By now we know what Inheritance/Subclasses are and what is the use of an Interface or an Abstract class, but how to we indicate that one class is a child of another on UML or that one class implements another.

Yup, there’s already a method to do that. If one class is simply a subclass of another if point to its superclass with an arrow. Likewise if one class is implementing an interface, it points to its interface with a doted arrow. Studying all these cases we can now draw any design using the UML and interpret from it as well. Here’s a diagram I leave for you to interpret:

UML diagram

 

 

 

 

Leave a comment