Programming in Java
5th Semester
Course Code: 66651
─
Md. Nazmul Islam
Instructor, CMT
National Institute of Engineering and Technology
Mobile: 0191-6157490
1
# Class, object & constructor ব াবহার কের জাভা া াম
public class A {
public int a;
public int b;
public static void main(String[] args) {
// TODO code application logic here
A xyz = new A();
xyz.a = 10;
xyz.b = 20;
System.out.println(xyz.a);
System.out.println(xyz.b);
}
}
# Copy Constructor ব াবহার কের জাভা া াম
public class A {
public int a;
public int b;
A()
{
a = 0;
b = 0;
}
Programming in Java Md. Nazmul Islam
2
A(int x)
{
a = x;
}
A(int x, int y)
{
a = x;
b = y;
}
public static void main(String[] args)
{
A xyz = new A();
System.out.println(xyz.a);
System.out.println(xyz.b);
A abc = new A(7);
System.out.println(abc.a);
System.out.println(abc.b);
A def = new A(7,5);
System.out.println(def.a);
System.out.println(def.b);
}
}
Programming in Java Md. Nazmul Islam
3
# Single inheritance ব াবহার কের জাভা া াম
Or Super Class, Sub Class ব াবহার কের জাভা া াম
public class A { //super class
public int a;
public int b;
public int sum(int x, int y)
{
return x+y;
}
}
public class B extends A{ //sub class
public int c;
public int d;
public int multi(int x, int y)
{
return x*y;
}
public int sum(int x, int y, int z)
{
return x+y+z;
}
public static void main(String[] args) {
Programming in Java Md. Nazmul Islam
4
B xyz = new B();
xyz.a = 100;
xyz.b = 200;
xyz.c = 300;
xyz.d = 400;
System.out.println(xyz.sum(10, 20));
System.out.println(xyz.sum(10, 20, 40));
System.out.println(xyz.multi(10, 20));
System.out.println(xyz.a);
System.out.println(xyz.b);
System.out.println(xyz.c);
System.out.println(xyz.d);
}
}
# Multi-level inheritance ব াবহার কের জাভা া াম
import static java.lang.Math.pow;
public class A {
public int a;
public int b;
Programming in Java Md. Nazmul Islam
5
public int sum(int x, int y)
{
return x+y;
}
}
public class B extends A {
public int c;
public int d;
public int multi(int x, int y)
{
return x*y;
}
}
public class C extends B {
public int e;
public int f;
public double power(int x, int y)
{
return pow(x, y);
}
public static void main(String[] args) {
C xyz = new C();
xyz.a = 100;
xyz.b = 200;
xyz.c = 300;
Programming in Java Md. Nazmul Islam
6
xyz.d = 400;
xyz.e = 500;
xyz.f = 600;
System.out.println(xyz.sum(10, 20));
System.out.println(xyz.multi(10, 20));
System.out.println(xyz.power(10, 20));
System.out.println(xyz.a);
System.out.println(xyz.b);
System.out.println(xyz.c);
System.out.println(xyz.d);
System.out.println(xyz.e);
System.out.println(xyz.f);
}
}
# Polymorphism ব াবহার কের জাভা া াম
Or Method Overloading ব াবহার কের জাভা া াম
public class A {
public int sum(int x, int y)
{
return x+y;
}
}
Programming in Java Md. Nazmul Islam
7
public class B extends A {
public int sum(int x, int y, int z)
{
return x+y+z;
}
}
public class C extends B {
public double sum(double x, double y)
{
return x+y;
}
public static void main(String[] args) {
C xyz = new C();
System.out.println(xyz.sum(10, 20));
System.out.println(xyz.sum(10, 20, 40));
System.out.println(xyz.sum(10.74, 9.8));
}
}
# Thread ব াবহার কের জাভা া াম / িকভােব ড তির করা যায়
Thread মূলত দুইভােব তির করা যায়,
১. Interface ব াবহার কের
২. inheritance ব াবহার কের
Programming in Java Md. Nazmul Islam
8
// Interface ব াবহার কের
public class B implements Runnable {
public void run()
{
System.out.println("Hello !! I am from class B");
for(int i=101; i<=200; i++)
{
System.out.println(i);
}
}
}
public class D {
public static void main(String[] args) {
B abc = new B();
Thread hij = new Thread(abc);
hij.start();
}
}
// inheritance ব াবহার কের
public class A extends Thread {
public void run()
{
System.out.println("Hello !! I am from class A");
for(int i=1; i<=100; i++)
{
System.out.println(i);
Programming in Java Md. Nazmul Islam
9
}
}
}
public class D {
public static void main(String[] args) {
A xyz = new A();
xyz.start();
}
}
# Multi - Thread ব াবহার কের জাভা া াম
public class A extends Thread {
public void run()
{
System.out.println("Hello !! I am from class A");
for(int i=1; i<=100; i++)
{
System.out.println(i);
}
}
}
public class B implements Runnable {
public void run()
{
System.out.println("Hello !! I am from class B");
Programming in Java Md. Nazmul Islam
10
for(int i=101; i<=200; i++)
{
System.out.println(i);
}
}
}
public class C extends Thread {
public void run()
{
System.out.println("Hello !! I amd from class C");
for(int i=300; i<=400; i++)
{
System.out.println(i);
}
}
}
public class D {
public static void main(String[] args) {
A xyz = new A();
xyz.start();
B abc = new B();
Thread hij = new Thread(abc);
hij.start();
Programming in Java Md. Nazmul Islam
11
C def = new C();
def.start();
}
}
# Thread Synchronization িকভােব কের, তা দিখেয় জাভা া াম
public class E {
synchronized void callSync()
{
System.out.println("Hello !! 5th Semester ...");
}
}
public class A extends Thread {
public void run()
{
E xyz = new E();
xyz.callSync();
System.out.println("I am from class A");
}
}
public class B extends Thread {
public void run()
{
Programming in Java Md. Nazmul Islam
12
E xyz = new E();
xyz.callSync();
System.out.println("I am from class B");
}
}
public class D {
public static void main(String[] args) {
A xyz = new A();
xyz.start();
B abc = new B();
abc.start();
}
}
# Interface ( ই ারেফজ ) ব াবহার কের জাভা া াম
Or Method Override ব াবহার কের জাভা া াম
public interface A {
public int sum(int x, int y);
public int multi(int x, int y);
}
Programming in Java Md. Nazmul Islam
13
public class B implements A {
public int multi(int x, int y)
{
return x*y;
}
public int sum(int x, int y)
{
return x+y;
}
public static void main(String[] args) {
B xyz = new B();
int a = xyz.sum(100, 200);
System.out.println(a);
int b = xyz.sum(100, 200);
System.out.println(b);
}
}
# ি ঘাত সমীকরেণর মূল িনণেয়র জন জাভা া াম
public class A {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
Programming in Java Md. Nazmul Islam
14
int c = input.nextInt();
double D, E;
D = -b + Math.sqrt( (b*b) - (4*a*c));
E = -b - Math.sqrt( (b*b) - (4*a*c));
System.out.println(D);
System.out.println(E);
}
}
# 1 থেক ১০০ পয জাড় সংখ া িনণেয়র জাভা া াম
public class A {
public static void main(String[] args) {
for(int n=1; n<=100; n++)
{
int x = n%2;
if(x == 0)
System.out.println(n+" is even number");
}
}
}
# 1 থেক ১০০ পয িবেজাড় সংখ া িনণেয়র জাভা া াম
public class A {
public static void main(String[] args) {
Programming in Java Md. Nazmul Islam
15
for(int n=1; n<=100; n++)
{
int x = n%2;
if(x == 1)
System.out.println(n+" is odd number");
}
}
}
# ি ভু েজর ফল িনণেয়র জাভা া াম
public class A {
public static void main(String[] args) {
int h, w;
h = 100;
w = 50;
double A = (1/2) * h * w;
System.out.println(A);
}
}
# ১+২+৩+-------------+n এর যাগফল িনণেয়র জাভা া াম
public class A {
public static void main(String[] args) {
int n = 100;
int x = (n * ( n + 1 ) )/2;
Programming in Java Md. Nazmul Islam
16
System.out.println(x);
}
}
অথবা
public class A {
public static void main(String[] args) {
int n = 100, sum = 0;
for(int i= 1; i<= n; i++)
{
sum = sum +i;
}
System.out.println(sum);
}
}
# ২+৪+৬+-------------+n এর যাগফল িনণেয়র জাভা া াম
public class A {
public static void main(String[] args) {
int n = 100;
int x = n * ( n + 1 );
System.out.println(x);
}
}
অথবা
Programming in Java Md. Nazmul Islam
17
public class A {
public static void main(String[] args) {
int n = 100, sum = 0;
for(int i= 1; i<= n; i++)
{
if(i%2 == 0)
{
sum = sum +i;
}
}
System.out.println(sum);
}
}
# জাভা িদেয় ফাইেলর ডাটা িরড এবং রাইট করার া াম
// ফাইেল িকছু লখার জন
public class A {
public static void main(String[] args) {
try{
String dir = "C:/Users/user/Desktop/NIET/Java All Assignments.txt";
FileReader fr = new FileReader(dir);
char ch = fr.read();
while( ch != null )
{
System.out.println(ch);
ch = fr.read();
}
Programming in Java Md. Nazmul Islam
18
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
// ফাইেল িকছু লখার জন
public class A {
public static void main(String[] args) {
try{
String dir = "C:/Users/user/Desktop/NIET/Java All Assignments.txt";
String content = "Hello !!! This is a file from java";
FilerWriter fr = new FileWriter(content);
fr.write();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
Programming in Java Md. Nazmul Islam
19
# জাভা িদেয় ডাটােবজ কােন করার া াম
public class A {
private Connection con;
private PreparedStatement pst;
private ResultSet rst;
public static void main(String[] args) {
try{
Class.forName("org.h2.Driver");
con = DriverManager.getConnection("jdbc:h2:~/db_name", "root","");
String sql ="select * from table_name";
pst= con.prepareStatement(sql);
rst = pst.executeQuery();
while(rst.next()){
System.out.println(rst.getString['name']);
System.out.println(rst.getString['mail']);
System.out.println(rst.getString['passord']);
}
rst.close();
pst.close()
con.close();
}
catch(Exception ex)
{
System.out.println(ex);
Programming in Java Md. Nazmul Islam
20
}
}
}
# Applet ব াবহার কের জাভা া াম
public class A extends Applet {
public void paint(Graphics g)
{
g.drawString("This is my 1st applet", 20, 30); // কান লখা দখােনার জন
g.setColor(red); // রং দােনর জন
g.setFont(BOLD); // ফ দােনর জন
}
}
HTML ফাইেল Applet যাগ করার জন িনেচর কাড িলখেত হয়
<applet code="A.class" width=200 height=300 >
</applet>
Programming in Java Md. Nazmul Islam