Please study the following: (this is IMPORTANT as it’s preparation for Paper 2 Exam)
(We will discuss it on the next meeting)

* Explain the role of the constructor and destructor in Object Oriented Programming (Give examples of it)!

// Create a MyClass class
public class MyClass {
  int x;  // Create a class attribute

  // Create a class constructor for the MyClass class
  public MyClass() {
    x = 5;  // Set the initial value for the class attribute x
  }

  public static void main(String[] args) {
    MyClass myObj = new MyClass(); // Create an object of class MyClass (This will call the constructor)
    System.out.println(myObj.x); // Print the value of x
  }
}

// Outputs 5

Destructor is a special method that will be executed when the object is deleted from memory.

Java itself does not have a destructor method, because Java uses a gerbage collector for memory management.

So the gerbage collector will automatically delete unused objects.

* Explain the differences between public, private and protected!

* Explain the terms inheritance, encapsulation and polymorphism (Give examples of it)!

Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Information:

Y means that it can be accessed;
N means inaccessible;
Subclass means child class;
World means all packages in the application.

* Explain the differences between Collection and Array in Java!

· Arrays are fixed in size, but Collections are dynamic in size. This is the reason why Collections are preferred over Arrays with respect to memory

· Arrays are preferred over Collections when we see with the perspective of performance

· Arrays can hold only homogeneous elements, but collections can hold both homogeneous and heterogeneous elements

· Collections have ready made Data Structures, Arrays do not

* Explain the function of following method of String Object in Java:

indexOf(), substring()

import java.io.*;
public class Test {

public static void main(String args[]) {
String Str = new String(“Welcome to the jungle”);
System.out.print(“Found Index :” );
System.out.println(Str.indexOf( ‘o’ ));
}
}

public class TestSubstring{  

 public static void main(String args[]){  

  String s=”Hello World”; 

  System.out.println(s.substring(6));//World 

  System.out.println(s.substring(0,5));//Hello 

 }  

}

* Every email address must:

– contain exactly one ‘@’ sign

– after the ‘@’ sign, there must be exactly one period ‘.’

– there must be at least 2 characters between the ‘@’ and the ‘.’ period.

– there must be at least 3 characters after the ‘.’ period

– there must be at least 3 characters before the ‘@’ sign

Using standard Java String methods, construct a checkEmail method that checks all the rules stated above.

public boolean checkEmail(String e)
{
boolean result = true; // assume the address is valid

int indexOfAt = e.indexOf(“@”);
int indexOfDot = e.indexOf(“.”);

if (e.substring(indexOfDot+1).indexOf(“@”) != -1) 
// there is more than one @
result = false;

else if (e.substring(indexOfDot+1).indexOf(“.”) != -1)

// more than one dot after the @
result = false;

else if (indexOfDot – indexOfAt < 2)
// more than two chars between the @ and the dot

result = false;

else if (e.substring(indexOfDot).lenth() < 3)
// less than 3 chars after the dot

result = false;

else if (e.substring(0,indexOfAt).length < 3)
// less than 3 chars before the @

result = false;

return result;
}

Additional for HL Students only

======================

* Explain all the components of the tree!

* State the algorithm of the traversal (Pre Order, In Order and Post Order) using recursive!

* Explain the captured flag method for implementing the tree traversal!