Sem 5‎ > ‎

JAVA LAB

Q. Exception Handling #1

posted Aug 29, 2012, 6:06 PM by Neil Mathew   [ updated Aug 29, 2012, 6:09 PM ]

Not part of the list of Amizone questions, but explains a little about the try-catch-finally statements.

CONCLUSION:

1) The Finally block of statements WILL run if the error or jump statement is within the try block.

2) In this program, all try statements in functions lack catch statements. 
    Hence, they will be caught by the main function, leaving the function mid way as seen as in Process A.
      (The finally statements will run, on leaving the try block)

3) If there is a return statement in the try block, the function will end, but the finally statement will run before leaving the function.


SOURCE CODE:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package TryCatchExamples;
 
import java.lang.Exception.*;
 
class TryCatchExample
{
static void processA()
{
boolean check=false;
 
System.out.println(" BEG of Process A. \n");
 
try
{
check=true;             
System.out.println("    BEG of Try of Process A. ");
 
if(check)
throw new RuntimeException("OH MY! AN ERROR!");
 
System.out.println("    BEG of Try of Process A. ");
}
finally
{       
System.out.println("    >Finally of Process A. ");
}
 
System.out.println("\n END of Process A. ");
}
 
static void processB()
{
boolean check=true;
System.out.println(" BEG of Process B. \n");
 
        
try
{
System.out.println("    BEG of Try of Process B. ");
if(check)
return;
System.out.println("    BEG of Try of Process B. ");
}
finally
{
System.out.println("    >Finally of Process B. ");
}
 
System.out.println("\n END of Process B. ");
        
}
 
static void processC()
{
System.out.println(" BEG of Process C. \n");
 
try
{
System.out.println("    BEG of Try of Process C. ");
System.out.println("    END of Try of Process C. ");
}
finally
{
System.out.println("    >Finally of Process C. ");
}
 
System.out.println("\n END of Process C. ");
}
 
public static void main(String args[])
{
System.out.println("\n Understanding try-catch-finally-throw: ");
System.out.print(" A try-finally in each Process");
System.out.println(", with a try-catch in main: ");
System.out.println("\n Process A: throws Exception ");
System.out.println(" Process B: Return statement in try-catch. ");
System.out.println(" Process C: No Exception. No Return. ");
 
try
{
System.out.print("\n\n");
processA();
}
catch(Exception e)
{
System.out.println("    Exception Caught at "+e);
 
System.out.print("\n\n");
processB();
System.out.print("\n\n");
processC();
}
}
 
 
}


OUTPUT:



C:\SomeRandomFolder> java TryCatchExamples.TryCatchExample

                                                                                                                   

Understanding try-catch-finally-throw:                                                          

A try-finally in each Process, with a try-catch in main:                                                          

                                                                                                                   

Process A: throws Exception                                                          

Process B: Return statement in try-catch.                                                          

Process C: No Exception. No Return.                                                          

                                                                                                                   

                                                                                                                   

BEG of Process A.                                                          

       BEG of Try of Process A.                                                          

       >Finally of Process A.                                                                                                                  

       Exception Caught at java.lang.RuntimeException: OH MY! AN ERROR!                                                          

                                                                                                                                                                           

                                                                                                                                                                          

BEG of Process B.                                                          

       BEG of Try of Process B.                                                          

       >Finally of Process B.                                                          

                  

                                                                                                 

BEG of Process C.                                                          

       BEG of Try of Process C.                                                          

       END of Try of Process C.                                                          

       >Finally of Process C.                                                          

END of Process C.  



P4: WAP a program to maintain the student record (BufferedReader)

posted Aug 22, 2012, 10:32 AM by Neil Mathew   [ updated Aug 22, 2012, 10:36 AM ]



SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.*;
 
 class StudentRecord
        {
         String name;
         String rollno;
         double marks[] = new double[3];
         double avg;
 
        void getdata() throws IOException
        {
 InputStreamReader ir=new InputStreamReader(System.in);
 BufferedReader br = new BufferedReader(ir);
 
        System.out.print("\n\n Enter the Name:");       
        name = br.readLine();
 
        System.out.print("\n Enter the RollNo:");       
        rollno=br.readLine();
 
        System.out.print("\n Enter the Marks of Maths:");
        marks[0] = Double.parseDouble(br.readLine());
 
        System.out.print("\n Enter the Marks of Physics:");
        marks[1] = Double.parseDouble(br.readLine());
 
        System.out.print("\n Enter the Marks of Chemistry:");
        marks[2] = Double.parseDouble(br.readLine());
        }
        void setdata()
        {       
        avg=0;
        for(int i=0; i<3; i++)
        avg += marks[i];
        avg/=3;
        }
        void display()
        {
        System.out.println("\n\n Name:"+name);
        System.out.println(" RollNo:"+rollno);
        System.out.println("\n Marks of Maths:"+marks[0]);
        System.out.println(" Marks of Physics:"+marks[1]);
        System.out.println(" Marks of Chemistry:"+marks[2]);
        System.out.println("\n Average Marks:"+avg);
        }
}
 
public class Record
{       
        public static void main(String args[]) throws IOException
                {
                        StudentRecord obj=new StudentRecord();
                        obj.getdata();  
                        obj.setdata();  
                        obj.display();
                }
}
 



OUTPUT:



C:\Java 1.6\jdk1.6.0_30\bin\src3>javac Record.java


C:\Java 1.6\jdk1.6.0_30\bin\src3>java Record

 

 

 Enter the Name:Neil

 

 Enter the RollNo:3305

 

 Enter the Marks of Maths:85

 

 Enter the Marks of Physics:80

 

 Enter the Marks of Chemistry:90

 

 

 Name:Neil

 RollNo:3305

 

 Marks of Maths:85.0

 Marks of Physics:80.0

 Marks of Chemistry:90.0

 

 Average Marks:85.0

 




P5: WAP to increment the employee salaries on the basis of designation.

posted Aug 22, 2012, 10:25 AM by Neil Mathew   [ updated Aug 22, 2012, 10:26 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class SmallerEmp
{      
int id,sal;
String name,desg;
 
        void getdata(String p,int q,String r, int s)
        {
        name=p;
        id=q;
        desg=r;
        sal=s;
 
        System.out.println("\n Name: "+name);
        System.out.println(" Id: "+id);
        System.out.println(" Desgignation: "+desg);
        System.out.println(" Salary: "+sal);
        }
}
 
class EmpIncrement extends SmallerEmp
{
 
        void inc_sal()
        {
        if (desg.compareTo("Manager") == 0) {
        sal+=5000;
        System.out.println("\n Incremented Salary :"+sal);
        } if (desg.compareTo("General Manager") == 0 ) {
        sal+=10000;
        System.out.println("\n Incremented Salary :"+sal);
        } if ( desg.compareTo("CEO") == 0) {
        sal+=20000;
        System.out.println("\n Incremented Salary :"+sal);
        } if ( desg.compareTo("Worker") == 0 ) {
        sal+=2000;
        System.out.println("\n Incremented Salary :"+sal);
        }
        }
 
        public static void main(String arg[])
        {
        EmpIncrement e1=new EmpIncrement ();
        String p=arg[0];
        int q=Integer.parseInt(arg[1]);
        String r=arg[2];
        int s=Integer.parseInt(arg[3]);
 
        e1.getdata(p,q,r,s);
        e1.inc_sal();
        }
} 
 



OUTPUT:


C:\Java 1.6\jdk1.6.0_30\bin>javac EmpIncrement.java

 

C:\Java 1.6\jdk1.6.0_30\bin>java EmpIncrement Neil 3305 CEO 20000

 

 Name: Neil

 Id: 3305

 Desgignation: CEO

 Salary: 20000

 

 Incremented Salary :40000

 

C:\Java 1.6\jdk1.6.0_30\bin>java EmpIncrement Jimmy 3323 Worker 10000

 

 Name: Jimmy

 Id: 3323

 Desgignation: Worker

 Salary: 10000

 

 Incremented Salary :12000

 

 


Px10: WAP to implement multiple inheritance using Interfaces.

posted Aug 22, 2012, 10:14 AM by Neil Mathew   [ updated Aug 22, 2012, 10:15 AM ]


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Interface
interface FirstNameShow
{
String fname = "Neil";
void showFirst();
}
 
//Interface #2
interface LastNameShow
{
String lname = "Mathew";
void showLast();
}
 
//Normal Class
class ID_GetShow
{
int idnum;
 
void getnumber(int n)
        {
        idnum=n;
        }
 
void putnumber()
        {
        System.out.println("ID Number:"+idnum);
        }
 
}
 
class InterfaceExample extends ID_GetShow 
implements FirstNameShow, LastNameShow
{ 
public void showFirst()
        {
        System.out.println("First Name: "+fname);
        } 
public void showLast()
        {
        System.out.println("Last Name: "+lname);
        } 
 
public static void main(String args[])
        {
 
        InterfaceExample a=new InterfaceExample();
        a.getnumber(15);
        a.showFirst();
        a.showLast();
        a.putnumber();
        }
}
 




OUTPUT:


C:\Java 1.6\jdk1.6.0_30\bin>cd src3


C:\Java 1.6\jdk1.6.0_30\bin\src3>javac InterfaceExample.java

 

C:\Java 1.6\jdk1.6.0_30\bin\src3>java InterfaceExample

 

First Name: Neil

Last Name: Mathew

ID Number:15

 


Px11: WAP to create Student class and Marks class in two different packages

posted Aug 22, 2012, 10:08 AM by Neil Mathew   [ updated Aug 22, 2012, 10:09 AM ]



SOURCE CODE OF STUDENT CLASS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
        //package declaration first
package StudyHard;
 
        //Import statements
import java.io.*;
import java.lang.*;
 
        //Class
public class Student
{
String name;
long rollno;
 
public void getStudentDetails()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //remember to remove the ln from SOP
System.out.print(" Enter Student Name: ");
name=br.readLine();
System.out.print(" Enter Roll No: ");
rollno=Long.parseLong(br.readLine());
}
 
        //public so that another package can access it 
        //(if no inheritance)
public void showStudentDetails()
{
System.out.println("\n Student Name: "+name);
System.out.println(" Enrollment No: "+rollno);
}
 
        /*Since this class is made to be accessed by another, 
        main() is not necessary, but to access this class directly,
        main function is necessary */
 
public static void main(String args[])throws IOException
{
Student ob=new Student();
ob.getStudentDetails();
ob.showStudentDetails();
}
 
}
 


