Java Programming Lab-2
Java Programming Lab-2
15
19 Design an applet to do
the following tasks:
a) To output the
question “WHO IS
THE PRIME
MINISTER OF
INDIA?”
c) To print “TRY
AGAIN” and if the
answer is not correct.
d) To display the
correct answer, if the
answer is wrong even
after third attempt.
20 Design a java
program to write an
applet that computes
the payment of loan
which is based on the
amount of loan,
interest rate and
number of months. It
takes one parameter
from browser which
is called Monthly
Rate. If it is True
then interest rate is
per month else it is
21 - Design a java
program to write an
applet with following
AWT components :
textarea and button.
24 Design a program to
implement card
layout in applets.
25 Program in java to
demonstrate file
writer, file reader
and buffer reader.
27 Design a program in
java to implement
client-server TCP/IP
socket application
which exchanges
strings among them .
28 program to create
database connectivity
using jdbc, create
statement object
create resultset object
and display.
29 p program to create
database connectivity
using jdbc, create
statement object
create resultset object
and Insert a record
using create
statement.
30 program to create
database connectivity
using jdbc, create
statement object
create resultset object
and update a record
using create
statement.
31 program to create
database connectivity
using jdbc, create
Java Programming Lab
anubhav
IT-3
03476803121
Page 7
statement object
create resultset object
and delete a record
using create
statement.
Practical 1
Aim : Design a java program to find the biggest of three given integers. Description: The name of
class is Largest, having data members num1, num2, num3, res. Member functions of class are
inputData(), findLargest() and displayLargest(). Make separate class to create void main.
Code :
class Largest {
int num1, num2, num3, result;
void input() {
num1 = 8;
num2 = 9;
num3 = 10;
}
void largest() {
if (num1 > num2 && num1 > num3) {
[Link]("Largest : " + result);
result = num1;
} else if (num2 > num1 && num2 > num3) {
result = num2;
[Link]("Largest : " + result);
} else {
result = num3;
Java Programming Lab
anubhav
IT-3
03476803121
Page 8
[Link]("Largest : " + result);
}
}
}
class Execute {
public static void main(String[] args) {
Largest l = new Largest();
[Link]();
[Link]();
}
}
Output : -
Code :
class Shapes {
double len;
double bre;
double result;
Shapes() {
len = 0.0;
bre = 0.0;
}
Shapes(double l) {
len = l;
bre = 0.0;
}
Shapes(double l, double b) {
len = l;
bre = b;
}
class Execute2 {
public static void main(String a[]) {
double para1, para2;
para1 = 10.0;
para2 = 20.0;
Shapes s1 = new Shapes();
Shapes s2 = new Shapes(para1);
Shapes s3 = new Shapes(para1, para2);
[Link]();
[Link]();
[Link]();
}
}
Practical 3
Aim : Design a java program to demonstrate the use of nested class.
Code :
class Outer {
private int outer_x = 100;
void test() {
Inner inner = new Inner();
Java Programming Lab
anubhav
IT-3
03476803121
Page 12
[Link]();
[Link]();
}
class Inner {
private void hello() {
[Link]("Private Method Hello() of Inner Class");
}
void display() {
[Link]("Display: outer_x = " + outer_x);
Outer o = new Outer();
[Link]("Display: outer_x = " + o.outer_x);
}
}
}
class Execute3 {
public static void main(String args[]) {
Outer outer = new Outer();
[Link]();
}
}
Code :
class Quadratic {
double a, b, c, r1, r2, d;
void calculate_roots() {
d = b * b - 4 * a * c;
if (d > 0) {
[Link]("The roots of the equation are real and
different. \n");
r1 = -b + sqrt(abs(d)) / 2 * a;
r2 = -b - sqrt(abs(d)) / 2 * a;
[Link](" roots are : " + r1 + " " + r2);
} else if (d == 0) {
[Link]("The roots of the equation are real and
same. \n");
r1 = -b / 2 * a;
r2 = -b / 2 * a;
[Link](" roots are : " + r1 + " " + r2);
}
else{
[Link]("The roots of the equation are complex and
different. \n");
r1 = -b / 2 * a;
r2 = -b / 2 * a;
Java Programming Lab
anubhav
IT-3
03476803121
Page 15
[Link](r1 + " + i"+ d + "\n"+ r2+ " - i" + d);
}
}
}
class Execute4{
public static void main(String[] args) {
Quadratic q = new Quadratic();
q.input_equation(1.0,3.0,5.0);
q.calculate_roots();
}
}
Output :
Code :
class Fibonacci_with_recursion {
int n1 = 0, n2 = 1, n3 = 0;
class Fibonacci_without_recursion {
int n1 = 0, n2 = 1, n3 = 0;
class Execute5 {
public static void main(String[] args) {
Fibonacci_with_recursion f = new Fibonacci_with_recursion();
[Link]("Fibonacci with recursion : ");
Java Programming Lab
anubhav
IT-3
03476803121
Page 17
[Link](7);
Fibonacci_without_recursion f2 = new
Fibonacci_without_recursion();
[Link]("\n");
[Link]("Fibonacci without recursion : ");
f2.fibonaaci2(7);
}
}
Output :
Code :
import [Link].*;
class Prime {
int value, num;
int i, flag;
void inputNumber() {
Scanner sc = new Scanner([Link]);
[Link]("ENTER A NUMBER ");
value = [Link]();
}
void displayPrime() {
for (num = 2; num <= value; num++) {
flag = 0;
for (i = 2; i <= num - 1; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
[Link](num);
}
}
}
}
class Execute6 {
public static void main(String a[]) {
Prime o = new Prime();
Output :
import [Link].*;
class P_Check {
void check(String data) {
String rdata = "";
int i, len;
len = [Link]();
for (i = len - 1; i >= 0; i--)
rdata = rdata + [Link](i);
if ([Link](rdata))
[Link]("Entered string is a palindrome.");
else
[Link]("Entered string is not a palindrome.");
}
}
class Execute7 {
public static void main(String a[]) {
String name;
P_Check pc = new P_Check();
Scanner in = new Scanner([Link]);
[Link]("Enter a string");
name = [Link]();
[Link](name);
}
}
Aim : Design a java program that reads a line of integers, then display each integer and the sum of
all integers.
Code :
import [Link].*;
class IntData {
int arr[];
int i, sum;
IntData(int size) {
arr = new int[size];
sum = 0;
}
void input() {
Scanner sc = new Scanner([Link]);
for (i = 0; i < [Link]; i++) {
arr[i] = [Link]();
}
}
void display() {
for (i = 0; i < [Link]; i++) {
[Link](arr[i]);
}
}
void sumCal() {
for (i = 0; i < [Link]; i++) {
sum = sum + arr[i];
}
[Link]("SUM IS " + sum);
}
}
class Execute8 {
public static void main(String a[]) {
IntData obj = new IntData(5);
[Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 23
[Link]();
[Link]();
}
}
Output :
class Stack {
int stck[];
int tos;
Stack(int size) {
stck = new int[size];
tos = -1;
}
int pop() {
if (tos < 0) {
[Link]("Stack underflow.");
return 0;
} else
return stck[tos--];
}
}
class Execute9 {
public static void main(String args[]) {
Stack mystack1 = new Stack(5);
Stack mystack2 = new Stack(8);
[Link]("Stack in mystack1:");
for (int i = 0; i < 5; i++)
Java Programming Lab
anubhav
IT-3
03476803121
Page 25
[Link]([Link]());
[Link]("Stack in mystack2:");
for (int i = 0; i < 8; i++)
[Link]([Link]());
}
}
Output :
Aim : Design a java program to create an abstract class named Shape, that contains an empty
method called numberOfSides(). Provide four classes named Trapezoid, Triangle, Rectangle and
Hexagon such that each one of the classes contains only the method numberOfSides(), that contains
number of sides in each geometrical figure.
Code :
void area() {
double areasq;
s = 10.0;
areasq = s * s;
[Link]("Area of Square " + areasq);
}
}
void area() {
double arearect;
l = 10.0;
b = 2.0;
arearect = l * b;
[Link]("Area of Rectangle " + arearect);
}
}
class Execute10 {
public static void main(String a[]) {
// MyArea ma= new MyArea();
// [Link]();
MyArea sa = new SQArea();
[Link]();
MyArea ra = new RECTArea();
Java Programming Lab
anubhav
IT-3
03476803121
Page 27
[Link]();
}
}
Output :
class MyArea {
void area() {
[Link]("A concrete function called Area()");
}
}
class Execute11 {
public static void main(String a[]) {
MyArea ma; // Base class object
ma = new SQArea(); // Refering to SQArea
[Link](); // Calling SQArea method (Dynamic Ploymorphism)
ma = new RECTArea(); // Refering to RECTArea
Java Programming Lab
anubhav
IT-3
03476803121
Page 29
[Link](); // Calling RECTArea method (Dynamic Ploymorphism)
}
}
Output :
interface IntStack {
void push(int item); // Store an item.
class Execute12 {
public static void main(String args[]) {
DynStack mystack1 = new DynStack(5);
DynStack mystack2 = new DynStack(8);
// These loops cause each stack to grow.
for (int i = 0; i < 12; i++)
[Link](i);
for (int i = 0; i < 20; i++)
[Link](i);
[Link]("Stack in mystack1:");
for (int i = 0; i < 12; i++)
[Link]([Link]());
[Link]("Stack in mystack2:");
for (int i = 0; i < 20; i++)
[Link]([Link]());
}
}
Output :
Code :
class Employee {
public void decideSal(String s1) throws NSalException, PSalException,
NumberFormatException {
int sal = [Link](s1);
Java Programming Lab
anubhav
IT-3
03476803121
Page 33
if (sal <= 0) {
NSalException no = new NSalException("Invalid Salary");
throw (no);
} else {
PSalException po = new PSalException("Valid Salary");
throw (po);
}
}
}
class Execute13 {
public static void main(String args[]) {
try {
Employee e = new Employee();
[Link](args[0]);
} catch (NumberFormatException nfe) {
[Link]("Please enter integer values.");
} catch (NSalException no) {
[Link]("Negative Salary.");
} catch (PSalException po) {
[Link]("Valid Positive Salary.");
} finally {
[Link]("Finally Block executing");
}
}
}
Output :
import [Link].*;
SortNames() {
for (i = 0; i < n; i++) {
name[i] = null;
}
}
void inputNames() {
name[0] = "Yogesh";
name[1] = "Deepak";
name[2] = "Aman";
name[3] = "Srishti";
name[4] = "Baljinder";
}
void bubbleSort() {
int j;
String temp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (name[j].compareTo(name[j + 1]) > 0) {
temp = name[j];
name[j] = name[j + 1];
name[j + 1] = temp;
}
}
}
}
void displayNames() {
for (i = 0; i < n; i++) {
[Link](name[i]);
}
}
}
class Execute14 {
public static void main(String a[]) {
SortNames sn = new SortNames();
[Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 36
[Link]();
[Link]();
}
}
Output :
Aim : - Design a java program to create two threads: one for odd numbers and other for even
numbers.
Code : -
void set() {
[Link] = 0;
}
void even() {
num = num + 2;
[Link]("Even : " + num);
}
}
int num;
void set() {
[Link] = 1;
}
class Execute15 {
Code : -
class Q {
int n;
boolean valueSet = false;
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
class Execute16 {
Output : -
Code : -
class Callme {
String msg;
int time;
Callme target;
Thread t;
class Execute17 {
Output : -
Code : -
class Callme {
String msg;
int time;
Callme target;
Thread t;
Output :-
b) To accept the answer and print “CORRECT” and then stop if the answer is correct.
d) To display the correct answer, if the answer is wrong even after third attempt.
Code : -
import [Link].*;
import [Link].*;
import [Link].*;
Output : -
Code : -
import [Link].*;
import [Link].*;
import [Link].*;
String s1;
Int n1;
TextField tf1, tf2;
Button result;
Checkbox cb1, cb2;
CheckboxGroup cbg;
Code : -
import [Link].*;
import [Link].*;
import [Link].*;
Output : -
Code : -
import [Link].*;
import [Link].*;
import [Link].*;
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
Output : -
import [Link].*;
import [Link].*;
import [Link].*;
Output : -
Code :
import [Link].*;
import [Link].*;
import [Link].*;
Output :
Code :
import [Link].*;
class program25 {
Output :
Code :
import [Link];
import [Link];
class program26 {
Aim : Design a program in java to implement client-server TCP/IP socket application which
exchanges strings among them .
Java Programming Lab
anubhav
IT-3
03476803121
Page 68
Code :
[Link]
import [Link].*;
import [Link].*;
class server
{
public static void main(String args[])
{
int port=6666;
try
{
ServerSocket ss=new ServerSocket(port);
[Link]("WAITING FOR CLIENT ");
Socket socket=[Link]();
[Link]("GOT A CLIENT ");
InputStream sin=[Link]();
OutputStream sout=[Link]();
DataInputStream in=new DataInputStream(sin);
DataOutputStream out=new DataOutputStream(sout);
String line=null;
while(true)
{
line=[Link]();
[Link]("LINE RECEIVED FROM CLIENT "+line);
[Link]("SENDING LINE BACK....");
[Link](line);
[Link]();
[Link]("WAITING FOR ANOTHER LINE");
}
}
catch(IOException e)
{
[Link]("EXCEPTION OCCURRED………");
}
}
}
[Link]
class client {
Ouput :
Java Programming Lab
anubhav
IT-3
03476803121
Page 70
Aim : program to create database connectivity using jdbc, create statement object create
resultset object and display.
import [Link].*;
class DBConnectivity {
void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link]("jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}
void displayFirst() {
try {
ResultSet RS;
Statement SMT;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
[Link]();
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}
void displayLast() {
try {
ResultSet RS;
Statement SMT;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
[Link]();
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}
void displayNextAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}
void displayPreviousAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 74
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}
}
Output :
Code :
import [Link].*;
import [Link].*;
import [Link].*;
class DBConnectivity {
void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link](
"jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}
void closeConnection() {
try {
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}
void displayNextAll() {
try {
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}
void insertRecord() {
int Hnum, Hrooms;
String Hloc, Howner;
Scanner sc = new Scanner([Link]);
try {
Statement SMT;
String QU;
SMT = [Link]();
[Link]("ENTER HOUSE NUMBER ");
Hnum = [Link]();
[Link]("ENTER HOUSE LOCALITY ");
Hloc = [Link]();
[Link]("ENTER Mr. / Mrs. ");
String salu = [Link]();
[Link]("ENTER HOUSE OWNER NAME ");
Java Programming Lab
anubhav
IT-3
03476803121
Page 78
Howner = [Link]();
Howner = salu + " " + Howner;
[Link]("ENTER HOUSE ROOMS ");
Hrooms = [Link]();
QU =
"INSERT INTO HOUSE VALUES(" +
Hnum +
",'" +
Hloc +
"'," +
Hrooms +
",'" +
Howner +
"');";
[Link](QU);
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Insert .....");
}
}
void insertRecordUsingPrepared() {
int Hnum, Hrooms;
String Hloc, Howner;
Scanner sc = new Scanner([Link]);
try {
PreparedStatement PSMT;
String QU;
PSMT = [Link]("INSERT INTO HOUSE VALUES(?,?,?,?)");
[Link]("ENTER HOUSE NUMBER ");
Hnum = [Link]();
[Link]("ENTER HOUSE LOCALITY ");
Hloc = [Link]();
[Link]("ENTER Mr. / Mrs. ");
String salu = [Link]();
[Link]("ENTER HOUSE OWNER NAME ");
Howner = [Link]();
Howner = salu + " " + Howner;
[Link]("ENTER HOUSE ROOMS ");
Hrooms = [Link]();
[Link](1, Hnum);
[Link](2, Hloc);
[Link](3, Hrooms);
Java Programming Lab
anubhav
IT-3
03476803121
Page 79
[Link](4, Howner);
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Insert .....");
[Link](Ee);
}
}
}
Output :
Code :
import [Link].*;
import [Link].*;
import [Link].*;
class DBConnectivity {
void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link](
"jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}
void closeConnection() {
try {
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}
void displayNextAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}
void updateRecord() {
int Hnum, Hrooms;
String Hloc, Howner;
Scanner sc = new Scanner([Link]);
try {
Statement SMT;
String QU;
SMT = [Link]();
[Link]("ENTER HOUSE NUMBER TO UPDATE ");
Hnum = [Link]();
[Link]("ENTER HOUSE LOCALITY ");
Hloc = [Link]();
[Link]("ENTER Mr. / Mrs. ");
String salu = [Link]();
[Link]("ENTER HOUSE OWNER NAME ");
Howner = [Link]();
Howner = salu + " " + Howner;
[Link]("ENTER HOUSE ROOMS ");
Hrooms = [Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 83
QU =
"UPDATE HOUSE SET H_LOCALITY = '" +
Hloc +
"', H_ROOMS = " +
Hrooms +
", H_OWNER = '" +
Howner +
"' WHERE H_NO = " +
Hnum +
";";
[Link](QU);
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Update .....");
}
}
}
Code :
import [Link].*;
import [Link].*;
import [Link].*;
class DBConnectivity {
void makeConnection() {
try {
[Link]("[Link]");
CO =
[Link](
"jdbc:mysql://localhost:3306/shiksha",
"root",
"ADMIN"
);
} catch (Exception Ee) {
[Link](Ee);
}
}
void closeConnection() {
try {
[Link]();
} catch (Exception Ee) {
[Link](Ee);
}
}
void displayNextAll() {
try {
Statement SMT;
ResultSet RS;
SMT = [Link]();
String QU = "SELECT * FROM HOUSE";
RS = [Link](QU);
[Link]();
Java Programming Lab
anubhav
IT-3
03476803121
Page 86
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
while ([Link]()) {
[Link](
[Link]("H_NO") +
[Link]("H_LOCALITY") +
[Link]("H_ROOMS") +
[Link]("H_OWNER")
);
}
[Link]();
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught for Next.....");
}
}
void deleteRecord() {
int Hnum;
Scanner sc = new Scanner([Link]);
try {
Statement SMT;
String QU;
SMT = [Link]();
[Link]("ENTER HOUSE NUMBER, TO DELETE ");
Hnum = [Link]();
QU = "DELETE FROM HOUSE WHERE H_NO = " + Hnum + ";";
[Link](QU);
[Link]();
} catch (Exception Ee) {
[Link]("Exception Caught During Delete .....");
}
}
}
Output :