34
Java Programming Laboratory 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 protection. 2. Write a program in Java for String handling which performs the following: i) Checks the capacity of StringBuffer objects. ii) Reverses the contents of a string given on console and converts the resultant string in upper case. iii) Reads a string from console and appends it to the resultant string of ii. 3. a.Write a JAVA Program to demonstrate Inheritance. b. Simple Program on Java for the implementation of Multiple inheritance using interfaces to calculate the area of a rectangle and triangle. 4. 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. 5. Write a JAVA program using Synchronized Threads, which demonstrates Producer Consumer concept. 6. Write a JAVA program to implement a Queue using user defined Exception Handling (also make use of throw, throws.). 7. Complete the following: 1. Create a package named shape. 2. Create some classes in the package representing some common shapes like Square, Triangle, and Circle. 3. Import and compile these classes in other program. 8. Write a JAVA Program a. Create an enumeration Day of Week with seven values SUNDAY through SATURDAY. Add a method is Workday( ) to the DayofWeek class that returns true if the value on which it is called is MONDAY through FRIDAY. For example, the call DayOfWeek.SUNDAY.isWorkDay ( ) returns false.

JAVA Lab Sorce Codes

Embed Size (px)

DESCRIPTION

JAVA

Citation preview

CG Lab Source Codes

Java Programming Laboratory

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 protection.

2. Write a program in Java for String handling which performs the following:i) Checks the capacity of StringBuffer objects.ii) Reverses the contents of a string given on console and converts the resultant string in upper case.iii) Reads a string from console and appends it to the resultant string of ii.

3. a.Write a JAVA Program to demonstrate Inheritance.b. Simple Program on Java for the implementation of Multiple inheritance using interfaces to calculate the area of a rectangle and triangle.

4. Write a JAVA program which hasi. 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.

5. Write a JAVA program using Synchronized Threads, which demonstrates Producer Consumer concept.

6. Write a JAVA program to implement a Queue using user defined Exception Handling (also make use of throw, throws.).

7. Complete the following:1. Create a package named shape.2. Create some classes in the package representing some common shapes like Square, Triangle, and Circle.3. Import and compile these classes in other program. 8. Write a JAVA Programa. Create an enumeration Day of Week with seven values SUNDAY through SATURDAY. Add a method is Workday( ) to the DayofWeek class that returns true if the value on which it is called is MONDAY through FRIDAY. For example, the call DayOfWeek.SUNDAY.isWorkDay ( ) returns false.

9. Write a JAVA program which hasi. A Interface class for Stack Operationsii. 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.

10. Write a JAVA program to print a chessboard pattern.

11. Write a JAVA Program which uses FileInputStream / FileOutPutStream Classes.

12. Write JAVA programs which demonstrates utilities of LinkedList Class.

13. Write a JAVA program which uses Datagram Socket for Client Server Communication.

14. Write a JAVA applet program, which handles keyboard event.1a)class Rectangle{int area,length,breadth;

Rectangle(){length=0;breadth=0;}

Rectangle(int length,int breadth){this.length=length;this.breadth=breadth;}

Rectangle(Rectangle r){length=r.length;breadth=r.breadth;}

Rectangle(int a){length=breadth=a;}

void ComputeArea(){area=length*breadth;}

int ComputeArea(int lenc,int brec){return((length+lenc)*(breadth+brec));}

int ComputeArea(Rectangle r){return((length+r.length)*(breadth+r.breadth));}

void display(){System.out.println(area);}

public static void main(String args[]){int a;

Rectangle r=new Rectangle();Rectangle r1=new Rectangle(5);Rectangle r2=new Rectangle(4,5);Rectangle r3=new Rectangle(r2);

r.ComputeArea();r1.ComputeArea();r2.ComputeArea();r3.ComputeArea();

System.out.println("Area of all the Rectangles =");

r.display();r1.display();r2.display();r3.display();

System.out.println("Area of r2 with length and breadth incremented by 2:"+r2.ComputeArea(2,2));System.out.println("Area of r2 and r3:"+r2.ComputeArea(r3));}}

1b)class Outer{int out=1000;

public int outer_x=100;private int outer_y=200;protected int outer_z=300;

void test(){Inner inner=new Inner();inner.display();}

class Inner{public int inner_x=1000;private int inner_y=2000;protected int inner_z=3000;

void display(){System.out.println("Inside inner class");System.out.println("outer_x:"+outer_x);System.out.println("outer_y:"+outer_y);System.out.println("outer_z:"+outer_z);System.out.println("outer_x:"+outer_x);System.out.println("outer_y:"+outer_y);System.out.println("outer_z:"+outer_z);}}}

class DemoInner{public static void main(String[] args){Outer outer=new Outer();outer.test();}}

2)import java.util.Scanner;class Ex2{public static void main(String args[]){StringBuffer sb=null;Scanner s=new Scanner(System.in);sb=new StringBuffer(s.next());System.out.println("String Buffer Capacity:"+sb.capacity());StringBuffer sbr=sb.reverse();String s1=new String(sbr);System.out.println("Reverse of given String:"+sbr);sbr=new StringBuffer(s1.toUpperCase());System.out.println("Upper case of reverse String "+sbr);sbr=sbr.append(s.next());System.out.println("String after appending:"+sbr);}}

3a)class Box{double width;double height;double depth;

Box(Box ob){width=ob.width;height=ob.height;depth=ob.depth;}

Box(double w,double h,double d){width=w;height=h;depth=d;}Box(){width=-1;height=-1;depth=-1;}

Box(double len){width=height=depth=len;}

double volume(){return width*height*depth;}}

class BoxWeight extends Box{double weight;

BoxWeight(double w,double h,double d,double wt){super(w,h,d);weight=wt;}}

class DemoBoxWeight{public static void main(String args[]){BoxWeight mybox1 = new BoxWeight(10,20,15,34.3);BoxWeight mybox2 = new BoxWeight(2,3,4,0.076);double vol;

vol=mybox1.volume();System.out.println("Volume of mybox1 is :" +vol);System.out.println("Weight of mybox1 is :" +mybox1.weight);

vol=mybox2.volume();System.out.println("Volume of mybox2 is :" +vol);System.out.println("Weight of mybox2 is :" +mybox2.weight);}}

3b)interface TwoD{void calArea();}

class TwoDim{float s1,s2;float area;

void displayArea(){System.out.println("Area="+area);}}

class Triangle extends TwoDim implements TwoD{Triangle(int a,int b){s1=a;s2=b;}

public void calArea(){area=0.5f*s1*s2;}}

class Rectangle extends TwoDim implements TwoD{Rectangle(int a,int b){s1=a;s2=b;}

public void calArea(){area=s1*s2;}}

class Ex3b{public static void main(String args[]){Rectangle r=new Rectangle(5,4);Triangle t=new Triangle(5,6);

r.calArea();t.calArea();

r.displayArea();t.displayArea();}}

4)import java.io.*;

class LessBalanceException extends Exception{double amt;LessBalanceException(double wamt){amt=wamt;System.out.println("withdraw not possible"+amt);}}

class Account{public double bal;

Account(){bal=500.0;}

public void deposit(double damt){bal=bal+damt;}

public void withdraw(double wamt) throws LessBalanceException{if((bal-wamt)r||r==-1){throw(new QueueEmptyException());}else{return(a[f++]);}}

void display() throws QueueEmptyException{if(f>r||r==-1)throw(new QueueEmptyException());else{for(int i=f;i