Java Lab Manual
Java Lab Manual
SUBJECT CODE: 10MCA37 HRS/WEEK: 3 TOTAL HRS: 42 I.A MARKS: 50 EXAM MARKS: 50 EXAM HRS: 3
1. a. Write a JAVA Program to demonstrate Constructor Overloading and Method overloading. b. Write a JAVA Program to implement Inner class and demonstrate its Access Protections. 2. a. Write a JAVA Program to demonstrate Inheritance. b. Write a JAVA Program to demonstrate Exception Handling (Using Nested try catch and finally). 3. Write a JAVA program which has i. A Class called Account that creates account with 500Rs minimum balance, a deposit() method to deposit amount, a withdraw() method to withdraw amount and also throws LessBalanceException if an account holder tries to withdraw money which makes the balance become less than 500Rs. ii. A Class called LessBalanceException which returns the statement that says withdraw amount (___Rs) is not valid. iii. A Class which creates 2 accounts, both account deposit money and one account tries to withdraw more money which generates a LessBalanceException take appropriate action for the same. 4. Write a JAVA program using Synchronized Threads, which demonstrates Producer Consumer concept. 5. Write a JAVA program which has i. A Interface class for Stack Operations ii. A Class that implements the Stack Interface and creates a fixed length Stack. iii. A Class that implements the Stack Interface and creates a Dynamic length Stack. iv. A Class that uses both the above Stacks through Interface reference and does the Stack operations that demonstrates the runtime binding. 6. Write i. ii. iii. a JAVA program which has 2 classes which initializes a String in its constructor A Generic class with 2 type Parameters Create a Generic Class reference for the above 2 Class and try to print the message inside the constructor (Use to string method).
7. 8.
Write JAVA programs which demonstrates utilities of LinkedList Class Write a JAVA Program which uses FileInputStream / FileOutPutStream Classes.
OURPUT
OUTPUT
2. b. WRITE A JAVA PROGRAM TO DEMONSTRATE EXCEPTION HANDLING (USING NESTED TRY CATCH AND FINALLY).
SOURCE CODE: class NestTry { public static void main(String args[]) { try { int a=args.length; int b=42/a; System.out.println("a = "+a); try{ if(a==1) a=a/(a-a); // division bt zero if(a==2){ int c[]={1}; c[32]=89; } }catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index out-of-bounds: "+e); } }catch(ArithmeticException e) { System.out.println("Divide by 0: "+e); } finally { System.out.println("Finally block executed:"); }}}
OUTPUT
public static void main(String arg[])throws IOException { Account ob[]=new Account[10]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); double amt; String name; int ch; int an; boolean b=true,f=false; int k; while(b) { System.out.println("BANKING OPERATION"); System.out.println("1: Opening New Account"); System.out.println("2: Deposit"); System.out.println("3: Withdraw"); System.out.println("4: Balance"); System.out.println("5: Exit"); System.out.println("Enter ur choice"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: System.out.println("Opening New Account"); System.out.print("Enter ur Name: "); name=br.readLine(); System.out.println("Enter initial amount(>Rs. 500/-)"); amt=Double.parseDouble(br.readLine()); ob[i]=new Account(amt,name);i++; break; case 2: System.out.println("Enter account number"); an=Integer.parseInt(br.readLine());
10
OUTPUT
11
12
13
14
OUTPUT
15
16
17
18
19
20
OUTPUT
21
OUTPUT
22
import java.io.*; public class FileInStreamDemo { public static void main(String[] args) { // create file object File file = new File("DevFile.txt"); int ch; StringBuffer strContent = new StringBuffer(""); FileInputStream fin = null; try { fin = new FileInputStream(file); while ((ch = fin.read()) != -1) strContent.append((char) ch); fin.close(); } catch (FileNotFoundException e) { System.out.println("File " + file.getAbsolutePath() + " could not be found on filesystem"); } catch (IOException ioe) { System.out.println("Exception while reading the file" + ioe); } System.out.println("File contents :"); System.out.println(strContent); } }
OUTPUT
OR import java.io.*; public class FileOutStreamDemo { public static void main(String[] args) { FileOutputStream out; // declare a file output object PrintStream p; // declare a print stream object try { // Create a new file output stream // connected to "DevFile.txt"
23
OUTPUT
24
25
OUTPUT
26
27
28
29
OUTPUT
30
OUTPUT
31
32
33
34
35
36
37
38