Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, June 8, 2013

Java - Thread

Java offers thread manipulation that allows even single core computer do multitasking, which is also known as Multiprogramming, a way of parallelism.

A Java thread will always be in one of four different states:

  • New
  • Runnable
  • Blocked
  • Dead
"When designing a program that uses multiple threads, we must be careful not to allow an individual thread to monopolize the CPU."  (Textbook)
The above mentioned could potentially lead to hanging of an application, which appears to be running but in fact, doing no computation at all. However, some OS can solve this problem by bring Round Robin as the mechanism to avoid such monopolization. 

A linked list is a collection of nodes that form a linear ordering together. 
A singly linked list:
The next reference inside a node can be viewed as a link/pointer to another node.
Moving from one node to another by following a next reference is known as link hopping/pointer hopping.
The first and last node of a linked list are called the head and tail of the list.
The tail contains a null reference, that indicates the ends of the list.

A singly linked list does not have a predetermined fixed size, and uses space proportional to the number of its elements.

Saturday, April 13, 2013

Java - Iteration and Arrays


"while" loops to check a number is prime or not.

public static boolean isPrime (int n) {
  int divisor =2;
  while (divisor < n) {
    if (n%divisor == 0) {
      return false;
    }
  return true;
}

"for" loops

for (initialize; test; next) {
  statement;
}
  Equivalent to: 
initialize;
while (test) {
  statement;
}
next;

ARRAYS
initialize a list of charactors:
char[] c = new char[4]; // reference to array of charactors of any length

For strings c.length() is a method
For characters c.length is a field.

Print the primes from 1 to n using Sieve of Eratosthenes:
public static void printPrimes(int n) {
boolean[] prime = new boolean[n+1];
        int i;

for (i=2; i<=n; i++){
prime[i]=true;
}
for(int divisor=2; divisor*divisor<=n; divisor++){
if(prime[divisor]){
for(i=2*divisor; i<=n; i=i+divisor){
prime[i]=false;
}
}
}
for(i=2; i<=n; i++){
if(prime[i]){
System.out.print(" "+i);
}
}
}
}

Friday, April 12, 2013

Java - Object & Methods

Object & Methods

String s1; //Step 1: declare a String variable.
s1 = new String(); //Step 2: assign it a value: a new empty string.

first line create a box, store references in strings.
second line construct a brand new string object.
= create a reference

A syntax sugar in Java:
String s2 = new String();

s1 = "Yow!"; also creates a constructor with Yow in the box.

now s1 forget the last one and remembers yow instead of the empty one.

s2 = s1; // reference to the same object;
s2 = new String (s1); //now 2 different, identical objects.

3 String constructors:

  1. new String() constructs empty string - contains no character. 
  2. "whatever"
  3. new String(s1) takes a parameter s1. Makes copy of object that s1 references.
Constructors always have the same name as their class, except "stuff in quotes".

s2 = s1.toUppercase();
String s3= s2.concat("!!"); // or just s2+"!!";
String s4 = "*".concat(s2).concat("*");//"*".concat(s2) compute first

In Java, String object are immutable unlike in C: their contents never change.


Java I/O Classes

Objects in System class for interacting with a user:
System.out is a PrintStream object that outputs to the screen.
System.in is an InputStream " " reads from the keyboard.
Actually, the two mentioned above should be variables that references to the object.

readLine method is defined on BufferedReader objects.

How do we construct a BufferedReader(compose into entire lines of text)? With an InputStream.Reader.
How .. an InputStreamReader(compose into characters typically 2 bytes)? We need an InputStream 
How .. an InputStream(read raw data)? System.in is one.
Figure this out from online Java libraries API. Specifically, java.io library here.

import java.io.*;//to use the java library, other than java.lang

class SimpleIO{
  public static void main(String[] arg) throws Exception{
    BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(keybd.readLine());
  }

-------------------------------------------------------------------------------------------------------

import java.net.*;
import java.io.*;

public class readURL {
public static void main(String[] arg) throws Exception{
URL u = new URL("http://www.bbc.co.uk");
InputStream s = u.openStream();
InputStreamReader inputrd = new InputStreamReader(s);
BufferedReader bbc = new BufferedReader(inputrd);
System.out.println(bbc.readLine());
}

}

Java – OOP intro


Some concepts in Object-Oriented Programming.
Object: Repository of data, such as milk or jam. 
Class: Type of object. 
Method: Procedure or Function that operates on an object (or class).
Inheritance: A class can inherit properties from a more general class. For instance, Shopping List inherits from List class the property of storing a sequence of items.
Polymorphism: One method call works on several classes, even if the classes need different implementation. e.g. “addItem” method on every kind of List, even though adding item to ShoppingList is different from ShoppingCart.
Polymorphism is the thing that distincts from other languages.
Object-oriented: Each object knows its own class&method. Each ShoppingList & ShoppingCart knows which addItem method it uses.


Code example:

String myString;
// mystring is a variable
myString = new String();
// new String() is called a constructor. It creates a string object,
// "=" causes myString to a reference the object

Differences between Java & Scheme:

  1. Everything in Java has a type; you must declare it.
  2. Scheme program (.sch) --> (eval) Answer. Java program (.java) --> (javac) .class Files --> (java) Answer