Java Lab Cbcs
Java Lab Cbcs
Write a Java program that creates an object and initializes its data
members using constructor. Use constructor overloading concept.
import java.lang.*;
class student
{
String name;
int regno;
int marks1,marks2,marks3;
student()
{
name="raju";
regno=12345;
marks1=56;
marks2=47;
marks3=78;
}
student(student s)
{
name=s.name;
regno=s.regno;
marks1=s.marks1;
marks2=s.marks2;
marks3=s.marks3;
}
void display()
{
System.out.println(name + "\t" +regno+ "\t"
+marks1+ "\t" +marks2+ "\t" + marks3);
}
}
class studentdemo
{
public static void main(String arg[])
{
student s1=new student();
student s2=new student("john",34266,58,96,84);
student s3=new student(s1);
s1.display();
s2.display();
s3.display();
}
}
************************OUTPUT****************************
2. Write your own simple Account class. You should be able to make
deposits and withdrawals and read out the balance — a private
double variable. Member functions should be: void
Account::withdraw (const double &amount); //Take from account
void Account::deposit(const double &amount); // Put into account
double account::balance(void); //Return the balance Make sure that
the Account constructor function initializes the balance to zero. If you
like, add an overloaded constructor function to set an initial balance.
class BankAccount
{
private String accountNum;
private double balance;
first.transferFundsA(second, 5000);
System.out.println("\nAfter \"first.transferFunds(second,
5000)\" ...");
System.out.printf("Account #%s has new balance of
$%.2f%n",
first.getAccount(), first.getBalance());
System.out.printf("Account #%s has new balance of
$%.2f%n",
second.getAccount(), second.getBalance());
second.transferFundsB(first, 10000);
System.out.println("\nAfter \"second.transferFunds(first,
10000)\" ...");
System.out.printf("Account #%s has new balance of
$%.2f%n",
first.getAccount(), first.getBalance());
System.out.printf("Account #%s has new balance of
$%.2f%n",
second.getAccount(), second.getBalance());
}
}
***************************OUTPUT*****************************
3. Write a java program to calculate gross salary & net salary taking the
following data. Input: empno, empname, basic Process: DA=50%of
basic HRA=12%of basic CCA=Rs240/- PF=10%of basic PT=Rs100/-
import java.util.*;
class Employee
{
public static void main(String []args)
{
float DA, HRA, PF, grass_sal, net_sal;
float CCA=240f, PT=100f;
Scanner sc= new Scanner(System.in);
DA = (0.5f)* basic_sal;
HRA = (0.12f)*basic_sal;
PF = (0.1f)*basic_sal;
grass_sal=(basic_sal + DA + HRA);
net_sal=(grass_sal - CCA - PT - PF);
System.out.println("Employee Gross Salary : " +grass_sal);
System.out.println("Employee Net Salary : " +net_sal);
}
}
****************************OUTPUT****************************
4. Write a Java program to sort the elements using bubble sort.
import java.util.Scanner;
class BubbleSort
{
public static void main(String args[])
{
int n, i, j, temp;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
****************************OUTPUT****************************
5. Write a Java program to search an element using binary search.
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int n, i, search, first, last, middle;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
first = 0;
last = n-1;
middle = (first+last)/2;
****************************OUTPUT****************************
6. Write a Java program that counts the number of objects created by
using static variable.
class CountObject
{
private static int count;
public CountObject()
{
count++;
}
public static void main(String args[])
{
CountObject ob1 = new CountObject();
CountObject ob2 = new CountObject();
CountObject ob3 = new CountObject();
CountObject ob4 = new CountObject();
CountObject ob5 = new CountObject();
System.out.print("Total Number of Objects: " +
CountObject.count);
}
}
****************************OUTPUT****************************
7. Write a Java program to count the frequency of words, characters in
the given line of text.
import java.util.Scanner;
class Frequency_of_Characters_in_String
{
public static void main(String args[])
{
String str;
Scanner scan = new Scanner(System.in);
****************************OUTPUT****************************
8. Write a java program to identify the significance of finally block in
handling exceptions.
class TestFinallyBlock1
{
public static void main(String args[])
{
try
{
int data=25/0;
System.out.println(data);
}
catch(NullPointerException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
****************************OUTPUT****************************
9. Write a java program to access member variables of classes defined in
user created package.
*******************************OUTPUT**************************
C:\jdk1.6.0_26\bin>javac p1\Student.java
C:\jdk1.6.0_26\bin>javac StudentTest.java
C:\jdk1.6.0_26\bin>java StudentTest
regno = 123
name = xyz
10. Write a Java Program to implement multilevel inheritance by
applying various access controls to its data members and methods.
import java.io.DataInputStream;
class Student
{
private int rollno;
private String name;
DataInputStream dis=new DataInputStream(System.in);
public void getrollno()
{
try
{
System.out.println("Enter rollno ");
rollno=Integer.parseInt(dis.readLine());
System.out.println("Enter name ");
name=dis.readLine();
}
catch(Exception e){ }
}
void putrollno()
{
System.out.println("Roll No ="+rollno);
System.out.println("Name ="+name);
}
}
void putmarks()
{
System.out.println("m1="+m1);
System.out.println("m2="+m2);
System.out.println("m3="+m3);
}
}
class MultilevelDemo
{
public static void main(String arg[])
{
Result r=new Result();
r.getrollno();
r.getmarks();
r.putrollno();
r.putmarks();
r.compute_display();
}
}
**************************OUTPUT*******************************
11. Write a Java Program to implement Vector class and its methods.
import java.lang.*;
import java.util.Vector;
import java.util.Enumeration;
class vectordemo
{
public static void main(String arg[])
{
Vector v=new Vector();
v.addElement("one");
v.addElement("two");
v.addElement("three");
v.insertElementAt("zero",0);
v.insertElementAt("oops",3);
v.insertElementAt("four",5);
System.out.println("Vector Size :"+v.size());
System.out.println("Vector apacity :"+v.capacity());
System.out.println(" The elements of a vector are :");
Enumeration e=v.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement() +" ");
System.out.println();
System.out.println("The first element is : " +v.firstElement());
System.out.println("The last element is : " +v.lastElement());
System.out.println("The object oops is found at position :
"+v.indexOf("oops"));
v.removeElement("oops");
v.removeElementAt(1);
System.out.println("After removing 2 elements ");
System.out.println("Vector Size :"+v.size());
System.out.println("The elements of vector are :");
for(int i=0;i<v.size();i++)
System.out.println(v.elementAt(i)+" ");
}
}
**************************OUTPUT******************************
12. Write a program to demonstrate use of user defined packages.
When the code is ready, the next job is compilation. We must compile
with package notation. Package notation uses –d compiler option as
follows.
C:\snr > javac -d . Tiger.java
The –d compiler option creates a new folder called forest and places
the Tiger.class in it. The dot (.) is an operating system's environment
variable that indicates the current directory. It is an instruction to the
OS to create a directory called forest and place the Tiger.class in it.
3rd Step: Now finally, write a program from the target directory
D:\vinay and access the package.
D:\vinay> notepad Animal.java
import forest.Tiger;
public class Animal
{
public static void main(String args[])
{
Tiger t1 = new Tiger ();
t1.getDetails("Everest", 50);
}
}
****************************OUTPUT****************************
14. Design stack and queue classes with necessary exception handling.
import java.util.*;
/* Class arrayStack */
class arrayStack
{
protected int arr[];
protected int top, size, len;
/* Class StackImplement */
public class StackImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Stack Test\n");
System.out.println("Enter Size of Integer Stack ");
int n = scan.nextInt();
case 2 :
try
{
System.out.println("Popped Element = " +
stk.pop());
}
catch (Exception e)
{
System.out.println("Error : " +
e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = " +
stk.peek());
}
catch (Exception e)
{
System.out.println("Error : " +
e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = " +
stk.isEmpty());
break;
case 5 :
System.out.println("Full status = " +
stk.isFull());
break;
case 6 :
System.out.println("Size = " +
stk.getSize());
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* display stack */
stk.display();
System.out.println("\nDo you want to continue (Type y
or n) \n");
ch = scan.next().charAt(0);
}
while (ch == 'Y'|| ch == 'y');
}
}
****************************OUTPUT****************************
Stack Test
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
4
Empty status = true
Stack = Empty
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to push
24
Stack = 24
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
1
Enter integer element to push
6
Stack = 6 24
****************************OUTPUT****************************
16. Write a Java program to illustrate basic calculator using grid layout
manager.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
for(int i=0;i<6;i++)
{
b1[i]=new Button(""+op2[i]);
add(b1[i]);
b1[i].setBackground(Color.pink);
b1[i].addActionListener(this);
}
add(t);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("+"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("-"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("*"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("%"))
{
p=Integer.parseInt(t.getText());
oper=str;
t.setText(str1="");
}
else if(str.equals("="))
{
str1="";
if(oper.equals("+"))
{
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p+q)));
}
else if(oper.equals("-"))
{
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p-q)));
}
else if(oper.equals("*"))
{
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p*q)));
}
else if(oper.equals("%"))
{
q=Integer.parseInt(t.getText());
t.setText(String.valueOf((p%q)));
}
}
else if(str.equals("C"))
{
p=0;q=0;
t.setText("");
str1="";
t.setText("0");
}
else
{
t.setText(str1.concat(str));
str1=t.getText();
}
}
}
****************************OUTPUT****************************
17. Illustrate creation of thread by extending Thread class.
import java.lang.Thread;
class A extends Thread
{
public void run()
{
System.out.println("thread A is started:");
for(int i=1;i<=5;i++)
{
System.out.println("\t from thread A:i="+i);
}
System.out.println("exit from thread A:");
}
}
class Threadtest
{
public static void main(String arg[])
{
new A().start();
new B().start();
new C().start();
}
}
****************************OUTPUT****************************
18. Illustrate thread creation by implementing runnable interface.
import java.lang.Runnable;
class X implements Runnable
{
public void run()
{
for(int i=1;i<10;i++)
{
System.out.println("\t Thread X:"+i);
}
System.out.println("End of Thread X");
}
}
class Runnabletest
{
public static void main(String arg[])
{
X R=new X();
Thread T=new Thread(R);
T.start();
}
}
****************************OUTPUT****************************