SOURCE CODE OF MARKS CLASS:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package TopperMaterial;
 
import java.io.*;
import java.lang.*;
import StudyHard.*;
 
 
class Marks
{
        //array declarations different in java
int marks[] = new int[3];
double avg;
 
void getMarksDetails()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 
        //remember to remove ln
System.out.print(" Enter Marks of Physics: ");
marks[0] = Integer.parseInt(br.readLine());
 
System.out.print(" Enter Marks of Chemistry: ");
marks[1] = Integer.parseInt(br.readLine());
 
System.out.print(" Enter Marks of Mathematics: ");
marks[2] = Integer.parseInt(br.readLine());
 
avg = marks[0]+marks[1]+marks[2];
avg/=3;
 
}
 
void showMarksDetails()
{
System.out.println("\n Marks of Physics: "+marks[0]);
System.out.println(" Marks of Chemistry: "+marks[1]);
System.out.println(" Marks of Mathematics: "+marks[2]);
 
System.out.println(" Average Marks:"+avg);
}
 
public static void main(String args[]) throws IOException
{
        //The functions of Student should be public so 
        //that they can be accessed by the different package
        //(if no inheritance)
Student Sob=new Student();
Marks Mob=new Marks();
 
System.out.println("\n Enter Details: \n");
Sob.getStudentDetails();
Mob.getMarksDetails();
 
System.out.println("\n Showing Details: \n");
Sob.showStudentDetails();
Mob.showMarksDetails();
}
}


OUTPUT:




c:\>cd Java

 

c:\Java>cd RandomFolder

 

c:\Java\RandomFolder>path=C:\Java\jdk1.6.0_30\bin;

 

c:\Java\RandomFolder>javac -d . Student.java

 

c:\Java\RandomFolder>javac -d . Marks.java

 

c:\Java\RandomFolder>java TopperMaterial.Marks

 

 Enter Details:

 

 Enter Student Name: Neil Mathew

 Enter Roll No: 3305

 Enter Marks of Physics: 80

 Enter Marks of Chemistry: 85

 Enter Marks of Mathematics: 87

 

 Showing Details:

 

 

 Student Name: Neil Mathew

 Enrollment No: 3305

 

 Marks of Physics:80

 Marks of Chemistry:85

 Marks of Mathematics:87

 Average Marks:84.0

 

 


