0% found this document useful (0 votes)
5 views17 pages

Java Unit 4 - Practical - New

The document contains a series of Java programming exercises focused on practical applications of core Java concepts, including exception handling, multithreading, and user input validation. Each exercise includes a code snippet that demonstrates the implementation of specific functionalities such as finding the smallest numbers, calculating averages, and converting units. The document is prepared by Manesh Patel and includes contact information.
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)
5 views17 pages

Java Unit 4 - Practical - New

The document contains a series of Java programming exercises focused on practical applications of core Java concepts, including exception handling, multithreading, and user input validation. Each exercise includes a code snippet that demonstrates the implementation of specific functionalities such as finding the smallest numbers, calculating averages, and converting units. The document is prepared by Manesh Patel and includes contact information.
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

ASSI. PROF.

MANESH PATEL
ASIA SPECIFIC BCA COLLEGE, USMANPURA, AHMEDABAD

BCA SEM: 3 JAVA - Unit – 4 –Practical CORE JAVA

1. Write a Java program to input n integer numbers and display lowest and second
lowest number. Also handle the different exceptions possible to be thrown during
execution.

import [Link].*;
import [Link].*;

class pro1
{
public static void main(String args[])
{
int arr[] = {40,11,80,90,110,33,77,92};
int N = [Link];
int small,next_small = Integer.MAX_VALUE;
small = arr[0];

try
{
for(int i=1;i<N;i++)
{
if(arr[i]<small)
{
next_small =small;
small = arr[i];
}
else if(arr[i]<next_small && arr[i]>small)
{
next_small = arr[i];
}
}
}
catch(Exception e)
{
[Link](e);
}

[Link]("\n The Smallest number = "+small);

[Link]("\n The second smallest number = "+next_small);


}
}

PREPARED BY: PATEL MANESH - [Link](CA & IT) Contact: 90165 17796 1
PATEL MANESH

2. Write an application that accepts marks of three different subject from user. Marks
should be between 0 to 100, if marks of any of the subject is not belong to this
range, generate custom exception out of RangeException. If marks of each subjects
are greater than or equal to 40 then display message “PASS” along with
percentage, otherwise display message “FAIL”. Also write exception handling code
to catch all the possible runtime exceptions likely to be generated in the program.

import [Link].*;
class pro2
{
public static void main(String args[])
{
try
{
int m1,m2,m3;
m1=[Link](args[0]);
m2=[Link](args[1]);
m3=[Link](args[2]);

if((m1>0 && m1<=100)&&(m2>0 && m2<=100)&&(m3>0 && m3<=100))


{
if(m1>=40 && m2>=40 && m3>=40)
{
[Link]("\n Pass");
[Link]("\n Percehtage: "+ ((m1+m2+m3) * 100) / 300);
}
else
{
[Link]("\n Fail");
}
s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 2
PATEL MANESH

}
else
{
[Link]("please enter marks between 1 to 100");
}
}
catch(Exception e)
{
[Link](e);
}
}
}

3. Write a program which takes the age of 5 persons from command line and find the
average age of all persons. The program should handle exception if the argument is
not correctly formatted and custom exception if the age is not between 1 to 100.

import [Link].*;

class AgeException extends Exception


