Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

PRACTICLE 1

Programme
Class Example { Public static void main(String args[]) { System.out.println(This is a simple java program.); } }

PRACTICLE 2

Programme

class Example2 { public static void main(String args[]) { int num; num = 100; System.out.println("This is num: " + num); num = num * 2; System.out.print("The value of num * 2 is "); System.out.println(num); } }

Programme
class IfSample { public static void main(String args[]) {

int x, y; x = 10; y = 20; if(x < y) System.out.println("x is less than y"); x = x * 2; if(x == y) System.out.println("x now equal to y"); x = x * 2; if(x > y) System.out.println("x now greater than y"); (x == y) System.out.println("you won't see this"); } } class CharDemo { public static void main(String args[]) { char ch1, ch2; ch1 = 88; ch2 = 'Y'; System.out.print("ch1 and ch2: "); System.out.println(ch1 + " " + ch2); } } PRACTICLE 3

Programme

class Light { public static void main(String args[]) {

int lightspeed; long days; long seconds; long distance; lightspeed = 186000; days = 1000; seconds = days * 24 * 60 * 60; distance = lightspeed * seconds; System.out.print("In " + days); System.out.print(" days light will travel about "); System.out.println(distance + " miles."); } }

Programme
class BoolTest { public static void main(String args[]) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); if(b) System.out.println("This is executed."); b = false; if(b) System.out.println("This is not executed.");

System.out.println("10 > 9 is " + (10 > 9)); } }

class DynInit { public static void main(String args[]) { double a = 3.0, b = 4.0; double c = Math.sqrt(a * a + b * b); System.out.println("Hypotenuse is " + c); } }

Programme

class LifeTime { public static void main(String args[]) { int x; for(x = 0; x < 3; x++) { int y = -1; System.out.println("y is: " + y); y = 100; System.out.println("y is now: " + y); } } }

class Conversion { public static void main(String args[]) { byte b; int i = 257; double d = 323.142; System.out.println("\\nConversion of int to byte."); b = (byte) i; System.out.println("i and b " + i + " " + b); System.out.println("\\nConversion of double to int."); i = (int) d; System.out.println("d and i " + d + " " + i); System.out.println("\\nConversion of double to byte."); b = (byte) d; System.out.println("d and b " + d + " " + b);

Programme

class Promote { public static void main(String args[]) { byte b = 42; char c = 'a'; short s = 1024;

int i = 50000; float f = 5.67f; double d = .1234; double result = (f * b) + (i / c) - (d * s); System.out.println((f * b) + " + " + (i / c) + " - " + (d *s)); System.out.println("result = " + result); } } PRACRICLE 4

Programme
class Array { public static void main(String args[]) { int month_days[]; month_days = new int[12]; month_days[0] = 31; month_days[1] = 28; month_days[2] = 31; month_days[3] = 30;

month_days[4] = 31; month_days[5] = 30; month_days[6] = 31; month_days[7] = 31; month_days[8] = 30; month_days[9] = 31;

month_days[10] = 30; month_days[11] = 31; System.out.println("April has " + month_days[3] + " days."); } }

Programme class BasicMath { public static void main(String args[]) { System.out.println("Integer Arithmetic"); int a = 1 + 1; int b = a * 3; int c = b / 4; int d = c - a; int e = -d; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); System.out.println("\\nFloating Point Arithmetic"); double da = 1 + 1; double db = da * 3; double dc = db / 4; double dd = dc - a;

double de = -dd; System.out.println("da = " + da); System.out.println("db = " + db); System.out.println("dc = " + dc); System.out.println("dd = " + dd); System.out.println("de = " + de); } }

Programme
class Modulus { public static void main(String args[]) { int x = 42; double y = 42.3; System.out.println("x mod 10 = " + x % 10); System.out.println("y mod 10 = " + y % 10); } }

Programme
class IncDec { public static void main(String args[]) { int a = 1; int b = 2;

int c; int d; c = ++b; d = a++; c++; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } }

Programme
class ByteShift { public static void main(String args[]) { byte a = 64, b; int i; i = a << 2; b = (byte) (a << 2); System.out.println("Original value of a: " + a); System.out.println("i and b: " + i + " " + b); } }

Programme
class BoolLogic { public static void main(String args[]) {

boolean a = true; boolean b = false; boolean c = a | b; boolean d = a & b; boolean e = a ^ b; boolean f = (!a & b) | (a & !b); boolean g = !a; System.out.println(" a = " + a); System.out.println(" b = " + b); System.out.println(" a|b = " + c); System.out.println(" a&b = " + d); System.out.println(" a^b = " + e); System.out.println("!a&b|a&!b = " + f); System.out.println(" !a = " + g); } }

Programme
class Ternary { public static void main(String args[]) { int i, k; i = 10; k = i < 0 ? -i : i; // get absolute value of i System.out.print("Absolute value of "); System.out.println(i + " is " + k);

i = -10; k = i < 0 ? -i : i; // get absolute value of i System.out.print("Absolute value of "); System.out.println(i + " is " + k); } }

Programme
class IfElse { public static void main(String args[]) { int month = 4; // April String season; if(month == 12 || month == 1 || month == 2) season = "Winter"; else if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); }

} PRACTICLE 5

Programme
class SampleSwitch { public static void main(String args[]) { for(int i=0; i<6; i++) switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break; case 3: System.out.println("i is three."); break; default: System.out.println("i is greater than 3."); } } }

Programme
class Nested { public static void main(String args[]) { int i, j; for(i=0; i<10; i++) { for(j=i; j<10; j++) System.out.print("."); System.out.println(); } } }

Programme
class Box { double width; double height; double depth;

class BoxDemo { public static void main(String args[]) { Box mybox = new Box(); double vol; mybox.width = 10; mybox.height = 20; mybox.depth = 15;

vol = mybox.width * mybox.height * mybox.depth; System.out.println("Volume is " + vol); } }

Programe
class Box { double width; double height; double depth; void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); } } Class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; mybox1.volume();

mybox2.volume(); } }

