Java Journal Program Unit - 3
Unit - 3
//PROGRAM - 1
/*Write a program for example of try and
catch block. In this check whether the
given array size is negative or not.*/
public class program1
{
public static void main(String[] args) {
int arrsize = -8;
try
{
int[] myarr = new int[arrsize];
}
catch(NegativeArraySizeException ex)
{
System.out.println("can't create Negative size of array");
}
}
}
OUTPUT:
can't create Negative size of array
_______________________________________________________
//PROGRAM - 2
/*
Write a program for example of multiple
catch statements and finally statement
occurring in a program.
*/
class program2
{
public static void main(String[] args) {
try
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a / b;
System.out.println("Division : " + c);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter Two Argument");
}
catch(NumberFormatException ne)
{
System.out.println("Enter Two Numaric Argument");
}
catch(ArithmeticException ae)
{
System.out.println("Second Argument must be Non-Zero");
}
catch(Exception e)
{
System.out.println("Exception : " + e);
}
finally
{
System.out.println("Finally block Execute");
}
}
}
OUTPUT:
java program2 4 2
Division : 2
Finally block Execute
----------------------------------------------------------
java program2
Enter Two Argument
Finally block Execute
----------------------------------------------------------
java program2 d d
Enter Two Numaric Argument
Finally block Execute
----------------------------------------------------------
java program2 4 0
Second Argument must be Non-Zero
Finally block Execute
_________________________________________________
//PROGRAM - 3
/*
Write a program to describe usage
of throws clause.
*/
class program3
{
public static void main(String[] args) {
try
{
//ArithmeticException obj = new ArithmeticException();
//throw obj;
throw new ArithmeticException();
}
catch(ArithmeticException ae)
{
System.out.println("Exception Caught");
}
catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
}
/*
OUTPUT:
Exception Caught
*/
_________________________________________________
//PROGRAM - 4
/*
Write a program for creation of user d
defined exception.
*/
class ExceptionA extends Exception
{
ExceptionA(String msg)
{
super(msg);
System.out.println(msg);
}
}
class ExceptionB extends Exception
{
ExceptionB(String msg)
{
super(msg);
System.out.println(msg);
}
}
class program4
{
public static void main(String[] args) throws ExceptionA, ExceptionB
{
int n = Integer.parseInt(args[0]);
if (n % 2 == 1) {
throw new ExceptionA("Problem");
}
else
{
throw new ExceptionB("Big Problem");
}
}
}
/*
java program4 23
Problem
Exception in thread "main" ExceptionA: Problem
at program4.main(program4.java:29)
OR
java program4 22
Big Problem
Exception in thread "main" ExceptionB: Big Problem
at program4.main(program4.java:33)
*/
_________________________________________________
//PROGRAM - 5
/*
Write a java program that implements bank
transactions using user defined exception.
*/
import java.util.*;
class Exception_deposite extends Exception
{
Exception_deposite(String msg)
{
super(msg);
System.out.print(msg);
Scanner amount = new Scanner(System.in);
int amt = amount.nextInt();
System.out.println("Deposite amount is " +amt);
}
}
class Exception_withdrow extends Exception
{
Exception_withdrow(String msg)
{
super(msg);
System.out.print(msg);
Scanner withdraw_amount = new Scanner(System.in);
int wi_amt = withdraw_amount.nextInt();
System.out.println("Withdrow amount is " + wi_amt);
}
}
class program5
{
public static void main(String[] args)throws Exception_deposite,Exception_withdrow,Exception_checkbalance
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter 1 for Deposite : ");
System.out.println("Enter 2 for Withdrow : ");
System.out.print("Enter Your Chice : ");
int n = obj.nextInt();
try
{
if(n == 1)
{
throw new Exception_deposite("Enter Deposite Amount : ");
}
else if(n == 2)
{
throw new Exception_withdrow("Enter Withdrow Amount : ");
}
else
{
System.out.println("Invalid choice");
}
}
catch(Exception ex)
{
System.out.println("Transactions Successfully complited");
}
}
}
/*
OUTPUT:
Enter 1 for Deposite :
Enter 2 for Withdrow :
Enter Your Chice : 1
Enter Deposite Amount : 500
Deposite amount is 500
Transactions Successfully complited
OR
Enter 1 for Deposite :
Enter 2 for Withdrow :
Enter Your Chice : 7
Invalid choice
*/
_________________________________________________
//PROGRAM - 6
/*
Write an application that executes two threads.
One thread displays ―”A” every 1000 milliseco-
nds and other displays ―”B” every 3000 millis-
econds. Create the threads by extending the
Thread class.
*/
class MyThread1 extends Thread
{
public void run()
{
try{
for (int i = 0; i <= 5; i++) {
System.out.println("\t A");
Thread.sleep(1000);
}
}
catch(InterruptedException ex){
System.out.println(ex);
}
}
}
class Mythread2 extends Thread
{
public void run()
{
try
{
for (int i = 0; i <= 5; i++) {
System.out.println("\t B");
Thread.sleep(3000);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class program6
{
public static void main(String[] args) {
MyThread1 obj1 = new MyThread1();
obj1.start();
Mythread2 obj2 = new Mythread2();
obj2.start();
}
}
/*
OUTPUT:
A
B
A
A
B
A
A
A
B
B
B
B
*/
_________________________________________________
//PROGRAM - 7
/*
Write a program that creates three threads.
First thread displays “Good Morning” every
one second, the second thread displays
“Hello” every two seconds and the third
thread displays “Welcome” every three seconds.
Note : program run krti vakhte aa comment
kadhi nakhvi.
when run this program please delete
this comment.
*/
class MyThread1 extends Thread
{
public void run()
{
try{
for (int i = 0; i <= 5; i++) {
System.out.println("\t" + "Good Morning");
Thread.sleep(1000);
}
}
catch(InterruptedException ex){
System.out.println(ex);
}
}
}
class Mythread2 extends Thread
{
public void run()
{
try
{
for (int i = 0; i <= 5; i++) {
System.out.println("\t" + "Hello");
Thread.sleep(2000);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class Mythread3 extends Thread
{
public void run()
{
try
{
for (int i = 0; i <= 5; i++) {
System.out.println("\t"+ "Welcome");
Thread.sleep(3000);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class program7
{
public static void main(String[] args) {
MyThread1 obj1 = new MyThread1();
obj1.start();
Mythread2 obj2 = new Mythread2();
obj2.start();
Mythread3 obj3 = new Mythread3();
obj3.start();
}
}
/*
OUTPUT:
Good Morning
Hello
Welcome
Good Morning
Hello
Good Morning
Welcome
Good Morning
Hello
Good Morning
Good Morning
Welcome
Hello
Hello
Welcome
Hello
Welcome
Welcome
*/
_________________________________________________
//PROGRAM - 8
/*
Write a java program that implements a
multi-thread application that has three
threads. First thread generates random
integer every 1 second and if the value
is even, second thread computes the
square of the number and prints. If the
value is odd, the third thread will print
the value of cube of the number.
*/
class MyThread extends Thread
{
public void run()
{
try{
for (int i = 1; i <= 10; i++) {
System.out.println(i);
Thread.sleep(1000);
if(i % 2 == 0)
{
int sq = i * i;
System.out.println("Square = " + sq);
}
else if (i % 2 == 1) {
int cu = i * i * i;
System.out.println("Cube = " + cu);
}
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class program8
{
public static void main(String[] args) {
MyThread obj = new MyThread();
obj.start();
}
}
/*
OUTPUT:
1
Cube = 1
2
Square = 4
3
Cube = 27
4
Square = 16
5
Cube = 125
6
Square = 36
7
Cube = 343
8
Square = 64
9
Cube = 729
10
Square = 100
*/
_________________________________________________
//PROGRAM - 10
/*
Write a program illustrating
*/
class MyThread extends Thread
{
public void run()
{
System.out.println("Thread Start");
try{
Thread.sleep(1000);
}
catch(Exception ex)
{
System.out.println(ex);
}
System.out.println("Thread End");
}
}
class program10
{
public static void main(String[] args) {
MyThread obj1 = new MyThread();
MyThread obj2 = new MyThread();
obj1.start();
try{
obj1.join();
}
catch(Exception ex)
{
System.out.println(ex);
}
obj2.start();
System.out.println(obj1.isAlive());
System.out.println(obj2.isAlive());
}
}
/*
OUTPUT:
Thread Start
Thread End
false
Thread Start
true
Thread End
*/
_________________________________________________
//PROGRAM - 11
/*
Write a program for inventory problem
in this to illustrates the usage of
synchronized keyword.
*/
class Account
{
static int bal;
Account()
{
bal = 0;
}
synchronized void deposit(int amt)
{
bal = bal + amt;
}
}
class customer extends Thread
{
Account act;
customer (Account ac)
{
act = ac;
}
public void run()
{
for (int i = 1; i <= 10000; i++) {
act.deposit(10);
}
}
}
class program11
{
public static void main(String[] args) {
Account acc = new Account();
customer cust[] = new customer[10];
int a;
for (a = 0; a < 5; a++) {
cust[a] = new customer(acc);
cust[a].start();
}
for (a = 0; a < 5; a++) {
try{
cust[a].join();
}
catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
System.out.println("Balance : " + acc.bal);
}
}
/*
OUTPUT:
Balance : 500000
*/
_________________________________________________
//PROGRAM - 12
/*
Write a Program illustrating Daemon Threads.
*/
public class program12 extends Thread
{
public program12(String name){
super(name);
}
public void run()
{
// Checking whether the thread is Daemon or not
if(Thread.currentThread().isDaemon())
{
System.out.println(getName() + " is Daemon thread");
}
else
{
System.out.println(getName() + " is User thread");
}
}
public static void main(String[] args)
{
program12 t1 = new program12("t1");
program12 t2 = new program12("t2");
program12 t3 = new program12("t3");
t1.setDaemon(true);
t1.start();
t2.start();
t3.setDaemon(true);
t3.start();
}
}
/*
OUTPUT:
t1 is Daemon thread
t3 is Daemon thread
t2 is User thread
*/
_________________________________________________
//PROGRAM - 13
/*
Write a program to create a text file in the path c:\java\abc.txt
and check whether that file is exists. Using the command exists(),
isDirectory(), isFile(), getName() and getAbsolutePath().
*/
import java.io.*;
class program13
{
public static void main(String[] args) {
try
{
File file = new File("C:\\Users\\Divyesh\\Desktop\\java\\abc.txt");
System.out.println("exists() = " + file.exists());
System.out.println("isDirectory() = " + file.isDirectory());
System.out.println("isFile() = " + file.isFile());
System.out.println("getName() = " + file.getName());
System.out.println("getParent() = " + file.getParent());
System.out.println("getAbsolutePath() = " + file.getAbsolutePath());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
OUTPUT:
exists() = true
isDirectory() = false
isFile() = true
getName() = abc.txt
getParent() = C:\Users\Divyesh\Desktop\java
getAbsolutePath() = C:\Users\Divyesh\Desktop\java\abc.txt
*/
_________________________________________________
//PROGRAM - 14
/*
Write a program to rename the given file, after
renaming the file delete the renamed file. (Accept
the file name using command line arguments.)
*/
import java.io.*;
class program14
{
public static void main(String[] args) {
File f1 = new File("abc.txt");
File f2 = new File("xyz.txt");
boolean b2 = f1.renameTo(f2);
boolean b1 = f2.delete();
if (b2 == true) {
System.out.println("File Rename Successfully");
if (b1 == true) {
System.out.println("File Deleted Successfully");
}
}
}
}
/*
OUTPUT:
File Rename Successfully
File Deleted Successfully
*/
_________________________________________________
//PROGRAM - 15
/*
Write a java program that reads a file name from the user,
and then displays information about whether the file exists,
whether the file is readable, whether the file is writable,
the type of file and the length of the file in bytes.
*/
import java.io.*;
import java.util.*;
class program15
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter file name : ");
String str = obj.next();
File f1 = new File(str);
System.out.println("exists() = " + f1.exists());
System.out.println("canRead() = " + f1.canRead());
System.out.println("canWrite() = " + f1.canWrite());
System.out.println("length() = " + f1.length() + "bytes");
}
}
/*
OUTPUT:
Enter file name : program15.txt
exists() = true
canRead() = true
canWrite() = true
length() = 24
*/
_________________________________________________
//PROGRAM - 17
/*
Write a Java program to copy the contents
of one file into another file. File names
should be inserted by the user.
*/
import java.io.*;
import java.util.*;
class program17
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter Source file name : ");
String str1 = obj.next();
System.out.print("Enter destination file name : ");
String str2 = obj.next();
try
{
File f1 = new File(str1);
File f2 = new File(str2);
FileReader fin = new FileReader(str1);
FileWriter fout = new FileWriter(str2, true);
int c;
while ((c = fin.read()) != -1) {
fout.write(c);
}
System.out.println("Copy sucessfully..." +str1+ " to " + str2);
fin.close();
fout.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
/*
OUTPUT:
Copy sucessfully...program17source.txt to program17destination.txt
*/
_________________________________________________
//PROGRAM - 18
/*
Write a Java program to display the last N
characters of a file.
*/
//Buffereader
import java.io.*;
class program18
{
public static void main(String[] args) {
try
{
FileReader fr = new FileReader("program18.txt");
BufferedReader br = new BufferedReader(fr);
int ch;
while((ch = br.read()) != -1)
{
System.out.print((char) ch );
}
br.close();
fr.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
OUTPUT:
Hello
this is program18
*/
_________________________________________________
Comments
Post a Comment