KEMBAR78
Java Practical Program Answers | PDF | String (Computer Science) | Software Development
0% found this document useful (0 votes)
4 views10 pages

Java Practical Program Answers

The document contains a series of Java programming exercises, including implementations of the StringBuffer class, Vector elements, multidimensional arrays, exception handling, applets, frames, and database operations. Each exercise provides a brief description followed by the corresponding Java code. The document serves as a guide for practicing various Java concepts and functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Java Practical Program Answers

The document contains a series of Java programming exercises, including implementations of the StringBuffer class, Vector elements, multidimensional arrays, exception handling, applets, frames, and database operations. Each exercise provides a brief description followed by the corresponding Java code. The document serves as a guide for practicing various Java concepts and functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1 Write a Program to implement all methods of StringBuffer Class.

Ans: import java.util.*;


-
class stringbuffer
{
public static void main(String x[])
{
StringBuffer s1 = new StringBuffer();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string:");
s1.append(sc.nextLine());
s1.setCharAt(3,'a');
System.out.print("String : "+s1);
s1.insert(0,"abc");
System.out.print("String : "+s1);
s1.setLength(5);
System.out.print("String : "+s1);
System.out.print("String : "+s1.substring(0,7));
}
}
2 Write a program to insert different elements in the Vector & display them.
Ans: import java.util.*;
-
class exam
{
public static void main(String x[])
{
Vector v1 = new Vector();
v1.addElement(new Integer(10));
v1.addElement(new Float(3.14f));
v1.addElement(new Double(2.777));
v1.addElement(new Integer(10));
v1.addElement("Ram");
System.out.print("Vector Elements are:"+v1);
}
}
3 Write a Program to implement all methods of StringBuffer Class.
Ans: import java.util.*;
-
class stringbuffer
{
public static void main(String x[])
{
StringBuffer s1 = new StringBuffer();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string:");
s1.append(sc.nextLine());
s1.setCharAt(3,'a');
System.out.print("String : "+s1);
s1.insert(0,"abc");
System.out.print("String : "+s1);
s1.setLength(5);
System.out.print("String : "+s1);
System.out.print("String : "+s1.substring(0,7));
}
}
4 Write a program to insert different elements in the Vector & display them.
Ans: import java.util.*;
-
class exam
{
public static void main(String x[])
{
Vector v1 = new Vector();
v1.addElement(new Integer(10));
v1.addElement(new Float(3.14f));
v1.addElement(new Double(2.777));
v1.addElement(new Integer(10));
v1.addElement("Ram");
System.out.print("Vector Elements are:");
for(int i=0;i<v1.size();i++)
{
System.out.print("\n"+v1.elementAt(i));
}
}
}
5 Write a program to implement multidimentional array.
Ans: import java.util.*;
-
class example
{
public static void main(String x[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of columns:");
int i = sc.nextInt();
System.out.print("Enter number of columns:");
int j = sc.nextInt();
int a[][] = new int[i][j];
System.out.print("Enter numbers in matrix:");
for(i=0;i<a.length;i++)
{
for(j=0;j<a[i].length;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.print("Your matrix is ready:\n");
for(i=0;i<a.length;i++)
{
for(j=0;j<a[i].length;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.print("\n");
}
}
}
6 Write a program to make use of character Wrapper class Methods.
Ans:
-
7 Write a program to implement different types of constructors to perform addition
of complex numbers ?
Ans:
-
8 Develop a program to calculate the room area and volume to illustrate the concept
of single inheritance.
Ans: class room
- {
float length=10,width=15,height=20;
void area()
{
System.out.print("Area of room is : "+length*width);
}
void volume()
{
System.out.print("Volume of room is : "+length*width*height);
}
}

class exam extends room


{
public static void main(String x[])
{
exam e1 = new exam();
e1.area();
e1.volume();
}
}
9 Demonstrate the use of interfaces to implement the concept of multiple inheritance.
Ans: interface A
- {
public void get();
}

interface B
{
public void set();
}

class demo implements A,B


{
public void set()
{
System.out.print("This is set() method of interface B");
}
public void get()
{
System.out.print("This is get() method of interface A");
}

public static void main(String x[])


{
demo d1 = new demo();
d1.set();
d1.get();
}
}
10 Implement a Program to create and import userdefined package in java.
Ans: //To create a package:-
- package MyPackage;
class MyClass
{
public void display()
{
System.out.print("Hello from MyClass in MyPackage");
}
}
//To import a package:-
import MyPackage.MyClass;

class A
{
public static void main(String x[])
{
MyClass abc = new MyClass();
abc.display();
}
}
11 Define an Exception called “NotMatchExceptions” that is thrown when a string is
not equals to “India”.Write a program that uses this exception.
Ans: import java.util.*;
-
class NoMatchException extends Exception
{
public NoMatchException(String a)
{
System.out.print(a);
}
}

class exam
{
public void display(String a,String b) throws NoMatchException
{
String s1=a;
String s2=b;
if(s1.equals(s2))
{
System.out.print("Welcome to the India");
}
else
{
throw new NoMatchException("String is not matched");
}
}
public static void main(String x[])
{
Scanner sc = new Scanner(System.in);
exam e1 = new exam();
String s1 = "India";
System.out.print("Enter the String:");
String s2 = sc.nextLine();
try
{
e1.display(s1,s2);
}
catch(Exception e)
{}
}
}
12 Design an applet/Frame to demonstrate the use of RadioButton and Checkbox.
Ans:
-
13 Design an applet/Frame to create form usingTextField,TextArea,Button,Area.
Ans: import java.awt.*;
- import java.applet.*;

public class demo extends Applet


{
public void init()
{
TextField tf=new TextField("Implement the components on the applet");
Label l1=new Label("Java Programming");
TextArea ta=new TextArea("Java is the object oriented programming language.\
nJava is the platform independent.\nIt is simple and easy to understand");
Button b1=new Button("Click here");

add(tf);
add(l1);
add(ta);
add(b1);
}
}
14 Develop an applet/Frame using List components to add names of 10 different cities.
Ans: import java.awt.*;
-
class exam extends Frame
{
exam()
{
setLayout(new FlowLayout());
List l1 = new List();
l1.add("Pune");
l1.add("Mumbai");
l1.add("Delhi");
l1.add("Nagpur");
l1.add("Aurangabad");
l1.add("Goa");
l1.add("Pimpalgaon");
l1.add("Amravati");
l1.add("Kashmir");
l1.add("Kolhapur");
add(l1);
}
public static void main(String x[])
{
exam e1 = new exam();
e1.setSize(300,400);
e1.setTitle("Frame demo");
e1.setVisible(true);
}
}
15 Write a program which creates Menu of Different colors and Disable menu item for
Black color.
Ans:
-
16 Write a java program to create a table of Name of Student,Percentage and Grade
of 10 Students using JTable.
Ans:
-
17 Write a JTree Program to show root Directory and its subfolders of your System.
Ans: import java.awt.*;
- import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

class JTreeDemo extends JFrame


{
JTreeDemo()
{
Container ct=getContentPane();

DefaultMutableTreeNode root=new DefaultMutableTreeNode("TOP");


DefaultMutableTreeNode A=new DefaultMutableTreeNode("A");
DefaultMutableTreeNode B =new DefaultMutableTreeNode("B");
DefaultMutableTreeNode A1=new DefaultMutableTreeNode("A1");
DefaultMutableTreeNode A2=new DefaultMutableTreeNode("A2");
DefaultMutableTreeNode B1 =new DefaultMutableTreeNode("B1");
DefaultMutableTreeNode B2 =new DefaultMutableTreeNode("B2");

DefaultMutableTreeNode A11 =new DefaultMutableTreeNode("A11");


DefaultMutableTreeNode A12 =new DefaultMutableTreeNode("A12");
DefaultMutableTreeNode B22 =new DefaultMutableTreeNode("B22");
DefaultMutableTreeNode B23 =new DefaultMutableTreeNode("B23");

A.add(A1);
A.add(A2);
B.add(B1);
B.add(B2);

A1.add(A11);
A1.add(A12);

B2.add(B22);
B2.add(B23);

root.add(A);
root.add(B);

JTree tree=new JTree(root);


ct.add(tree);
}
public static void main(String x[])
{
JTreeDemo fd=new JTreeDemo();
fd.setVisible(true);
fd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fd.setSize(700,500);
}
}
18 Write a program to generate KeyEvent when a key is pressed and display “Key
Pressed” message.
Ans: import java.awt.*;
- import java.awt.event.*;

class keylist extends Frame implements KeyListener


{
Label lb;
keylist()
{
setLayout(new FlowLayout());
lb = new Label();
Font f=new Font("Times New Roman",Font.BOLD,20);
lb.setFont(f);
add(lb);
addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
lb.setText("key is pressed");
}
public void keyReleased(KeyEvent e)
{
lb.setText("Relesed");
}
public void keyTyped(KeyEvent e)
{
lb.setText(e.getKeyChar()+ "is typed.");
}

public static void main(String x[])


{
keylist f1 = new keylist();
f1.setSize(500, 500);
f1.setVisible(true);
f1.setTitle("FileDialog demo");
}
}
19 Write a Program using JTextField to perform the addition of two numbers.
Ans:
-
20 Write a program to create a student table in database and insert a record in a
Student table.
Ans: //Create a table student:-
- Create table Student
( Rollno number(3),
Name varchar2(30));

//Create a table student:-


import java.sql.*;

class d1
{
public static void main(String ar[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:SYCM1");
String s="insert into Table1 values(22,'Abhi')";
Statement st=c.createStatement();
int r=st.executeUpdate(s);
System.out.println("inserted");
st.close();
c.close();
}
}
21 Write a program to create a student table in database and implementing a Delete
Statement.
Ans: //Create a table student:-
- Create table Student
( Rollno number(3),

Name varchar2(30));
//Create a table student:-
import java.sql.*;

class d2
{
public static void main(String ar[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:SYCM1");
String s="delete from Table1 where Roll_no=22";
Statement st=c.createStatement();
int r=st.executeUpdate(s);
st.close();
c.close();
}
}

You might also like