Programe
class Box { double width; double height; double depth; double volume() { return width * height * depth; } }

class BoxDemo4 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9;

vol = mybox1.volume(); System.out.println("Volume is " + vol); vol = mybox2.volume(); System.out.println("Volume is " + vol); } }

PRACTICLE 6 CONSTRUCTORS

Programe
class Box { double width; double height; double depth; Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } double volume() { return width * height * depth; } }

class BoxDemo6 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; vol = mybox1.volume(); System.out.println("Volume is " + vol); vol = mybox2.volume(); System.out.println("Volume is " + vol); } }

PRACTICLE 7 PARAMETERIZED CONSTRUCTORS

Programe
class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; }

double volume() { return width * height * depth; } } class BoxDemo7 { public static void main(String args[]) { Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(3, 6, 9); double vol; vol = mybox1.volume(); System.out.println("Volume is " + vol); vol = mybox2.volume(); System.out.println("Volume is " + vol); } } PRACTICLE 8

Programe
class Stack { int stck[] = new int[10]; int tos; Stack() { tos = -1; } void push(int item) { if(tos==9)

System.out.println("Stack is full."); else stck[++tos] = item; } int pop() { if(tos < 0) { System.out.println("Stack underflow."); return 0; } else return stck[tos]; } }

OVERLOADING METHODS

Programe
class OverloadDemo { void test() { System.out.println("No parameters"); } void test(int a) { System.out.println("a: " + a); } void test(int a, int b) {

System.out.println("a and b: " + a + " " + b); } double test(double a) { System.out.println("double a: " + a); return a*a; } }

class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.2); System.out.println("Result of ob.test(123.2): " + result); } }

PRACTICLE 9 OVERLOADING CONSTRUCTORS

Programe
class Box {

double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } Box() { width = -1; height = -1; depth = -1; } Box(double len) { width = height = depth = len; } double volume() { return width * height * depth; } } class OverloadCons { public static void main(String args[]) { Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7);

double vol; vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); } }

Programe
class Box { double width; double height; double depth; Box(Box ob) { width = ob.width; height = ob.height; depth = ob.depth; } Box(double w, double h, double d) { width = w; height = h; depth = d; }

Box() { width = -1; height = -1; depth = -1; } Box(double len) { width = height = depth = len; } double volume() { return width * height * depth; } } class OverloadCons2 { Public static void main(String args[]) { Box s mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); Box myclone = new Box(mybox1); double vol; vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); vol = mycube.volume(); System.out.println("Volume of cube is " + vol);

vol = myclone.volume(); System.out.println("Volume of clone is " + vol); } }

PRACTICLE 10 RECURSION

Programe
class Factorial { int fact(int n) { int result; if(n==1) return 1; result = fact(n-1) * n; return result; } } class Recursion { public static void main(String args[]) { Factorial f = new Factorial(); System.out.println("Factorial of is " + f.fact(3)); System.out.println("Factorial of 4 is " + f.fact(4)); System.out.println("Factorial of 5 is " + f.fact(5)); } }

PRACTICLE 11 UNDERSTANDING STATIC

Programe
class UseStatic { static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); } }

NESTED & INNER CLASSES

