Prepared by: Mr. HIEENG Sokh Hymns - 1 -
Java Programming
Java Q: Java ? A: Java (Program) (www.sun.com) Q: C++ ? A: C++ Concepts Java
1. Install JDK (Java Development Kit) Compiler Java 2. Install NetBeans File Code File NetBeans Versions
JDK and NetBeans 6.8 Version 6.8 File Programmer Java File JDK NetBeans File
JDK and NetBeans 6.9 Version 6.9 2 Files File JDK File NetBeans 6.9 (www.netbeans.org) Download last version free
Prepared by: Mr. HIEENG Sokh Hymns - 2 -
Chapter 1
Datatype and Variable
Datatype C++ Java 1.
Datatype C++ char, short, int, long, float, double, long double Datatype Java
1. Integer number : Datatype byte, short, int, long 2. Floating-point number : Datatype float, double 3. Character : Datatype char 4. Boolean : Datatype True False Boolean
1.1. Integer number
Datatype Size Range
byte 1 byte - 128 to 127
short 2 bytes - 215
to 215 1
int 4 bytes - 231
to 231 1
long 8 bytes - 263
to 263 1
1.2. Floating-point number
Datatype Size Range
Float 4 bytes 3.4e-38 to 3.4e+38
Double 8 bytes 1.7e-308 to 1.7e+308
Q: 3.4e+38 ? A: 3.4e+38 3.4 x 1038 Q: Logo Java ? A: James Gosling Java
Prepared by: Mr. HIEENG Sokh Hymns - 3 -
1.3. Character
Datatype Size Range
char 2 bytes 0 to 65, 536 Java Support Fonts , , ...
1.4. Boolean
Datatype Size Range
boolean 1 byte true or false
boolean b; Keyword Variable b = true;
System.out.print(b = +b);
// b = true
Output Console
Q: True or False ? A: True or False
C++ int a;
a = 2;
cout
Prepared by: Mr. HIEENG Sokh Hymns - 4 -
String Java Variable String Class String
String stName;
stName = Sok Rotha;
System.out.print(Hi!, +stName);
String st1 = Java;
String st2 = Programming;
String st = st1 + +st2;
System.out.print(st = +st);
// st = Java Programming
String
String a = 1;
String b = 2;
String sum = a + b; System.out.print(Sum = +sum);
// Sum = 12
Ctrl + R = Ctrl + Space = Shift + F6 = Run Java
2. (Variables)
Java Variable a z A Z 0 9 Underscore ( _ )
Prepared by: Mr. HIEENG Sokh Hymns - 5 -
int age;
long ID;
float price;
char ch1;
double total_price;
Initialization of variable Variable
int age=18; Initialization
int age; Declaration
age=18; Assignment ( C++ 100%) float
double x = 2.5; x = 2.5;
float y = 2.5f; y = 2.5f;
float
Prepared by: Mr. HIEENG Sokh Hymns - 6 -
Chapter 2
Control Statement
1. Overview
Java Control Statement 3 1. Selection Statements Statement Statement execute if-else, switch
2. Loop Statements Statement execute while, do while for
3. Jumping Statements Statement execute Statement break continue
2. Selection Statement
2.1. if-else
statement statement if statement if execute statement else execute
= Form : if(condition){
statement(s);
}else{
Statement(s);
}
int a = 0;
if(a>0)
System.out.print(A is positive.);
else if(a
Prepared by: Mr. HIEENG Sokh Hymns - 7 -
2.2. switch
Switch statement statement Statement Execute
Form: switch (expression){
case value1:
statement(s);
case value2:
statement(s);
case value3:
statement(s);
default:
statement(s);
}
char ch = R;
switch(ch){
case R:
System.out.print(Red); break;
case G:
System.out.print(Green); break;
case B:
System.out.print(Blue); break;
Defaut:
System.out.print(Unknown color);
}
char ch = R;
switch(ch){
case R: case r:
System.out.print(Red); break;
case G: case g:
System.out.print(Green); break;
case B: case b:
System.out.print(Blue); break;
Defaut:
System.out.print(Unknown color);
}
Prepared by: Mr. HIEENG Sokh Hymns - 8 -
3. Loop Statement
execute statement 3.1. while
Loop execute statement Form :
while(condition){
statement(s); }
int i = 1;
while(i=1){
System.out.print(i + , );
i--;
}
// 5, 4, 3, 2, 1,
3.2. do while
Loop execute statements
Form:
do{
statements;
}while(condition);
int i = 1;
do{
System.out.print(i + , ); i++;
}while(i
Prepared by: Mr. HIEENG Sokh Hymns - 9 -
int i = 5;
do{
System.out.print(i + , );
i--;
}while(i>=1);
3.3. for Loop Loop Expression 3 statements statements 3
Initialization: Statement Loop statement execute Loop
Condition: Statement Loop Step: Statement Loop Form:
for(initialization; condition; step){
statements;
}
for(int i=1; i=1; i--)
System.out.print(i + , );
// 5, 4, 3, 2, 1,
4. Jumping statement statement execute statement 4.1. Break statement statement switch Loop for(int i=1; i
Prepared by: Mr. HIEENG Sokh Hymns - 10 -
4.2. Continue statement statement 1 Loop
for(int i=1; ib)
System.out.print(Maximum is +a);
Else
System.out.print(Maximum is +b);
}
}
Prepared by: Mr. HIEENG Sokh Hymns - 11 -
Chapter 3
Array
Q. What is array?
A. Array Variables Datatype Index
1. One dimensional array
One dimensional array Array Datatype arrayName[ ] = new Datatype[elements];
a[0] a[1] a[2] a[3] a[4]
a array a [ ]
Java array n elements Variable int n;
n=cin.nextInt();
int a[]=new int[n];
import java.util.Scanner;
public class InputArrayN{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(Enter n = );
n=cin.nextInt();
int a[]=new int[n];
System.out.println(Now, you can enter +n+ values);
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 12 -
System.out.print(Enter a[+i+] = );
a[i] = cin.nextInt();
}
System.out.println(Now, you can display +n+ values);
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 13 -
import java.util.Scanner;
public class Main{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(Enter number of elements = );
N=cin.nextInt();
int a[]=new int[n];
System.out.println(Now, you can enter +n+ values);
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 14 -
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 15 -
System.out.println(Enter a[+i+] = );
boolean b=false;
int value;
value=cin.nextInt();
for(int j=0; j
Prepared by: Mr. HIEENG Sokh Hymns - 16 -
import java.util.Scanner;
public class InputArray2{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int r, c;
System.out.print(Enter rows = );
r=cin.nextInt();
System.out.print(Enter columns = );
c=cin.nextInt();
int a[][]=new int[r][c];
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 17 -
System.out.println(Enter a[+i+] = );
a[i]=cin.nextInt();
}
int temp[];
temp=a;
int m;
System.out.print(Enter m = );
m=cin.nextInt();
a=new int[n+m];
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 18 -
Chapter 4
Introduction to Class
1. Overview
Class Instances variables Methods Instances variables Methods Members Class
2. Class Fundamentals
2.1. The General form of Class
Class ClassName{
DataType InstanceVariable;
returnType methodName(arg){
//body
}
}
Class Rectangle{ Class
double w;
double n;
double parameter(){ Instance variable
return (w+n)*2;
}
double are(){
return w*n; Method
}
}
Instance variable Variable Class Variable Class variable class
Method Function Class Method Class
Prepared by: Mr. HIEENG Sokh Hymns - 19 -
2.2. Execute member of Class
Execute Member Class Object Class Object Member Class
Form:
ClassName ObjectName = New ClassName();
Retangle obj = new Rectangle();
public class Main{
public static void main(string arg[]){
Rectangle obj=new Rectangle();
obj.h = 10;
obj.w = 20;
System.out.println(Parameter = +obj.parameter());
System.out.println(Area = +obj.area());
}
}
3. Method
3.1. Method has return type Method return type double, long, int, ... Method return statement
3.2. Method has not return type Method return type void Method return statement
public class SubProgram{
double max(double a, double b){
if(a>b)
return a;
else
return b;
}
void showStars(int n){
for(int i=1; i
Prepared by: Mr. HIEENG Sokh Hymns - 20 -
Practice
User Interface ( Code + Design) 1. Right-Click on Package new Jframe Form Finish 2. Click F6 to run form
3. Click Palette to Design form
UserName:
Password:
4. Click on Frame Property Code
5. Exit (cmdExit) System.exit(0); ( exit ) 6. Font click on Label Property Font 7. Logo : Label Right-Click Property Icon 8. Background Frame Label Frame
Menu bar (Open Project D:\SH5) 1. Right-Click on Package New JFrame Form Finish 2. Palette ( Tool) Swing Menu Menu bar Project 3. Right-Click on Menu Edit Text Variable Name (cmdFile, cmdEdit, ...)
4. Code Exit Right-Click on Exit Event 5. Menu Right-Click on Mouse Menu
Add Menu
6. File Menu Menu Add from palette Menu Item
7. Code Exit Right-Click on Exit Event Action Performed cmdExit System.exit(0);
8. File, Edit . Menu Menu item Navigate Inspect File Member
txtUsername
txtPassword
Bottom Log In Exit
Prepared by: Mr. HIEENG Sokh Hymns - 21 -
19/Feb/2011
4. Constructors
Method Class Method execute Object Class Constructor Initialize Member Class
Create object
ClassName ObjectName = New Constructor();
Rectangle Object = new Constructor(); Member Class
public class Boxes{
double w, h, d;
public Boxes(){
w = 10;
h = 10;
d = 10;
}
double area(){
return w*h*d;
}
}
public class MainBox{
public static void main(String arg[]){
Boxes obj=new Boxes();
System.out.println (width = +obj.w);
System.out.println (height = +obj.h);
System.out.println (depth = +obj.d);
System.out.println (area = +obj.area());
}
}
Prepared by: Mr. HIEENG Sokh Hymns - 22 -
Overloaded constructors: Constructor Class Constructors Argument DataType Arguments
public class Boxes{
double w, h, d;
public Boxes(){
w = 10;
h = 10;
d = 10;
}
public Boxes(double width, double height, double depth){
w = width;
h = height;
d = depth;
}
double area(){
return w*h*d;
}
}
public class MainBox{
public static void main(String arg[]){
Boxes obj1=new Boxes();
System.out.println (width = +obj1.w);
System.out.println (height = +obj1.h);
System.out.println (depth = +obj1.d);
System.out.println (area = +obj1.area());
System.out.println (.);
Boxes obj2=new Boxes(5,5,5);
System.out.println (width = +obj2.w);
System.out.println (height = +obj2.h);
System.out.println (depth = +obj2.d);
System.out.println (area = +obj2.area());
}
}
Prepared by: Mr. HIEENG Sokh Hymns - 23 -
Overloaded constructor initialize Object
5. this keyword: keyword Current object Member Class
public class Boxes{
double w, h, d;
public Boxes(double w, double h, double d){
this.w = w;
this.h = h;
this.d = d;
}
}
6. Object and Array
object array array object constructor
Boxes obj1;
obj1.w = 6 // cant use
Boxes obj[] = new Boxes[2];
// obj[0].w = 1; cant use
obj[0] = new Boxes();
obj[1] = new Boxes(5,5,5);
obj[0].w = 1; // can use
public class Student{
long id;
string name;
char gender;
string address;
Prepared by: Mr. HIEENG Sokh Hymns - 24 -
public Student(long id, string name, char gender, string address){
this.id = id;
this.name = name;
this.gender = gender;
this.address = address;
}
}
import java.util.Scanner;
public class TestArray{
public static void mani(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(Enter n = );
n=cin.nextInt();
Student obj[]=new Student[n];
long id; string name; char gender; string address;
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 25 -
System.out.print(obj[i].address + \t);
System.out.println(____________________);
}
}
}
12 March, 2011
Object Array Record Records Records Field
ID long PName String Qty long UPrice float Amount double (Not input by user)
public class Product{
long id;
string pname;
long qty;
float uprice;
double amount;
public Product(long id, string pname, long qty, float uprice){
this.id = id;
this.pname = pname;
this.qty = qty;
this.uprice = uprice;
this. amount = qty*uprice;
}
}
Prepared by: Mr. HIEENG Sokh Hymns - 26 -
Import jave.util.Scanner;
Public class MainProduct{
Public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(Enter number of records = );
n=cin.nextInt();
Product obj[]=new Product[n];
long id; string pname; long qty; float uprice;
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 27 -
Homework:
N Records Students Records Field
ID long SName String Gender String Address String Year int Class String Java float Acc float C++ float AVG double (Not input by user) AVG () public class Student{
long id;
string sname;
string gender;
string address;
int year;
string class;
float java, acc, cpp;
double avg(){
return(java+acc+cpp)/3;
}
Student copy(){
Student temp=new Student(id, sname, gender, address, year, Class, java, acc,
cpp);
return temp;
}
public Student(long id, string sname, string gender, string address, int year, string
class, float java, float acc, float cpp){
this.id = id;
Prepared by: Mr. HIEENG Sokh Hymns - 28 -
this.sname = sname;
this.gender = gender;
this.address = address;
this.year = year;
this.class = class;
this.java = java;
this.acc = acc;
this.cpp=cpp;
}
}
import java.text.DecimalFormat;
import java.util Scanner;
pubic class ArrayObj{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(Enter number of Records = );
n=cin.nextInt();
Student obj[]=new Student[n];
long id; string sname; string gender; string address, int year; string class; float
java; float acc; float cpp;
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 29 -
System.out.print(Enter CLass = );
cin.nextLine();
CLass=cin.nextLine();
System.out.print(Enter Java = );
java=cin.nextFloat();
System.out.print(Enter Access =);
acc=cin.nextFloat();
System.out.print(Enter C++ = );
cpp = cin.nextFloat();
obj[i]=new Student(id, sname, gender, year, class, java, acc, cpp);
System.out.println(AVG = +obj[i].avg);
System.out.println(______________________________________\n);
}
System.out.println(\n__________________________________________\n);
System.out.println(ID\tStudentName\t\tGender\tYear\tClass\tJava\tAcc
\tC++\tAVG\n);
for(int i=0; i
Prepared by: Mr. HIEENG Sokh Hymns - 30 -
System.out.print(df.format(obj[i].avg())+ \t);
System.out.print(\n______________________________________\n);
}
}
}
20/March/2011
Referenct Object
Object Object Object Reference Referent Object Object Object Address Object Object
Boxes obj1=new Boxes();
Boxes obj2=new Boxes(5,5,5);
Obj1=obj2; // Referent Object
Referent Object Object Object Initialize Constructor Object Referent Object
Return Object
Method Return Type Class Return value Object Class Return Object Object Object Object Referent Object public class Boxes{
double w;
double h;
double d;
Boxes copy(){ // Return object
Boxes temp=new Boxes(w, h, d);
Return temp;
}
Prepared by: Mr. HIEENG Sokh Hymns - 31 -
public Boxes(){
w=10;
h=10;
d=10;
}
public Boxes(double w, double h, double d){
this.w=w;
this.h=h;
this.d=d;
}
double area(){
return w*h*d;
}
}
public class MainBox{
public static void main(String arg[]){
Boxes obj1=new Boxes();
Boxes obj2=new Boxes(5,5,5);
// obj1=obj2; // Referent Object
/*
obj1.d=obj2.d;
obj1.h=obj2.h; obj1.w=obj2.w; */
obj1.d=obj2.copy(); // Not Referent Object
obj2.d=1;
obj2.h=1;
obj2.w=1;
System.out.println(Width = +obj1.w);
System.out.println(Height = +obj1.h);
System.out.println(Depth = +obj1.d);
System.out.println(_______________________);
}
}
Prepared by: Mr. HIEENG Sokh Hymns - 32 -
Runnable Class Runnable
Thread Class Runnable Runnable
7. ArrayList Class Package Java.util.ArrayList n records ArrayObject
Form:
ArrayList obj=new ArrayList();
Ex: // ArrayList
ArrayList obj=new ArrayList();
Ex: // ArrayObject
Student obj[]=new Student[n];
Method in ArrayList
boolean add(Object record); Method add object record ArrayList Record add Record Index Record add index
object set(int index, object record); Method Record index ArrayList ( 1 Record)
object get(int index); Method Record ArrayList Index
int size(); Method Record ArrayList boolean remove(object record); Method Remove record ArrayList Record
Prepared by: Mr. HIEENG Sokh Hymns - 33 -
Chapter 5
More on Class
Method Arguments Objects Arguments Parameters Objects
Sub-program
public class Product{
long id;
string pname;
long qty;
float uprice;
double amount(){
return qty*uprice;
}
boolean equal(Product obj){
if(obj.id==id&&obj.paname.equals(pname)&&obj.qty==qty&&obj.uprice==
uprice)
return true;
else
return false;
}
public Product(long id, string pname, long qty, float uprice){
this.id=id;
this.pname=pname;
this.qty=qty;
this.uprice=uprice;
}
}
Main-program
public class Mproduct{
public static void main(String[] args){
Product obj1=new Product(1, ABC, 200, 23);
Product obj2=new Product(1, ABC, 200, 23);
Prepared by: Mr. HIEENG Sokh Hymns - 34 -
if(obj1.equal(obj2))
System.out.println(The same record.);
else
System.out.println(Not the same record.);
}
}
1. Overview 2. Parameter passing
Parameter Arguments Pass by value Pass by reference 1.1. Pass by value ( C++)
Sub-program
public class Data{
void changeValue(int a, int b){
a+=2;
b+=4;
}
}
Main-program
public class main{
public static void main(String[] args){
int a, b;
a=10;
b=10;
obj.changeValue(a, b); // Pass by value
System.out.println(A = +a);
System.out.println(B = +b);
}
}
1.2. Pass by reference ( C++) Sub-program
public class Data{
int a, b;
void changeValue(Data obj){
Prepared by: Mr. HIEENG Sokh Hymns - 35 -
obj.a+=2;
obj.b+=4;
}
}
Main-program
public class main{
public static void main(String[] args){
Data obj=new Data();
obj.a=10;
obj.b=10;
obj.changeValue(obj); // Pass by reference
System.out.println(A = +obj.a);
System.out.println(B = +obj.b);
}
}
Parameter Variable Pass by value Parameter object Array Pass by reference
3. Static keyword ( C++) Keyword Members Class Member Address
Address Object
Sub-program
public class Data{
static int a;
int b;
}
Main-program
class Main{
public static void main(String[] args){
Data obj1=new Data();
Prepared by: Mr. HIEENG Sokh Hymns - 36 -
// All members can use object(Static or non-static)
obj1.a=1;
obj2.b=1;
// Static members can use without object
Data.a=10;
// Non-static member cannot use without object
// Data.b=10;
Data obj2=new Data();
// All objects use the same static members
Obj2.a=20;
System.out.println(A = +obj1.a);
System.out.println(A = +obj2.a);
System.out.println(A = +Data.a);
}
}
Static keyword Global variable Global variable Java Variable Class Class Package Package
Prepared by: Mr. HIEENG Sokh Hymns - 37 -
Chapter 6
Inheritance
1. What is inheritance?
Inheritance Class Class SuperClass Class Member Class SubClass Class Member Class
2. Inheritance
Form: public inheritance
class SubClass entends SuperClass{
// Member SubClass
}
class SuperClass{
int a, b;
}
class Sub extends SuperClass{
int c;
}
3. Using super keyword 3.1. super keyword call Constructors of SuperClass
class SuperClass{
int a, b;
public SuperClass(int a, int b){
this.a=a;
this.b=b;
}
}
class Sub extends SuperClass{
int c;
public Sub(int a, int b, int c){
Prepared by: Mr. HIEENG Sokh Hymns - 38 -
Super(a, b);
this.c=c;
}
}
3.2. super keyword call Member SuperClass
class SuperClass{
int a, b;
void show(){
System.out.println(A = +a);
System.out.println(B = +b);
}
public SuperClass(int a, int b){
this.a=a;
this.b=b;
}
}
class Sub extends SuperClass{
int c;
void show(){
System.out.println(C = +c);
}
public Sub(int a, int b, int c){
Super(a, b);
this.c=c;
}
}
4. Method overriding
Overloaded method Method SubClass Arguments Method SuperClass
Prepared by: Mr. HIEENG Sokh Hymns - 39 -
class Star{
void show(){
for(int i=1; i
Prepared by: Mr. HIEENG Sokh Hymns - 40 -
obj.show();
obj.show(10);
obj.show(20, @);
}
}
Overloaded VS Override
1. Method Argu-ments DataType Arguments
2. Overloaded method Object Class Method Arguments
3. Overloaded Method SuperClass SubClass
1. Method Arguments DataType Arguments
2. Overrided Method Object Class Method Class
3. Overrided method SubClass
08 May 2011
5. Abstract class
Abstract class class method Abstract class can refer with other SubClass
abstract class AbSimple{
void show(){
System.out.println(You can call me.);
}
abstract void show(String txt);
}
class Sub extends AbSimple{
void show(String txt){
System.out.println(txt);
}
Prepared by: Mr. HIEENG Sokh Hymns - 41 -
}
public class Main{
public static void main(String[] args){
AbSimple obj=new AbSimple(){
// @override
void show(String txt){
Systme.out.println(txt);
}
};
obj.show(Just override);
Sub obj2=new Sub();
Obj2.show();
}
6. Using final keyword
6.1. Using final keyword to prevent overriding
abstract class AbSimple{
abstract void show(String txt); // Must override
final void show(){
System.out.println(Cannot override at SubClass.);
}
void show (String txt, int n){
System.out.println(txt+ +n);
System.out.println(Can or cannot override.);
}
}
class Sub extends AbSimple{
void show(String txt){
System.out.println(txt);
}
}
public class Main{
public static void main(String[] args){
Prepared by: Mr. HIEENG Sokh Hymns - 42 -
AbSimple obj=new AbSimple(){
// @override
void show(String txt){
Systme.out.println(txt);
}
};
obj.show(Just override);
Sub obj2=new Sub();
Obj2.show();
}