Write A Java Program That Creates An Object and Initializes Its Data Members Using Constructor. Use Constructor Overloading Concept
Write A Java Program That Creates An Object and Initializes Its Data Members Using Constructor. Use Constructor Overloading Concept
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;
// null constructor
student()
{
name="raju";
regno=12345;
marks1=56;
marks2=47;
marks3=78;
}
// parameterized constructor
student(String n,int r,int m1,int m2,int m3)
{
name=n;
regno=r;
marks1=m1;
marks2=m2;
marks3=m3;
}
// copy constructor
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****************************
c:\jdk1.6.0_26\bin>javac studentdemo.java
c:\jdk1.6.0_26\bin>java studentdemo
raju 12345 56 47 78
john 34266 58 96 84
raju 12345 56 47 78
2. 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/-
3. Write a Java program to sort the elements using bubble sort.
import java.util.Scanner;
****************************OUTPUT****************************
4. Write a Java program to search an element using binary search.
import java.util.Scanner;
first = 0;
last = n-1;
middle = (first+last)/2;
****************************OUTPUT****************************
5. Write a Java program that counts the number of objects created by
using static variable.
class VariableDemo
{
static int count=0;
public void increment()
{
count++;
}
public static void main(String args[])
{
VariableDemo obj1=new VariableDemo();
VariableDemo obj2=new VariableDemo();
obj1.increment();
obj2.increment();
System.out.println("Obj1: count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}
****************************OUTPUT****************************
Obj1: count is=2
Obj2: count is=2
6. Write a Java program to count the frequency of words, characters in
the given line of text.
import java.util.Scanner;
****************************OUTPUT****************************
Enter a String : This is GFGC Raibag
Total Number of Words in Entered Sentence is 4
Output:
finally block is always executed
Exception in thread main java.lang.ArithmeticException: / by zero
8. 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
9. 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*****************************
C:\jdk1.6.0_26\bin>javac MultilevelDemo.java
Note: MultilevelDemo.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\jdk1.6.0_26\bin>java MultilevelDemo
Enter rollno
12345
Enter name
Avinash
Enter marks :
54
78
46
Roll No =12345
Name =Avinash
m1=54
m2=78
m3=46
Total marks :178.0
10. 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******************************
C:\jdk1.6.0_26\bin>javac vectordemo.java
C:\jdk1.6.0_26\bin>java vectordemo
Vector Size :6
Vector apacity :10
The elements of a vector are :
zero
one
two
oops
three
four
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:
Arithmetic Exception occurs
rest of the code
13. 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
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();
}
}
}
15. 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****************************
thread A is started:
thread B is started:
thread C is started:
from thread A:i=1
from thread B:j=1
from thread C:k=1
from thread A:i=2
from thread B:j=2
from thread C:k=2
from thread A:i=3
from thread B:j=3
from thread C:k=3
from thread A:i=4
from thread B:j=4
from thread C:k=4
from thread A:i=5
from thread B:j=5
from thread C:k=5
exit from thread A:
exit from thread B:
exit from thread C:
16. Illustrate thread creation by implementing runnable interface.
import java.lang.Runnable;
class Runnabletest
{
public static void main(String arg[])
{
X R=new X();
Thread T=new Thread(R);
T.start();
}
}
*********************************OUTPUT****************************
Thread X:1
Thread X:2
Thread X:3
Thread X:4
Thread X:5
Thread X:6
Thread X:7
Thread X:8
Thread X:9
End of Thread X