Programe
class Outer {

int outer_x = 100; void test() { Inner inner = new Inner(); inner.display(); } class Inner { int y = 10; void display() { System.out.println("display: outer_x = " + outer_x); } }

class InnerClassDemo { public static void main(String args[]) { Outer outer = new Outer(); outer.test(); } } INHERITANCE

Programe
class Box { double width; double height; double depth; Box(Box ob) {

width = ob.width; height = ob.height; depth = ob.depth; } Box(double w, double h, double d) { width = w; height = h; depth = d; }

Box() { width = -1; height = -1; depth = -1; } Box(double len) { width = height = depth = len; } double volume() { return width * height * depth; } } class BoxWeight extends Box { double weight; BoxWeight(double w, double h, double d, double m) {

width = w; height = h; depth = d; weight = m; } }

Class DemoBoxWeight { public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076); double vol; vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.weight); System.out.println(); vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); System.out.println("Weight of mybox2 is " + mybox2.weight); } }

WHEN CONSTRUCTORS ARE CALLED

Programe

class A { A() { System.out.println("Inside A's constructor."); } } class B extends A { B() { System.out.println("Inside B's constructor."); } } class C extends B { C() { System.out.println("Inside C's constructor."); } }

class CallingCons { public static void main(String args[]) { C c = new C(); } }

PRACTICLE 12 METHOD OVERRIDING

Programe

