Archive

Posts Tagged ‘Java OO Programs’

Java Program to show Runnable Interface implementation – Q26

January 13, 2009 Leave a comment

Q26: Java Program to create thdemo class which implements Runnable Interface. Main thread prints numbers from 1 to 10 and child thread prints from 1 to 5.

A Runnable Interface should be implemented by any class whose instances are intended to be executed by a Thread. The class must define a method of no arguments called run.

class NewThread implements Runnable{
	
	Thread t;
	
	NewThread(){
		t = new Thread(this, "Demo Thread");
		System.out.println("Child thread: " + this);
		t.start();
	}
	
	public void run(){
		try{
			for(int i=1; i<=5; i++){
				System.out.println("Child Thread: " + i);
				System.out.println("Child Thread interrupted.");
				Thread.sleep(500);
			}
		}
		catch(InterruptedException e){
			System.out.println("Exiting child thread");
		}
	}
}	

class demo_thread_runnable{

	public static void main(String args[]){
		new NewThread();
		try{
			for(int i=1; i<=10; i++){
				System.out.println("Main Thread: " + i);
				System.out.println("Main Thread interrupted.");
				Thread.sleep(500);
			}
		}
		catch(InterruptedException e){
			System.out.println("main thread interrupted.");
		}
	System.out.println("Main Thread exiting");
	}
}

… from College notes (BCA/MCA assignments):


Java Program to show Multi Thread operation – Q25

January 13, 2009 Leave a comment

Q25: Java Program to create demothread class which extends thread class and main thread prints numbers from 1 to 10 and child thread prints from 1 to 5

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

class NewThread extends Thread{
	
	NewThread(){
		super("Demo Thread");
		System.out.println("Child thread: " + this);
		start();
	}
	
	public void run(){
		try{
			for(int i=1; i<=5; i++){
				System.out.println("Child Thread: " + i);
				System.out.println("Child Thread interrupted.");
				Thread.sleep(500);
			}
		}
		catch(InterruptedException e){
			System.out.println("Exiting child thread");
		}
	}
}

class demo_thread{
	public static void main(String args[]){
		new NewThread();
		try{
			for(int i=1; i<=10; i++){
				System.out.println("Main Thread: " + i);
				System.out.println("Main Thread interrupted.");
				Thread.sleep(500);
			}
		}
		catch(InterruptedException e){
			System.out.println("Main thread interrupted in 
Catch.");
		}
	System.out.println("Main Thread exiting");
	}
}

… from College notes (BCA/MCA assignments):


Java Program to show Abstract Class – Q24

January 12, 2009 Leave a comment

Q24: Java Program to create four classes figure(abstract), rectangle ,triangle and circle and
define Area method in all the classes.

An abstract class is a class that is declared as “abstract”, it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

abstract class figure{
	int a, b, c;
	abstract void area();
}

class rectangle extends figure{
	rectangle(int _a, int _b){
		a = _a;
		b = _b;
	}
	void area(){
		System.out.println("\n Area : " + a*b);
	}
}

class triangle extends figure{
	triangle(int _a, int _b, int _c){
		a = _a;
		b = _b;
		c = _c;
	}
	void area(){
		double s, ar;
		s = (a+b+c)/2;
		ar = Math.sqrt(s * (s-a) * (s-b) * (s-c));
		System.out.println("\n Area : " + ar);
	}
}

class circle extends figure{
	circle(int _a){
		a = _a;
	}
	void area(){
		System.out.println("\n Area : " + (22/7)*a*a);
	}
}

class fig_area{
	public static void main(String args[]){
		System.out.println("\n Area of Rectangle:-");
		rectangle Or = new rectangle(10, 5);
		Or.area();
		
		System.out.println("\n Area of Triangle:-");
		triangle Ot = new triangle(5, 10, 15);
		Ot.area();
		
		System.out.println("\n Area of Circle:-");
		circle Oc = new circle(10);
		Oc.area();
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags: ,

Java Program to show Class Inheritance – Q23

January 12, 2009 Leave a comment

Q23: Java Program to create a worker, hourly worker and salaried worker. Make compute-sal method in hourly worker and salaried worker which inherit the woker class and pass number of hours worked as argument. Two variables (name and hourly-wage) are there in the class which will be initialized by constructor.

Class Inheritance is when an object or class is based on another class, using the same implementation or specifying a new implementation to maintain the same behavior.

class worker{
	String name;
	int hrs_wage;
}

class hr_worker extends worker{
	int hrs;
	hr_worker(String n, int h_w){
		name = n;
		hrs_wage = h_w;
	}
	void compute_sal(int hrs){
		System.out.println("Worker Name: " + name);
		System.out.println("Salary: " + hrs_wage*hrs);
	}
}

class sal_worker extends worker{
	sal_worker(String n, int h_w){
		name = n;
		hrs_wage = h_w;
	}
	void compute_sal(){
		System.out.println("Worker Name: " + name);
		System.out.println("Salary: " + hrs_wage*250);
	}
}

class work_pay{
	public static void main(String args[]){
		System.out.println("\n Hourly Worker Salary:-");
		hr_worker O_h_w = new hr_worker("Nitin Kapoor", 20);
		O_h_w.compute_sal(150);
		
		System.out.println("\n Salaried Worker Salary:-");
		sal_worker O_s_w = new sal_worker("Manoj Pandey", 30);
		O_s_w.compute_sal();		
	}
}

… from College notes (BCA/MCA assignments):


Java Program to show Overriding Methods – Q22

January 11, 2009 Leave a comment

Q22: Java Program to show over-riding methods

Method Overriding allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes.

class base_cl{
	public void test(){
		System.out.println("Function test called of Base class");
	}
}

class derived_cl extends base_cl{
	public void test(){
		System.out.println("Function test called of Derived 
    class");
	}
}

class over_ride{
	public static void main(String args[]){
		
		System.out.println("\n Base class Instanciated.");
		base_cl Ob = new base_cl();
		Ob.test();
		
		System.out.println("\n Derived class Instanciated.");
		derived_cl Od = new derived_cl();
		Od.test();
	}
}

… from College notes (BCA/MCA assignments):