• Click below to download Unit 3 programs in pdf file: -




Unit 3


/* 1.Create a package P and within that package create class PackClass which have method called findmax( ) which find maximum value from three numbers. Now import the package within another class DemoClass and now display the maximum number. */


package p1; 


public class DemoClass 


 int no1=18, no2=15, no3=83; 


 public void max() 

 { 

 if(no1>no2 && no1>no3) 

 { 

 System.out.println("No1 is Maximum"); 

 } 

 else if (no2>no1 && no>no3) 

 { 

 System.out.println("No2 is Maximum"); 

 } 

 else

 { 

 System.out.println("No3 is Maximum"); 

 } 

 } 


// Save below code in separate file


package p; 

import p1.*; 


class PackClass extends DemoClass 

 public static void main(String[] args) 

 { 


 PackClass p=new PackClass(); 

 p.max(); 


 } 

}



/* 2.Write a program that creates three different classes in three different packages and access them from default package. All the three packages should be at the same level. */


//Save code in separate file accordingly


//First

package p1_3; 

public class first 

public void display() 

System.out.println("first package"); 


//second

package p2_3; 

public class second 

public void display() 

System.out.println("second package"); 

}


//Third

package p3_3; 

public class third 

public void display() 

System.out.println("third package"); 


//Main

import p1_3.*; 

import p2_3.*; 

import p3_3.*; 


public class p2

public static void main(String[] args) 

first f=new first(); 

f.display(); 


second s=new second(); 

s.display(); 


third t=new third(); 

t.display(); 



/* 3.Create package pack1 within this package create class A which contains one instance variable and one instance method. Create another package pack2 within this package create class B. where class B is calling the method and variable of class A */


//Save code in separate file accordingly


//Class A

package pack1; 

public class A 

int a=42; 

  

public void display() 

System.out.println("a==" + a); 

}


//Class B

package pack2; 

import pack1.*; 


class B extends A 

 public static void main(String[] args) 

 A a=new A(); 

a.display(); 

}



/* 4.Write a program that accepts a string from command line and perform following

operations:

1. Display each character on separate line in reverse order.

2. Count total number of chracters and display each character's position too.

3. Identify that whether the string is palindrom or not.

4. Count total number of uppercase and lowercase characters in it. */


class p4

 { 

   public static void main(String args[])

      {

         char c[]= args[0].toCharArray();


System.out.println("\n Display each character on seperate line in reverse order");


for(int i=c.length-1; 1>=0; i--)

{

System.out.println(c[i]);

}


System.out.println("Count total number of character and display each character's position"); 

System.out.println("\n Total number of characters are " + c.length); 


for(int i=0; i<c.length;i++)

{

System.out.println(c[i]+" character is at "+i+"position");

}


System.out.println("\n Identify the string is palindrom or not"); 

StringBuffer s1= new StringBuffer(args[0]);


StringBuffer s2=s1.reverse();


if(s1.equals(s2))

{

System.out.println("\n String is palindrom");

}

else

{

System.out.println("\n String is not palindrom");

}

System.out.println("\n Total number of uppercase and lower case characters");

int up=0,lw=0; 


for(int i=0; i<c.length;i++)

{

if(Character.isUpperCase(c[i]))

    up++;

else

    lw++;

}


System.out.println("\n Uppercase characters are: "+up); 

System.out.println("\n Lowercase characters are: "+lw);

   }

}



/* 5.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. */


class p5

{

public static void main(String[] args)

  {

int a[]= new int[args.length];


int temp;


try 

{

for(int i=0;i<args.length;i++)

   {

       a[i]= Integer.parseInt(args[i]);


   } 


for(int i=0;i<args.length;i++)

    {

          for(int j=i+1;j<args.length;j++)

                {

                         if(a[i]>a[j])

                              {

                                   temp=a[i];

                                   a[i]=a[j];

                                   a[j]=temp;

                              }

                }

      }


System.out.println("Lowest : " + a[0]+" and Second Lowest : "+a[1]);

}


catch (ArithmeticException e)

{

System.out.println("ArithmeticException occured");

catch (Exception e)

 { 

System.out.println("" + e.getMessage());

 }

}

}



/* 6.Write a program that takes a string from the user and validate it. The string should be at least 5 characters and should contain at least one digit. Display an appropriate valid message. */


class validatestring extends Exception

{

validatestring(String s)


System.out.println("\n String is not valid "+s);

}

}


