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

Saturday, February 5, 2011

Java Tutorial: BufferedReader Example

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author paiman
*/
public class HelloBufferedReader {

public static void main( String[] args ) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader( System.in) );
String name;
System.out.print("Please Enter Your Name: ");
name = br.readLine();
System.out.println("Hello, " + name +"! Thank you for visiting my blog :)");
}
}

Java Tutorial: Thread Example

Two ways of Creating Thread

1. Extending from Thread class
  • Extends a class from the Thread class
  • Override the run( )
  • Create an instance of the above class
  • Start the Thread

/**
*
* @author paiman
*/
class DispThread extends Thread{

String msg;

public DispThread(String msg) {
this.msg = msg;
}

@Override
public void run(){
for(int i=0; i<5;i++){
System.out.println(msg);
}

}
}


/**
*
* @author paiman
*/
public class ThreadTest {

public static void main (String[]args){
DispThread dt1 = new DispThread("Thread");
DispThread dt2 = new DispThread("Example");
dt1.start();
dt2.start();
}
}

2. Implementing Runnable
  • Implement Runnable on a class
  • Implement the run( ) of the Runnable interface
  • Create an instance of the above class
  • Create a Thread object by passing the Runnable object as parameter
  • Start the Thread

/**
*
* @author paiman
*/
public class DispThread2 implements Runnable{

String msg;

public DispThread2(String msg) {
this.msg = msg;
}

@Override
public void run(){
for(int i=0; i<5;i++){
System.out.println(msg);
}

}

}


/**
*
* @author paiman
*/
public class ThreadTest2 {

public static void main (String[]args){
DispThread2 dt1 = new DispThread2("Thread");
DispThread2 dt2 = new DispThread2("Example");
Thread t1 = new Thread(dt1);
Thread t2 = new Thread(dt2);
t1.start();
t2.start();
}
}

Java Tutorial: Array Example

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author paiman
*/
public class AverageValue {

public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double[] nilai = new double[4];

System.out.print("Value A : ");
nilai[0] = Double.parseDouble(in.readLine());

System.out.print("Value B : ");
nilai[1] = Double.parseDouble(in.readLine());

System.out.print("Value C : ");
nilai[2] = Double.parseDouble(in.readLine());

nilai[3] = (nilai[0] + nilai[1] + nilai[2])/3;
System.out.println("\nAverage value = " + nilai[3]);
}
}

Java Tutorial: Scanner Example

import java.util.Scanner;

/**
*
* @author paiman
*/
public class HelloScanner {

public static void main(String[] args) {
Scanner input = new Scanner( System.in );
System.out.print ("What is your name? ");
String name = input.nextLine();

System.out.println("Hello, "+name+"! Welcome to my blog :)");
}
}


import java.util.Scanner;

/**
*
* @author paiman
*/
public class RectangleWide {

public static void main(String[] args) {
Scanner input = new Scanner( System.in );
System.out.print ("Input long: ");
int l = input.nextInt();
System.out.print ("Input width: ");
int w = input.nextInt();

System.out.println("Rectangle wide: "+l*w);
}
}


import java.util.Scanner;

/**
*
* @author paiman
*/
public class CircumferenceOfACircle {

public static void main(String[] args) {
Scanner input = new Scanner( System.in );
System.out.print ("Input radius: ");
double r = input.nextDouble();
double pi = 3.142857143;
double circumference = 2*pi*r;

System.out.println("The circumference of a Circle with radius "+r+" is "+circumference);
}
}

Monday, February 1, 2010

Download Ebook: Thinking in Java, 3rd Edition

DOWNLOAD

Saturday, November 14, 2009

Download Ebook: Java How to Program, Fourth Edition (Harvey M. Deitel & Paul J. Deitel)


The authoritative DEITEL LIVE-CODE introduction to programming with the Java 2 Platform, Standard Edition (J2SE)

Java has revolutionized software development with multimedia-intensive, platform-independent, object-oriented code for conventional, Internet-, Intranet- and Extranet-based applications and applets. This exciting new Fourth Edition of the world's best-selling Java textbook now has a companion volume Advanced Java 2 Platform How to Program which focuses on the Java 2 Platform, Enterprise Edition (J2EE), presents advanced J2SE features and introduces the Java 2 Platform, Micro Edition (J2ME).

Dr. Harvey M. Deitel and Paul J. Deitel are the founders of Deitel & Associates, Inc., the internationally recognized corporate training and content-creation organization specializing in Java, C++, C, Visual C#, Visual Basic, Visual C++, .NET, XML, Python, Perl, Internet, Web and object technologies. The Deitels are also the authors of the world's #1 C++ textbook C++ How to Program, 3/e and many other best sellers.

In Java How to Program, Fourth Edition the Deitels introduce the fundamentals of object-oriented programming in Java. The 4th edition includes an optional 180-page case study that introduces object-oriented design with the UML.

DOWNLOAD