P2: WAP to calculate factorial of a number using command line arguments.

posted Aug 19, 2012, 10:10 AM by Neil Mathew   [ updated Aug 19, 2012, 10:11 AM ]


INPUT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class FactorialOf
{
        public static void main(String []args)
        {
 
        if( args.length != 1 )
        {
        System.out.println(" Only one Variable required. ");
        return;
        }
 
 
        int n= Integer.parseInt(args[0]);
 
        System.out.print("\n Factorial of "+n+" is: ");
 
        int f=1;
        for(int i=1; i<=n; i++)
        {
        f*=i;
        }
 
        System.out.println(f);
 
        }
}
 



OUTPUT:

 



C:\Java 1.6\jdk1.6.0_30\bin>javac src/FactorialOf.java

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 7

 

 Factorial of 7 is: 5040

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 3

 

 Factorial of 3 is: 6

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 9

 

 Factorial of 9 is: 362880

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 9 2

 Only one Variable required.

 

P1: WAP to find greatest of three numbers.

posted Aug 19, 2012, 10:02 AM by Neil Mathew   [ updated Aug 19, 2012, 10:09 AM ]


INPUT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class BiggerOf
{
        public static void main( String thenumbers[] )
        {
        
        //To ensure only three inputs are added:
        //Exception Handling:
        if( thenumbers.length != 3 )
        {
        System.out.println("Only Three Numbers at a time!");
        return;
        }
        
        int number1 = Integer.parseInt(thenumbers[0]);
        int number2 = Integer.parseInt(thenumbers[1]);
        
        int number3 = Integer.valueOf(thenumbers[2]);
        
        if( number1 > number2 && number1 > number3)             
        System.out.println("Number "+number1+
                                        " is the bigger of the three!");
        
        else if( number2 > number1 && number2 > number3)                
        System.out.println("Number "+number2+
                                        " is the bigger of the three!");
        
        else    
        System.out.println("Number "+number3+
                                        " is the bigger of the three!");
        
        
        }
}
 


OUTPUT:


C:\Java 1.6\jdk1.6.0_30\bin>javac src/BiggerOf.java

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf

Only Three Numbers at a time!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 16 2 34

Number 34 is the bigger of the three!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 13 24 7

Number 24 is the bigger of the three!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 19 5 3

Number 19 is the bigger of the three!

 

C:\Java 1.6\jdk1.6.0_30\bin>java BiggerOf 1 0

Only Three Numbers at a time!

 



0 Basic Setup

posted Aug 8, 2012, 10:02 AM by Neil Mathew   [ updated Aug 19, 2012, 9:58 AM ]



Bit sleepy and uninterested, so expect the content here to be messy.

First things first, you'll need to download the JDK (currently using jdk 1.6) 
and have a command prompt or a UNIX shell on your system.

Once that's done, find the location of the bin folder of your JDK folder.

@Home: 
C:\Java 1.6\jdk1.6.0_30\bin

@Amity: 
C:\Program Files\Java\jdk1.6.0_30\bin


(If C Drive is restricted through My Computer, Open Run, and type c:\)

Copy this address and type the line below to direct to its folder.
cd <copied_address_pasted> 


USEFUL DOS COMMANDS:

CLS                     :    Clear Screen

CD <path>            :    Change Directory

CD..                     :    Move Back One Directory

TITLE <name>    :    Change Title of cmd window

%TIME%              :    Show Time


And as for compiling and execution, take example of a class called BiggerOf.

Javac compiles and makes a .class file in the same directory as its java equivalent.

Java executes that .class file, by default, from its source folder, ..bin/
(set the classpath to the source of the java application, and you can execute from any folder)



C:\Java 1.6\jdk1.6.0_30\bin>
    path = C:\Java 1.6\jdk1.6.0_30\bin\;


  C:\Java 1.6\jdk1.6.0_30\bin> cd src\


C:\Java 1.6\jdk1.6.0_30\bin\src> javac BiggerOf.java


C:\Java 1.6\jdk1.6.0_30\bin\src> java BiggerOf 16 2 34


Number 34 is the bigger of the three!




(Screenshot of how to copy & paste into the command prompt)












1-8 of 8