You are on page 1of 5

Jan.

27 reflections

Class powerpoint can be found on ilearn… and I followed through the installation of Java SE and
then installation of netbeans with all packages. It shows the steps through opening project, build
and run, set breakpoint and add watch list. Instead of Idx in my version it uses wordIdx instead.

Then I went through GitHub tutorial… create master and edit-readme branch, made some
changes and commit, create pull request and then merge pull request where it has options of
create a merge all commit, squash and merge 1 commit, or rebase and merge 1 commit. The
result looks like the following:

Each object has its role and contains its information. Information hiding is achieved by knowing
what the object’s role is without knowing the details of the implementation.

Client passes messages to the server providing the service with its methods of computation.
Messages contain receiver destination of server and associated information to be processed by
server.

The role and service of the server can be described in a class, and each server object can be
instantiated with that class capable to performing computation of tasks from the messages
received.

Within class hierarchy, higher level class (superclasses) in general shared its role and
information with its lower level class (subclasses). We sometimes get to look into higher class in
hierarchy for instances function, and never create an instance from the more-generalized classes
in higher hierarchy, or abstract classes as no place to hold its functions.

Java comes from C++ and smalltalk, is web-based portable system in distributed programming
heterogeneous architecture. It doesn’t have multiple inheritance like C++, is object-oriented, is
network-savvied such as URLs and sockets, and is robust with exception handling and garbage
collection. It has its own VM and is portable on various architecture with fixed machine-
dependent details. It is multithreaded with parallel execution and client-server computing,
dynamic with pluggable code and classes.

Source code Java compiler Java VM output


xxx.java javac xxx.java java xxx
(generate byte code in xxx.class for JVM) (JVM execute byte code in xxx.class)

JVM is a program specification which interprets by scanning byte codes and performing
indicated operations on virtual machine. Therefore instances can execute same byte code on
various machine environment. Java programs are allowed only in sandboxes where the VM
won’t allow codes to access/modify client’s files.

Github: Hello.java

Import java.lang.* //import from standard library

public class Hello { //one public class per file, class name as file name
public static void main ( String args[] ) { //array of String
If ( args.length > 0 ) { //a property of the array object accessing by .length
System.out.println( “Hello “ + arg[0] ); //+ to concatenate Strings
} else {
System.out.println( “Hello everybody!” ); //print followed by a return
}
}
}

Compiling:

javac Hello.java
Results in Hello.class file with byte codes

Executing:

Java Hello
Hello everybody!

Java Hello folks


Hello folks
Default Constructor: An initial default method for creating instance. Without providing
constructors for a class, the compiler includes a default, public, no-argument constructor

public A() {} //is provided by compiler in the following code

/* default constructor example */


public class A {
int i;
//a default constructor is provided by the compiler: public A() {}
void p() {
System.out.println( I );
}
}

/* no default constructor example */


public class B {
int i;

public B( int j ) { //no default constructor is provided: always instantiate B with an int arg
i = j;
}

void p() {
System.out.println( i );
}
}

Github: ReferenceSemantics.java //unlike C++, Java refer to objects.

Public class ReferenceSematics {


Public static void main( String[] args ) {
A a = new A(); //a refers to an instance of A
A aa;
aa = a; // a and aa refer to the same object

B b = new B( 5 );
aa.incI( b ); // setting i in the same object
a.incI( b ); // setting i in the same object

System.out.format( “ a’s i is %d\n”, a.getI() );


System.out.format( “ aa’s i is %d\n”, aa.getI() );
}
}
class A {
private int i = 0;

public void incI( B bb ) {


i = bb.inc();
}

public int getI() {


return this.i;
}
}

class B {
private int I = 0;

B( int I ) {
This.i = I;
}

public int inc() {


return ++I;
}
}

Output
 java ReferenceSemantics
a’s I is 7
aa’s I is 7

aa  a  i: 0 6 7

b  i: 5 6 7

Exception Handling

try {
<statements that may throw exception>
} catch ( ExceptionType exceptionOne ) {
<statements to handle exception>
}
….
} catch ( ExceptionType exception ) {
<statements to handle exception>
} finally {
<statements that will always be executed, with or without exception>
}
/* exception without catching */
public class ExceptionTestOne {
static int number[] = { 0, -2, 5, 27, 3};

public static void main( String[] args ) {


int sum = 0;

for ( int I = 0; I <= 5; i++ ) {


sum += numbers[ i ];
}
}
}

 java ExceptionTestOne
Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException: 5
at ExceptionTestOne.main(ExceptionTestOne.java:8)

/* catching exception */
public class ExceptionTestTwo {
static int number[] = { 0, -2, 5, 27, 3};

public static void main( String[] args ) {


int sum = 0;

try {
for ( int I = 0; I <= 5; i++ ) {
sum += numbers[ i ];
}
} catch ( ArrayIndexOutOfBoundsException e ) {
System.out.println( “*** Array index out of bounds” );
}

System.out.println( “Sum: “ + sum );


}
}

 java ExceptionTestTwo
*** Array index out of bounds
sum: 33

You might also like