class A { int i, j; A(int a, int b) { i = a; j = b; } void show() { System.out.println("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } void show() { System.out.println("k: " + k); } }

class Override { public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show();

} }

ABSTRACT CLASSES

Programe
abstract class A { abstract void callme(); void callmetoo() { System.out.println("This is a concrete method."); } } class B extends A { void callme() { System.out.println("B's implementation of callme."); } }

class AbstractDemo { public static void main(String args[]) { B b = new B(); b.callme(); b.callmetoo(); } } PRACTICLE 13

PACKAGES

Programe
package MyPack; class Balance { String name; double bal; Balance(String n, double b) { name = n; bal = b; } void show() { if(bal<0) System.out.print("> "); System.out.println(name + ": $" + bal); } }

class AccountBalance { public static void main(String args[]) { Balance current[] = new Balance[3]; current[0] = new Balance("K. J. Fielding", 123.23); current[1] = new Balance("Will Tell", 157.02); current[2] = new Balance("Tom Jackson", -12.33); for(int i=0; i<3; i++) current[i].show(); }

PRACTICLE 14 EXCEPTION HANDLING

Programe
class Exc2 { public static void main(String args[]) { int d, a; try { d = 0; a = 42 / d; System.out.println("This will not be printed."); } catch (ArithmeticException e) { System.out.println("Division by Zero."); } System.out.println("After catch statement."); } }

class MultiCatch { public static void main(String args[]) { try { int a = args.length; System.out.println("a = " + a);

int b = 42 / a; int c[] = { 1 }; c[42] = 99; } catch(ArithmeticException e) { System.out.println("Divide by 0: " + e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index oob: " + e); } System.out.println("After try/catch blocks."); } }

THROW

Programe
class ThrowDemo { static void demoproc() { try { throw new NullPointerException("demo"); } catch(NullPointerException e) { System.out.println("Caught inside demoproc."); throw e; } } public static void main(String args[]) {

try { demoproc(); } catch(NullPointerException e) { System.out.println("Recaught: " + e) } } }

PRACTICLE15 MULTITHREADING

Programe
class CurrentThreadDemo { public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println("Current thread: " + t); t.setName("My Thread"); System.out.println("After name change: " + t); try { for(int n = 5; n > 0; n) { System.out.println(n); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted"); }

} }

Programe
class NewThread implements Runnable { Thread t; NewThread() { t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); } public void run() { try { for(int i = 5; i > 0; i) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } class ThreadDemo { public static void main(String args[]) {

new NewThread(); // create a new thread

try { for(int i = 5; i > 0; i) { Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); } }

isAlive() & Join

Programe
class NewThread implements Runnable { String name; Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); } public void run() {

try { for(int i = 5; i > 0; i) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); } }

Class DemoJoin { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two"); NewThread ob3 = new NewThread("Three"); System.out.println("Thread One is alive: "+ ob1.t.isAlive()); System.out.println("Thread Two is alive: "+ ob2.t.isAlive()); System.out.println("Thread Three is alive: "+ ob3.t.isAlive()); try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); ob3.t.join();

} catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Thread One is alive: "+ ob1.t.isAlive()); System.out.println("Thread Two is alive: "+ ob2.t.isAlive()); System.out.println("Thread Three is alive: "+ ob3.t.isAlive()); System.out.println("Main thread exiting."); } } SYNCHRONIZATION

Programe
class Callme { void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); } }

class Caller implements Runnable { String msg;

Callme target; Thread t; public Caller(Callme targ, String s) { target = targ; msg = s; t = new Thread(this); t.start(); } public void run() { target.call(msg); } } class Synch { public static void main(String args[]) { Callme target = new Callme(); Caller ob1 = new Caller(target, "Hello"); Caller ob2 = new Caller(target, "Synchronized"); Caller ob3 = new Caller(target, "World"); try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); }

} }

INTERTHREAD COMMUNICATION

Programe
class Q { int n; synchronized int get() { System.out.println("Got: " + n); return n; } synchronized void put(int n) { this.n = n; System.out.println("Put: " + n); } } class Producer implements Runnable { Q q; Producer(Q q) { this.q = q; new Thread(this, "Producer").start(); } public void run() { int i = 0; while(true) {

q.put(i++); } } }

class Consumer implements Runnable { Q q; Consumer(Q q) { this.q = q; new Thread(this, "Consumer").start(); } public void run() { while(true) { q.get(); } } }

class PC { public static void main(String args[]) { Q q = new Q(); new Producer(q); new Consumer(q); System.out.println("Press Control-C to stop."); }

} PRACTICLE 16 AWT

Programe
import java.awt.*; public class Example1 extends Frame {

public Example1() {

setBackground(Color.blue); setSize(300,400); setVisible(true); } public static void main(String[] args) { Example1 ex = new Example1(); } }

Programe
import java.awt.*; public class Example2 extends Frame { public Example2() { super("Tile: Testing Lables"); setLayout(new FlowLayout()); Label lable1 = new Label("GUI Lecture"); add(lable1); Label label2 = new Label("Which is a heavy one!");

add(label2); setSize(300,400); setVisible(true); } public static void main(String[] args) { Example2 ex = new Example2(); } }

LAB TASKS}

Problem 3: public class VariablesInJava{ public static void main(String args[]){ String $name="Waqar"; short rollno=036; int prev_semester_obtained=4000; long prev_semester_total=5000; double prev_semester_percentage= (prev_semester_obtained*100)/prev_semester_total;

char grade=(prev_semester_percentage>=50)?'A':'B'; boolean status=(grade=='A')?true:false; System.out.println("Name: "+$name + "\n" + "Roll NO: " + rollno + "\n Previous Semester Total: " + prev_semester_total + "\n Previous Semester Obtained: " + prev_semester_obtained + "\n Previous Semester Percentage: " + prev_semester_percentage + "\n Grade: " + grade + "\n Pass? " + status); } }

Problem 4:

public class ByteDemonstration{ public static void main(String args[]){ byte var=1; for(int i=0;i<=7;i++){ var*=2; System.out.println(var); } System.out.println("Reason for last display: \nByte can store numbers from -128 to 127 and after last muitiplication value becomes 256 that can not be store in byte type variable."); } } Reason:- Byte can store numbers from -128 to 127 and after last muitiplication value becomes 256 that can not be store in byte type variable. Problem 5:

public class DoubleDemonstration{ public static void main(String args[]){ double var=1234.6789; long integral=(int)var; System.out.println( integral ); System.out.println( var-integral ); } }

Problem 6:

public class StaticNonStatic{ static int var1=10; int var2=2; public static void main(String args[]){ System.out.println(var1); System.out.println(var2); } } Reason:There is a compile time error because non-static variable can not be access by static context.

LAB TASK 2 Problem 1: public class CmdArguments{ public static void main(String args[]){ if(args.length>0){ int var1=Integer.parseInt(args[0]); int var2=Integer.parseInt(args[1]); int var3=Integer.parseInt(args[2]); int sum=var1+var2+var3; int multiplication=var1*var2*var3; System.out.println(sum); System.out.println(multiplication);

} else{ System.out.println("Enter Numbers"); } } } Problem 2: public class ExpressionEvaluation{ public static void main(String args[]){ byte b=32; char c='z'; short s=256; int i=1000; float f=3.5f; double d=0.5; System.out.println((d*i)+(f*(-b))-(c/s)); } }

Problem 3: public class LeftRightShift{ public static void main(String args[]){ int var=20; System.out.println(var<<2); System.out.println(var>>2);

} } Problem 4: public class ShortCircuit{ public static void main(String args[]){ boolean var1=(boolean)1; boolean var2=0; System.out.println((var1 & !var2) | (!var & var2)); } } Problem 5:

public class MyFirstProgram{ public static void main(String args []){ int a = 10, b = 33, c = 6700; System.out.println(Addition of a, b & c + (a + b + c)); a = 97; System.out.println(Addition of a, b & c + (a + b + c)); } } Reason: A single variable can not ne publically initialized in a single class.

Problem 6: int k = 1; int i = ++k + k++ + ++k; Result: Value of I will be 8

Reason: Variable i is assigning a value by adding three expressions. Expression 1: ++k

Its a pre increment in k, so the value of first expression is set to 2, as well as the value of k is 2.

Expression 2: k++ Its a post increment in k, so the value of 2nd expression is set to 2, then the value of k will be incremented to 3. Expression 3: ++k Its a pre increment in k, so the value of 3rd expression is set to 4. Now i=2+2+4 So, i=8

THE END

You might also like