Sunday, 7 September 2014

Review

Assumed skills


  • Basic programming language skill
  • Fundamental knowledge of computers
  • Basics of database
  • Basic mathematics



3.6 JDK 1.5 introduced a new for loop, known as foreach loop, do youknow what it is?

The enhanced for loop has the following syntax
for (variable: Acollection) {
Statements;
}

4. Java arrays

An array is a data structure that stores a collection of values of the same type. You access each individual value through an integer index. The array is widely used
in Java.

4.1 Declaring an array variable and allocating storage

You need to specify the basic type of the array, i.e., the element type. The syntax is like

double[] mystore;

which declares the variable mystore. It does not yet initialize mystore with an actual array. You use the new operator to create the array (storage) with the size
information (5 below)

double[] mystore = new double[5];

mystore is simply a reference to the actual storage for the array elements.

4.2 Initializing array elements

Once the array is declared and allocated, you can fill the space with initializers. There are two ways
double[] mystore = {1.2, 1.4, 1.6, 1.8, 2.0}; // no new operator

or

double[] mystore = new double[5];
mystore[0] = 1.2;
mystore[1] = 1.4;
mystore[2] = 1.6;
mystore[3] = 1.8;
mystore[4] = 2.0;

4.3 Array copying

Suppose that array1 and array2 are two array variables of the same base type, then the following assignment makes both variables refer to the first array,
array2 = array1;

If you actually want to copy all values of the first array into the second array, you could use the arraycopy method in the System class

Symtem.arraycopy(from, fromIndex, to , toIndex, count);

Please try the following example yourself

// File: ArrayCopy.java
// A simple program used for practice
// Simulates array copying

public class ArrayCopy
   { 
public static void main( String[] argv )
   { 
int[] myPrimes =
   {
2, 3, 5, 7, 11, 13};
//create array of base type int and size 6
int[] myYears = {2001, 2002, 2003, 2004, 2005, 2006};

System.arraycopy(myPrimes, 2, myYears, 3, 3);

for (int i=0; i < myYears.length; i++)
      System.out.println( i + ": " + myYears[i]);
}// end main
}//end class


4.4 Array sorting

The Arrays class contains various static methods for processing an array. For example, you can use the sort method to arrange array elements in order.

Try this program and use the numbers printed as your next lottery numbers. It may make you rich.

// File: ArraySort.java
// A simple program used for creating lucky
// lottery numbers

public class ArraySort
{ public static void main( String[] argv )
{ int[] LotteryNumbers =
new int[49];
int[] myChoices = new int[6];

for (int i = 0; i < 49; i++)
LotteryNumbers[i] = i + 1;

for (int i=0; i < 6; i++) {
int r = (int) (Math.random() * 49);
myChoices[i] = LotteryNumbers[r];
}

Arrays.sort(myChoices);
System.out.println(“The lucky numbers are: ”);

for (int r: myChoices)
System.out.println(r);
}// end main
     }//end class

There is a small bug in the above program. Can you find it out and fix it?

5. Interfaces

The interface is one of important techniques that Java offers. Basically we always design java classes for our tasks. For example, an Employee class might have
extended classes Casual, SalesPerson and Manager.

Sometimes we want to begin a project with some basic promises. For example, the concept of geometric shapes may cover a lot of different kinds of geometric
objects such as triangles, rectangles, arcs, circles, elliptics etc. As shapes, those objects can be drawn and they may have areas. To characterize these, with Java
we can extract the common features to build an interface Shape which promises the capability for drawing and area calculation. At this stage, we have no
knowledge about how to draw and how calculate areas, but the Shape has such potential.

In Java, the interface Shape can be created by

interface Shape {
public drawing();
public areas();
... ...
}


The syntax is similar to a class declaration but with the keyword interface. An interface is not a class. It has nothing but some future promises. To convert an
interface into a concrete class, you must implement the interface as a class. For example,

class Triangle implements Shape {
Public drawing() {
Draw the triangle
public areas (){
calculate the area of an triangle
... ...
}

There are many interfaces defined in Java. For example, Comparable interface, Component interface, Action interface, ActionListener interface etc. You will
learn part of them in this subject.

Although an interface is not a concrete class from which we can create instances. Following the “is-a” rule you can treat an interface as the base type of the
implemented classes, just like base class for the extended classes.




6. Abstract classes

The interface provides promises for later implementation of concrete classes. However sometimes, it is convenient and efficient for us not to fully implement a
class. By the Java grammar you must implement all the methods for a class. If you don’t want to implement some of methods in a class, you must declare them
as an abstract methods. An abstract method means we will implement it later in an extended class. For example, the getWage function in the Employee base
class should be an abstract method because we don’t know how to calculate wages for an un-classified employee.

An abstract method is declared as follows

public abstract double getWage();

please note the keyword abstract just after the identifier public and no curly brackets { }.

If you have at least one abstract method in a class, then the class is an abstract class. You must have the keyword abstract in the class declaration as follows

public abstract class Employee {
... at least one abstract method
}

Like an interface, an abstract class is not a concrete class, so you cannot create any instances from an abstract class. To make an abstract class available, you
must extend it to a concrete class in which you should implement all the un-implemented abstract methods.

The difference between interfaces and abstract classes is that an interface contains only constant data and abstract methods and that an abstract class may
contain non-constant data members and implemented methods. An interface should be implemented into a concrete class and an abstract class should be
extended into a concrete class. An interface does not have any constructors.

No comments:

Post a Comment