{
public AgeException (String str)
{
[Link](str);
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 3
PATEL MANESH

public class pro3


{
public static void main(String args[])
{
int n = [Link];
int age[] = new int[n];
int sumage = 0;
int aveage;

try
{
for (int i = 0; i < n; i++)
{
age[i] = [Link](args[i]);
sumage = sumage + age[i];
}
aveage = sumage / n;
[Link]("\n Average age of person is: " + aveage);

for (int i = 0; i < n; i++)


{
if (age[i] < 1 || age[i] > 100)
{
throw new AgeException(age[i] + " is an invalid age");
}
else
{
[Link]("\n"+age[i] + " is a valid age");
}
}

}
catch (NumberFormatException e)
{
[Link]("Your input is not a number: " + [Link]());
}
catch (AgeException a)
{
[Link](a);
}
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 4
PATEL MANESH

4. Write an application that converts between meters and feet. Its first commandline
argument is a number and second command line argument is either "centimeter"
or "meter". If the argument equals "centimeter" displays a string reporting the
equivalent number of meters. If this argument equals "meters", display a string
reporting the equivalent number of centimeter. If unit is not given properly then
generate custom exception Unitformatexception. If first argument is not proper
format then generate numberformatexception. Generate other exception as per
requirements. (1 meter=100 centimeter)

class UnitFormateException extends Exception


{
UnitFormateException(String str)
{
super(str);
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 5
PATEL MANESH

public class pro4


{
public static void main(String args[])
{
int num,value;
String str;
try
{
num = [Link](args[0]);
str = args[1];
if( ([Link]("meter")) || ([Link]("centimeter")) )
{
if([Link]("meter"))
{
value = num* 100;
[Link](num+" meter is equal to "+value +" centimeter ");
}
else
{
value = (num * 1)/100;
[Link](num+" centimeter is equal to "+value +" meter ");
}
}
else
{
throw new UnitFormateException("Invalid unit");
}
}
catch(NumberFormatException e)
{
[Link] ("Invalid number Formate: "+e);
}
catch(UnitFormateException e)
{
[Link](e);
}
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 6
PATEL MANESH

5. Write a program that accepts 5 even numbers from command line , if any of the
numbers is odd then throw custom exception OddException and count such invalid
numbers.

import [Link].*;

class OddException extends Exception


{
OddException(String str)
{
super(str);
}
}

class prg5
{
public static void main(String args[])
{
int n;
int count = 0;

for(int i=0;i<[Link];i++)
{
try
{
n = [Link](args[i]);
if(n%2==0)
{
[Link]("\n"+n+" number is even");
}
else
{
count++;
throw new OddException("\n"+n+"number is odd number");
}
}
catch(OddException e)
{
[Link]("\n" + e);
}
}
[Link]("\n\n***** Invalid Number= "+count +" *****");
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 7
PATEL MANESH

6. Write an application that starts two thread. First thread displays even numbers in
the range specified from the command line and second thread displays odd
numbers in the same range. Each thread waits for 300 milliseconds before
displaying the next numbers. The application waits for both the thread to finish and
then displays the message “Both threads completed”.

class eventhread extends Thread


{
int ll,up;
eventhread(int x, int y)
{
ll=x;
up=y;
}
public void run()
{
try
{
for(int i=ll;i<=up;i++)
{
if(i%2==0)
{
[Link](1000);
[Link]("Even thread "+i);
}
}
}
s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 8
PATEL MANESH

catch(Exception e)
{
[Link](e);
}
}
}

class oddthread extends Thread


{
int ll,up;
oddthread(int x, int y)
{
ll=x;
up=y;
}
public void run()
{
for(int i=ll;i<=up;i++)
{
if(i%2!=0)
{
[Link]("Odd thread "+i);
}
}
}
}
class prg6
{
public static void main(String M[])
{
int ll= [Link](M[0]);
int up=[Link](M[1]);
eventhread e1= new eventhread(ll,up);
oddthread o1=new oddthread(ll,up);
[Link]();
[Link]();
try
{
[Link]();
[Link]();
}
s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 9
PATEL MANESH

catch(Exception e){}
[Link]("\n Both threads completed");
}
}

7. Write a program that create and starts five threads. Each thread is instantiated
from the same class. It executes a loop with ten iterations. Each iteration displays
the character 'x' and sleep for 500 milliseconds. The application waits for all
threads to complete and then display a message ‘hello’

class Mythread extends Thread


{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
[Link](500);
[Link]('x');
}
}
catch(Exception e)
{
[Link]();
}
}
}
class prg7
{
public static void main(String args[])
{
Mythread m[] =new Mythread[5];

for(int i=0;i<5;i++)
{
m[i]=new Mythread();
}
for(int i=0;i<5;i++)
{
m[i].start();
}
s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 10
PATEL MANESH

for(int i=0;i<5;i++)
{
try
{
m[i].join();
}
catch(Exception e)
{ }
}

[Link]("\n hello");
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 11
PATEL MANESH

8. Write a java program to create 2 threads each thread calculates the sum and
average of 1 to 10 and 11 to 20 respectively. After all thread finish, main thread
should print message “ Task Completed”. Write this program with use of runnable
interface.

class sum1 implements Runnable


{
int sum=0;
float avg =0.0f;

public void run()


{
for(int i=1;i<=10;i++)
{
sum=sum+i;
avg=(float)sum/i;
[Link]("\n In thread sum1 Sum is" + sum+ "Avg is "+avg);
}
}
}

class sum2 implements Runnable


{
int sum=0;
float avg =0.0f;

public void run()


{
for(int i=11;i<=20;i++)
{
sum=sum+i;
avg=(float)sum/i;
[Link]("\n In thread sum2 Sum is" + sum+ "Avg is "+avg);

}
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 12
PATEL MANESH

class prg8
{
public static void main(String args[])
{
sum1 s1= new sum1();
Thread th1= new Thread(s1);

sum2 s2=new sum2();


Thread th2= new Thread(s2);

[Link]();
[Link]();
try
{
[Link]();
[Link]();
}
catch(Exception e){ }

[Link]("\n****** Task Completed ******");


}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 13
PATEL MANESH

9. Create two thread. One thread print ‘fybca’ 4 times and another thread print
‘sybca’ 6 times. Set priority for both thread and when thread finished print ‘tybca’
from main.

class one extends Thread


{
public void run()
{
for(int i=1;i<=4;i++)
{
[Link]("\n In thread one FYBCA" );
}
}
}

class second extends Thread


{
public void run()
{
for(int i=1;i<=6;i++)
{
[Link]("\n In thread second SYBCA" );

}
}
}
class prg9
{
public static void main(String args[])
{
one o1 = new one();
second s2 = new second();
[Link](1);
[Link](10);

[Link]();
[Link]();
try
{
[Link]();
[Link]();
}
s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 14
PATEL MANESH

catch(Exception e)
{
}
[Link]("\n In main thread TYBCA");
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 15
PATEL MANESH

10. Write an application that starts two threads. Set priorities of both threads as 8 and
4 respectively. Each thread executes a loop with 5 iterations displaying its thread
name. Demonstrate the execution of a high priority thread and how it delays the
execution of low priority thread.

import [Link].*;
import [Link].*;

class ThreadNaming extends Thread


{
public void run()
{
for (int i = 0; i < 5; i++)
{
[Link](i + " " + getName());

try
{
sleep(1000);
}
catch (Exception e)
{ }
}
[Link]("DONE! " + getName());
}

class Demo1
{
public static void main(String args[])
{
ThreadNaming t1 = new ThreadNaming();
ThreadNaming t2 = new ThreadNaming();

[Link](8);
[Link](4);

[Link]();
[Link]();
}
}

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 16
PATEL MANESH

s
PREPARED BY: PATEL MANESH - [Link](CA & IT), [Link] Contact: 90165 17796 17

You might also like