Dept. of MCA/Sir MVIT/ Bangalore 1 JAVA PROGRAMMING LABORATORY [10MCA37] JAVA PROGRAMMING LABORATORY SUBJECT CODE: 10MCA37 I.A MARKS: 50 HRS/WEEK: 3 EXAM MARKS: 50 TOTAL HRS: 42 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 a JAVA program which has i. 2 classes which initializes a String in its constructor ii. A Generic class with 2 type Parameters iii. Create a Generic Class reference for the above 2 Class and try to print the message inside the constructor (Use to string method). 7. Write JAVA programs which demonstrates utilities of LinkedList Class 8. Write a JAVA Program which uses FileInputStream / FileOutPutStream Classes.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Dept. of MCA/Sir MVIT/ Bangalore 1
JAVA PROGRAMMING LABORATORY [10MCA37]
JAVA PROGRAMMING LABORATORY SUBJECT CODE: 10MCA37 I.A MARKS: 50 HRS/WEEK: 3 EXAM MARKS: 50 TOTAL HRS: 42 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 a JAVA program which has
i. 2 classes which initializes a String in its constructor
ii. A Generic class with 2 type Parameters
iii. Create a Generic Class reference for the above 2 Class and try to print the message inside the constructor (Use to string method).
7. Write JAVA programs which demonstrates utilities of LinkedList Class
8. Write a JAVA Program which uses FileInputStream / FileOutPutStream
Classes.
Dept. of MCA/Sir MVIT/ Bangalore 2
JAVA PROGRAMMING LABORATORY [10MCA37]
9. Write a JAVA Program which writes a object to a file (use transient variable
also).
10. Write a JAVA program which uses Datagram Socket for Client Server
Communication.
11. Write JAVA Applet programs which handles MouseEvent
12. Write JAVA Applet programs which handles KeyBoardEvent
13. Write a JAVA program which implements RMI.
14. Write a Swing Application which uses
i. JTabbed Pane ii. Each Tab should use JPanel, which includes any one component given
below in each Panel
iii. ComboBox / List / Tree / Radiobutton
Note: All the above Components should listen to any one of their respective
events and print appropriate message.
Dept. of MCA/Sir MVIT/ Bangalore 3
JAVA PROGRAMMING LABORATORY [10MCA37]
1. a. WRITE A JAVA PROGRAM TO DEMONSTRATE CONSTRUCTOR OVERLOADING AND METHOD OVERLOADING.
System.out.println("CONSTRUCTOR AND METHOD OVERLOADING\n");
Add ob=new Add();
ob.addition();
Add ob1=new Add(100,300);
ob1.addition(ob1);
}
}
Dept. of MCA/Sir MVIT/ Bangalore 4
JAVA PROGRAMMING LABORATORY [10MCA37]
OUTPUT
Dept. of MCA/Sir MVIT/ Bangalore 5
JAVA PROGRAMMING LABORATORY [10MCA37]
1. b. WRITE A JAVA PROGRAM TO IMPLEMENT INNER CLASS AND DEMONSTRATE ITS ACCESS PROTECTIONS.
SOURCE CODE:
class Outer
{
private int x=100;
int y=200;
void test()
{
Inner ob=new Inner();
ob.display();
}
class Inner
{
int z=300;
void display()
{
System.out.println("Outer class private data x = "+x);
System.out.println("Outer class default data y = "+y);
System.out.println("Inner class data z = "+z);
z=x+y+z;
System.out.println("Sum = "+z);
}
}
}
class InnerDemo
{
public static void main(String arg[])
{
System.out.println("INNER CLASS ACCESS PROTECTION\n");
Outer ob=new Outer();
ob.test();
}
}
OURPUT
Dept. of MCA/Sir MVIT/ Bangalore 6
JAVA PROGRAMMING LABORATORY [10MCA37]
2. a. WRITE A JAVA PROGRAM TO DEMONSTRATE INHERITANCE.
SOURCE CODE:
class A
{
int a;
void showA()
{
System.out.println("A = "+a);
}
}
class B extends A
{
int b;
void showB()
{
System.out.println("B = "+b);
}
void sum()
{
int z;
z=a+b;
showA();
showB();
System.out.println("Sum = "+z);
}
}
class InheritDemo
{
public static void main(String arg[])
{
System.out.println("DEMONSTRATION OF INHERITANCE\n");
B ob=new B();
ob.a=10;
ob.b=20;
ob.sum();
}
}
OUTPUT
Dept. of MCA/Sir MVIT/ Bangalore 7
JAVA PROGRAMMING LABORATORY [10MCA37]
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
Dept. of MCA/Sir MVIT/ Bangalore 8
JAVA PROGRAMMING LABORATORY [10MCA37]
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.
SOURCE CODE:
import java.io.*;
class LessBalanceException extends Exception
{
LessBalanceException(double amt)
{
System.out.println("withdrawing "+amt+" is invalid ");