class p6

{

public static void main(String args[]) 

{

String s= args[0];


char c[]=s.toCharArray();


int flag=0;


try{


if(s.length()>=5)

{

for(int i=0;i<s.length();i++)

{


if(Character.isDigit(c[i]))

{

flag=1;

System.out.println("\n String is valid");

break;


}


}


if(flag==0)


throw new validatestring(s);

}

else

{

throw new validatestring(s);

}


}

catch(Exception e){}

}

}



/* 7.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. */


class RangeException extends Exception

{

RangeException(int i) 

{

System.out.println("RangeException: Marks is not valid "+i);

}

}


class p7

{

public static void main(String args[])

{

int a[] = new int[3]; 

int sum=0; 

float per=0.0f; 


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

{

      try

        {

           a[i]= Integer.parseInt(args[1]);

           if(a[i] <0 || a[i]>100)

{

  throw new RangeException(a[i]);


else if(a[i]>=40)

{

sum=sum+a[i];


System.out.println("\n Pass in subject ");

}

else

{

sum=sum+a[i];

System.out.println("\n Fail in subject ");

  }

}


catch (NumberFormatException e) 

{

System.out.println("Number Format Exception"); 

}


catch(ArithmeticException e) 

{

System.out.println("arithmetic exception occur"); 


catch(ArrayIndexOutOfBoundsException e) 

{

 System.out.println("array index exception occur"); 

}


catch(Exception e) 

System.out.println("Exception: "+e.getMessage()); 

}


}


per=(float) sum/3;

System.out.println("\n Percentage is "+per);

  }

}



/* 8.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. */


class RangeException extends Exception 

RangeException(String message) 

{

super(message);

}


class p8 

{

public static void main(String args[]) 

{

int a[]=new int[5];

float avg=0.00f;

int sum=0;

try 

{


for(int j=0;j<5;j++) 

{

a[j]=Integer.parseInt(args[j]); 


if(a[j]>100||a[j]<0)

{

    throw new RangeException(" Invalid Range ");

}

}

for(int i=0;i<args.length;i++)

{

    sum += Integer.parseInt(args[i]);

}

avg = sum/5;

System.out.println("Average : " + avg);

}


catch (NumberFormatException e) 

{

System.out.println("Number Format Exception"); 

catch(ArithmeticException e) 

System.out.println("arithmetic exception occur"); 

catch(ArrayIndexOutOfBoundsException e) 

System.out.println("array index exception occur"); 

catch (Exception e) 

System.out.println("Exception"+e.getMessage());

}

}

}



/* 9.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 UnitformatException extends Exception

UnitformatException(String s)

super(" UnitformatException: unit is not valid "+s);

}

}


class p9

{

public static void main(String args[])

{

int no;

String u;


try

{

no= Integer.parseInt(args[0]); 

u=args[1];

if((u.equals("centimeter")) || (u.equals("meter")))

{

if(u.equals("centimeter"))

{

int m=no/100;

System.out.println("Equivalent number of meter is "+m);

}

else

{

int cm=no* 100;

System.out.println("Equivalent number of centi meter is " +cm);

}

else

{

throw new UnitformatException(u);

}

}


catch (NumberFormatException e) 

{

System.out.println("Number Format Exception"); 


catch(ArithmeticException e) 

{

System.out.println("arithmetic exception occur"); 


catch(ArrayIndexOutOfBoundsException e) 

{

System.out.println("array index exception occur"); 


catch (Exception e) 

{

System.out.println("Exception: "+e.getMessage()); 

}

}

}



/* 10.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. */


class Oddnumber extends Exception

{

Oddnumber(int i)

{

System.out.println("Number is odd ");

}

}


class p10

{

public static void main(String args[])

{

int a[]= new int[args.length]; 


for (int i=0;i<args.length;i++)

{

try

{

a[i]=Integer.parseInt(args[1]);


if((a[i])%2!=0) 

{

throw new Oddnumber(a[i]);

}

}


catch (Exception e)

{

System.out.println("Exception occured");

}

}

System.out.println("All numbers are even");

}

}