Thursday, 28 March 2013

LAB 5 -exercise 1 - ArrayList -July 2013


Array List Exercise ( LAB 4 )
--------------------------------------------------------------------------------------------------------
1) Create 1 class and 2 subclasses of that class . Implement appropriate polymorphic method. the method should print out something.
2) Creating an ArrayList of the specific superclass of above
3) Add 5 Item/Element/object of superclass/subclasses into the above ArrayList
4) Check and print the size of ArrayList
5) Check and print all the item inside the array list.
6) Check Index of an Item in Java Arraylist
7) Retriev Item from arrayList in a loop ( without using iterator interface )
8) Check ArrayList for an Item
9) Check if ArrayList is Empty
.
10) Remove an Item from ArrayList
11) Copy data from one ArrayList to another ArrayList in Java
12) Replace an element at a particular index
13) Clear all data from ArrayList
14) Convert from ArrayList to Array in Java
15) Traverse ArrayList in Java using iterator

Sunday, 17 March 2013

Lab 3 July 2013 - Additional Exercise 3



  1. Firstly, create and compile a simple class called Parent. Give it the following behaviour:
    1. A default constructor that does nothing other than print out “Parent default constructor” using System.out
    2. A single method called getMessage which returns a String, e.g. “Parent message”
  2. Next, create and compile a class called Child. Give it the following behaviour
    1. Do not give it a constructor
    2. Override the parent’s getMessage method to return an alternative String. E.g. “Child message”
    3. A main method which creates an instance of the Child object, and then writes the value returned by its getMessage method to the command line.
    4. What happens when the class is run?
  3. Alter the Child class to give it a default constructor which prints out “Child default constructor”. Compile and run the application again and identify what happens.
  4. Alter the implementation of the getMessage method in the Child class so that it first calls the parent class method, then concatenates the result with its own value to build a combined message. E.g. returning “Parent message and Child message”.
  5. Alter the Parent class by
    1. Adding a new constructor that accepts a String argument. This should be used to initialise a private member variable, myMessage. Again write a message to the console that indicates that the constructor has been called.
    2. Alter the getMessage method so that it returns the value of the myMessage rather than a fixed message.
    3. Now alter the Child class so that it from its default constructor it calls the new constructor on the parent class.
  6. Alter the myMessage member variable in the Parent class so that it is declared to be protected. Confirm that the Child class can now refer to the variable directly, rather than having to call its parent’s version of getMessage to build the combined message. Can you think of the pros and cons of the two different mechanisms?