0% found this document useful (0 votes)
1 views8 pages

Java 12

Uploaded by

yami2op4m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views8 pages

Java 12

Uploaded by

yami2op4m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Programming with Java BCS306A

Program 8: Develop a JAVA program to create an outer class with a function display. Create another class
inside the outer class named inner with a function called display and call the two functions in the main class.

class Outer {

public void display() {

[Link]("OuterClass display method");

class Inner {

public void display() {

[Link]("Inner class display method");

public class MainClass {

public static void main(String[] args) {

Outer outerObj = new Outer();

[Link]();

[Link] innerObj = [Link] Inner();

[Link]();

OUTPUT

[Link] Ise Page 1


Object Oriented Programming with Java BCS306A

Program 9: Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero
using try, catch, throw and finally.

class DivisionByZeroException extends Exception {

public DivisionByZeroException(String message) {

super(message);

public class CustomExceptionExample {

public static void main(String[] args) {

int a=10,b=10,c;

try{

if (b == 0) {

throw new DivisionByZeroException("Cannot divide by zero!");

catch(Exception e)

[Link]("Error: " + [Link]());

finally{

c=a/b;

[Link]("Result: " + c);

[Link] Ise Page 2


Object Oriented Programming with Java BCS306A

OUTPUT

[Link] Ise Page 3


Object Oriented Programming with Java BCS306A

Program 10: Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.

package mypack;

public class Program10b {

public void display()

[Link]("I am in MyPack package");

package pkg22scheme;

import mypack.Program10b;

public class program10 {

public static void main(String args[])

Program10b p=new Program10b();

[Link]();

OUTPUT

[Link] Ise Page 4


Object Oriented Programming with Java BCS306A

Program 11:Write a program to illustrate creation of threads using runnable class. (start method start each of
the newly created thread. Inside the run method there is sleep() for suspend the thread for 500 milliseconds).

class MyRunnable implements Runnable {

public void run() {

try {

[Link]("Thread " + [Link]().getId() + " is running.");

[Link](500);

[Link]("Thread " + [Link]().getId() + " has completed.");

} catch (InterruptedException e) {

[Link]();

public class ThreadExample {

public static void main(String args[]) {

int numberOfThreads = 5;

for (int i = 0; i < numberOfThreads; i++) {

MyRunnable myRunnable = new MyRunnable();

Thread thread = new Thread(myRunnable);

[Link]();

[Link] Ise Page 5


Object Oriented Programming with Java BCS306A

OUTPUT

[Link] Ise Page 6


Object Oriented Programming with Java BCS306A

Program 12:Develop a program to create a class MyThread in this class a constructor, call the base class
constructor, using super and start the thread. The run method of the class starts after this. It can be observed
that both main thread and created child thread are executed concurrently.

class MyThread extends Thread {

public MyThread(String name) {

super(name);

start();

public void run() {

for (int i = 1; i <= 5; i++) {

[Link]([Link]().getName());

try {

[Link](1000);

} catch (InterruptedException e) {

public class Prog12 {

public static void main(String[] args) {

MyThread myThread = new MyThread("Child Thread");

for (int i = 1; i <= 5; i++) {

[Link]([Link]().getName());

try {

[Link](500);

} catch (InterruptedException e) {

[Link] Ise Page 7


Object Oriented Programming with Java BCS306A

OUTPUT

[Link] Ise Page 8

You might also like