OOT
Lab Assignment Solution (ECS-553)
CS
5th Semester
|
S.No.
|
Program
|
|
1
|
Write a
program in java which prints your name using command line arguments.
class cmdarguments
{
public static void main(String
args[])
{
String str;
str=args[0];
System.out.println("Your name is "+str);
}
}
|
|
2
|
Write
a program in java which enters three number using command line arguments and
print sum and average of the number
class sumnavg
{
public static void main(String
args[])
{
int a,b,c,sum,avg;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=
Integer.parseInt(args[2]);
sum=a+b+c;
avg=sum/3;
System.out.println("sum is "+sum);
System.out.println("Average is "+avg);
}
}
|
|
3
|
Write
a program to swap the value of 2 variables without using 3rd variable
Program:-
class
swap
{
public static void main(String
args[])
{
int a=12,b=15;
System.out.println("numbers
before swapping are"+a +b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("numbers
before swapping are"+a +b);
}
Output: - Number of swap. 1215
1512
|
|
4
|
Write
a program to calculate the sum of digits of a given integer no.
Program :-
class
Digit
{
public static void main(String
ar[])
{
int
num=Integer.parseInt(System.console().readLine("enter no. for
sum"));
int n,i,sum=0;
while(num>0)
{
n=num%10;
sum=sum+n;
num=num/10;
}
System.out.println("sum
of digit of given number is"+sum);
}
}
Output:- Sum of digit number 5
Sum 5
|
|
5
|
Write
a program to compute the sum of the first and last digit of a given number.
class A3
{
public
static void main(String ar[])
{
int
num=Integer.parseInt(System.console().readLine("enter a number"));
int
a;
a=num%10;
System.out.println("last
digit is"+a);
while(num>=10)
{
num=num/10;
}
a=a+num;
System.out.println("first
digit is"+num);
System.out.println("sum
of first and last digit is"+a);
}
}
OUTPUT: - FIST NO. 2
LAST
NO.2
SUM
OF. 4
|
|
6
|
Write
a program to calculate and print first n Fibonacci numbers.
class
fibo
{
public static void main(String
ar[])
{
int a=0,b=1,c;
int
n=Integer.parseInt(System.console().readLine("enter no to print the Fibonacci
series"));
System.out.println(a);
System.out.println(b);
for(int
i=1;i<n;i++)
{
c=a+b;
System.out.println(c);
a=b;
b=c;
}
}
}
OUTPUT :- Fibonacci. 5
1
1
2
3
5
|
|
7
|
Write
a program to reverse the given number.
PROGRAM:-
class
Rev
{
public static void main(String
ar[])
{
int
num=Integer.parseInt(System.console().readLine("enter no. for
reverse"));
int n,i,rev=0;
while(num>0)
{
n=num%10;
rev=rev*10+n;
num=num/10;
}
System.out.println("sum
of digit of given number is"+rev);
}
}
OUTPUT:- Reverse 55
SUM OF DIGIT
IS 55
|
|
8
|
Write
a program in java which enter the number using DataInputStream and check
whether the entered number is even or odd.
import java.io.*;
class oddeven
{
public static void main(String
args[])throws IOException
{
int a ;
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter the
number");
a=Integer.parseInt(d.readLine());
if(a%2==0)
System.out.println("Number is even"+a);
else
System.out.println("Number is odd"+a);
}
}
|
|
9
|
Write
a program that calculate and print the roots of a quadratic equation
ax^2+bx+c = 0 and appropriate message should be printed if root are complex
class
Root
{
public static void main(String
ar[])
{
int
a=Integer.parseInt(System.console().readLine("enter value of a for the
equation a*x*x+b*x+c"));
int
b=Integer.parseInt(System.console().readLine("enter value of b for the
equation a*x*x+b*x+c"));
int
c=Integer.parseInt(System.console().readLine("enter value of c for the
equation a*x*x+b*x+c"));
double r1,r2,d;
d=b*b-4*a*c;
if(d<0)
System.out.println("roots
are imaginary");
else if(d==0)
{
r1=(-b)/(2*a);
System.out.println("roots
are equal and value is"+r1);
}
else if(d>0)
{
r1=((-b)+Math.sqrt(d)/(2*a));
r2=((-b)-Math.sqrt(d)/(2*a));
System.out.println("root
are: r1 is"+r1+"r2 is"+r2);
}
}
}
OUTPUT:- ENTER NO. A,B,C 55,44,5
R1,R2=
-43.737,-44.262
|
|
10
|
Write
an application that reads a string and determines whether it is a palindrome.
class
Pallindrome
{
public static void main(String
ar[])
{
String
s1=System.console().readLine("enter string");
boolean flag=false;
int len=s1.length();
int i=0,j=len-1;
while(j>=i)
{
if(s1.charAt(i)==s1.charAt(j))
{
flag=true;
}
else
{
flag=false;
break;
}
i++;j--;
}
if(flag)
System.out.println("String
is pallindrome");
else
System.out.println("String
is not pallindrome");
}
}
|
|
11
|
Write
a program to enter a sentence form keyboard and also find all the words in
that sentence with starting character as vowel.
PROGRAM: -
class
Vowel
{
public static void main(String
ar[])
{
String
s=System.console().readLine("enter a sentence");
int count=0;
String
vowel="aeiouAEIOU";
String
s1[]=s.split(" ");
for(int
i=0;i<s1.length;i++)
{
char
fc=s1[i].charAt(0);
if(vowel.indexOf(fc)>=0)
{
System.out.println(s1[i]);
count=count+1;
}
}
System.out.println("total
no of vowels"+count);
}
}
|
|
12
|
write
a program to print the string 'ALLAHABAD' in following format
A
A L
A L L
A L L A
A L L A H
A L L A H A
A L L A H A B
A L L A H A B A
A L L A H A B A D
class
Allahabad
{
public static void main(String
ar[])
{
String
s="ALLAHABAD";
int l=s.length();
for(int
i=0;i<l;i++)
{
for(int
j=0;j<=i;j++)
{
System.out.print(s.charAt(j));
}
System.out.println();
}
}
}
|
|
13
|
Write
a Program in java which creates the array of size 5; find the sum and average
of the five numbers.
import java.io.*;
class arraysumavg
{
public static void main(String
args[])throws IOException
{
int a[] =new int[5],sum=0 ;
DataInputStream d=new
DataInputStream(System.in);
System.out.println("Enter the number
in array");
for (int i=0;i<5;i++)
{
System.out.println("Enter
the"+(i+1)+"number");
a[i]=Integer.parseInt(d.readLine());
}
for (int i=0;i<5;i++)
{
sum=sum+a[i];
}
System.out.println("Sum of the number
is"+sum);
System.out.println("Average of the number is"+sum/5);
}
}
|
|
14
|
[Package
and Array] Create a package named Mathematics and add following classes to
it:
i)
A class Matrix with methods to add and multiplt matrices.
ii)A
class Complex with methods to add, multiply and subtract complex numbers.
Write
a Java program importing the Mathematics package and use the classes defined
in it.
package
Mathematics;
public
class Matrix
{
int i,j,k;
int [][] o=new int [3][3];
int [][] m={
{5,6,9},
{7,9,6},
{8,9,2},
};
int [][] n={
{1,2,3},
{4,5,6},
{7,8,9},
};
public void add()
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
o[i][j]=m[i][j]+n[i][j];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("matrix
after addition is\n"+o[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
public void multi()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
o[i][j]=0;
for(k=0;k<3;k++)
{
o[i][j]=o[i][j]+(m[i][k]*n[k][i]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("matrix
after addition is\n"+o[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
|
|
15
|
Write a program in java which input
the String “Ajay Kumar Maurya” and checks whether the string ends with Maurya
or not.
public class endstringtest{
public static void main(String args[]){
String Str = new String("Ajay Kumar Maurya");
boolean retVal;
retVal = Str.endsWith( "Maurya" );
System.out.println("Returned Value = " + retVal );
}
}
|
|
16
|
Write a program in java which input
the string “Ajay Kumar Maurya” and
I.
Find the length of the string.
II.
Check whether the string contains the substring
“jay”.
III.
Concatenate Mr at the starting of the name.(Mr.
Ajay Kumar Maurya).
import java.io.*;
public class stringtest
{
public static void main(String args[])
{
String Str = new String("Ajay
Kumar Maurya");
System.out.print("Length of the
string is \t" );
System.out.println(Str.length());
System.out.print("Return Value
:" );
System.out.println(Str.matches("(.*)jay(.*)"));
Str="Mr.".concat(Str);
System.out.println("New string is
"+Str);
}
}
|
|
17
|
[Class
and Object] Define a class Worker with the following specifications:
Data Member : workerNumber, nameOfWorker,
wegeRatePerHour, totalWage,
hoursWorkedByAWorker
Methods :>To
assign initial value for worker number, name of worker, hours worked by
worker, wage rate per hour. [ public Worker(......) ]
>
To calculate total wage to be paid to the worker. [ public void
wageToBePaid()
>
To display all the information - [ public void displayWorkerDetai( ) ]
Write a program in Java to test
the program.
PROGRAM: -
class Worker
{
int
workerno,hours,wagerate;
String workername;
float totalwage;
Worker()
{
workerno=54;
hours=6;
workername="ramesh";
wagerate=30;
}
void wagetobepaid()
{
totalwage=wagerate*hours;
}
void display()
{
System.out.println("workerno.
is :"+workerno);
System.out.println("hours
is :"+hours);
System.out.println("wagerate.
is :"+wagerate);
System.out.println("workername
is :"+workername);
wagetobepaid();
System.out.println("wage
to be paid to a worker. is :"+totalwage);
}
public static void
main(String ar[])
{
Worker
t=new Worker();
t.display();
}
|
|
18
|
Write
a program in java which handles the runtime exception using try catch block.
class trycatch
{
public static void main(String
args[])
{
int a,b,c;
try
{
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=a/b;
System.out.println("result is "+c);
}
catch(Exception x1)
{
System.out.println("pl check the data");
System.out.println("error type is "+x1.getMessage());
}
finally
{
System.out.println("working
with java");
}
}
}
|
|
19
|
[Class
and Object] Define a class named “Test” with an instance variable num. Define
a Constructor and a method named
getReverse(). Create an object of the class
pass an integer to the constructor to initialize num. Call getReverse() to
get the reverse and print the reverse no.
class
Test
{
int num;
Test()
{
num=Integer.parseInt(System.console().readLine("enter
no. to get reverse of it"));
}
void getreverse()
{
int rev=0;
while(num>0)
{
int n=num%10;
rev=rev*10+n;
num=num/10;
}
System.out.println("reverse
is.."+rev);
}
public static void main(String
ar[])
{
Test t=new Test();
t.getreverse();
}
}
|
|
20
|
[Class and
Object] Calculate the area of circle
and cylinder by creating methods name areaOfCircle and areaOfCylinder
in a class named Area using a constant attribute PI=3.14.
|
|
21
|
Write
a program in java which creates a class name works having three methods
input, sum and show. Create the object of the class and invoke the methods of
the class on the created object
class works
{
int a,b,c;
void input()
{
a=30;
b=33;
}
void sum()
{
c=a+b;
}
void show()
{
System.out.println("fist
no "+a);
System.out.println("second no "+b);
System.out.println("sum
no "+c);
}
}
class democlass
{
public static void main(String
args[])
{
works x=new works();
x.input();
x.sum();
x.show();
}
}
|
|
22
|
Create
a class SimpleCalculator that has functionality of addition, substraction
division, multiplication, square and squareroot and then create another class
ScientificCalculator that has functionality of impleCalculator and some other
functionality like sin, cos, tan.
//import
java.lang.Math;
class
SimpleCalc
{
void add(float a,float b)
{
float c=a+b;
System.out.println("addition
of 2 no. is -:"+c);
}
void sub(float a,float b)
{
float c=a-b;
System.out.println("substraction
of 2 no. is: -"+c);
}
void mul(float a,float b)
{
float c=a*b;
System.out.println("multiplication
of 2 no. is -:"+c);
}
void div(float a,float b)
{
float c=a/b;
System.out.println("division
of 2 no. is -:"+c);
}
void square(float a)
{
float c=a*a;
System.out.println("square
of 2 no. is -:"+c);
}
void squareroot(double d)
{
double c=Math.sqrt(d);
System.out.println("sqrt
of no. is -:"+d);
}
}
class
ScientificCalc extends SimpleCalc
{
void sinum(double d)
{
double
c=Math.sin(d);
System.out.println("sin
of no. is -:"+d);
}
void cosine(double d)
{
double
c=Math.cos(d);
System.out.println("cos
of no. is -:"+d);
}
void tangent(double d)
{
double
c=Math.tan(d);
System.out.println("tan
of no. is -:"+d);
}
}
class
calc
{
public static void main(String
ar[])
{
float
a=Float.parseFloat(System.console().readLine("enter value of a"));
float
b=Float.parseFloat(System.console().readLine("enter value of b"));
double
d=Double.parseDouble(System.console().readLine("enter value of
d"));
int
ch=Integer.parseInt(System.console().readLine("enter a character for choice
1 for:simplecalc, 2 for:scientific calc"));
switch(ch)
{
case 1:
SimpleCalc
s=new SimpleCalc();
System.out.println("1-:
for addition");
System.out.println("2-:
for substraction");
System.out.println("3-:
for multiplication");
System.out.println("4-:
for division");
System.out.println("5-:
for square");
System.out.println("6-:
for sqroot");
int
ch1=Integer.parseInt(System.console().readLine("enter character for
choice in simplecalc "));
switch(ch1)
{
case 1:
s.add(a,b);
break;
case
2:
s.sub(a,b);
break;
case
3:
s.mul(a,b);
break;
case
4:
s.div(a,b);
break;
case
5:
s.square(a);
break;
case
6:
s.squareroot(d);
break;
default:
break;
}
break;
case 2:
ScientificCalc
sc=new ScientificCalc();
System.out.println("1-:
for addition");
System.out.println("2-:
for substraction");
System.out.println("3-:
for multiplication");
System.out.println("4-:
for division");
System.out.println("5-:
for square");
System.out.println("6-:
for sqroot");
System.out.println("7-:
for sin of no");
System.out.println("8-:
for cos of no");
System.out.println("9-:
for tan of no");
int
ch2=Integer.parseInt(System.console().readLine("enter character for
choice in scientificcalc "));;
switch(ch2)
{
case
1:
sc.add(a,b);
break;
case
2:
sc.sub(a,b);
break;
case
3:
sc.mul(a,b);
break;
case
4:
sc.div(a,b);
break;
case
5:
sc.square(a);
break;
case
6:
sc.squareroot(d);
break;
case
7:
sc.sinum(d);
break;
case
8:
sc.cosine(d);
break;
case
9:
sc.tangent(d);
break;
default:
break;
}
break;
default:
break;
}
}
}
|
|
23
|
[Overloading]Create
a java program that has three version of add method which can add two, three, and four integers
PROGRAM: -
class Voverload
{
//float size,r,h,l,area,w;
float area;
void volume(float size)
{
area=(size*size*size);
System.out.println("area
of cube is :::::"+area);
}
void volume(float r,float h)
{
area=((3.14f*r*r*h));
System.out.println("area
of cylinder is :::::"+area);
}
void volume(float l,float h1,float
w)
{
area=(l*w*h1);
System.out.println("area
of rectangle is :::::"+area);
}
public static void main(String
ar[])
{
float size,r,h,l,w,h1;
size=Float.parseFloat(System.console().readLine("enter
size of cube"));
r=Float.parseFloat(System.console().readLine("enter
radius of cylinder"));
h=Float.parseFloat(System.console().readLine("enter
height of cylinder "));
l=Float.parseFloat(System.console().readLine("enter
length of rectangle"));
w=Float.parseFloat(System.console().readLine("enter
width of rectangle"));
h1=Float.parseFloat(System.console().readLine("enter
height of rectangle"));
//size=Float.parseFloat(System.console().readLine("enter
size of cube"));
Voverload o=new
Voverload();
o.volume(size);
o.volume(r,h);
o.volume(l,h1,w);
}
}
|
|
24
|
Write
a program in java having class good1, class good2 extends good1 and good3
extends good2.Check that all the methods are accessible in inherited
class.good1 having method data,good2 having method sum and good3 having
method show.
class good1
{
int a,b,c;
void data()
{
a=30;
b=33;
}
}
class good2 extends
good1
{
void sum()
{
c=a+b;
}
}
class good3 extends
good2
{
void show()
{
System.out.println("sum
is "+c);
}
good3()
{
System.out.println("testing
class in java");
}
}
class p5
{
public static void main(String
args[])
{
good3 g=new good3();
g.data();
g.sum();
g.show();
}
}
|
|
25
|
[Overloading]Write
a Java prgram that uses an overloaded method volume() that returns volume of
different structures.
The first version takes one float side
of a cube and returns its value. (side*side*side)
The second version takes float radius
and float height and returns the volume of a cylinder. (PI*r^2*h)
The third version takes float length,
float width and float height of a rectangular box and returns a volume.
(l*w*h)
class
Overload
{
int a,b,c,d,e;
void add(int a,int b)
{
e=a+b;
System.out.println("addition
of 2 no is "+e);
}
void add(int a,int b,int
c)
{
e=a+b+c;
System.out.println("addition
of 2 no is "+e);
}
void add(int a,int
b,int c,int d)
{
e=a+b+c+d;
System.out.println("addition
of 2 no is "+e);
}
public static void
main(String ar[])
{
//int
a,b,c,d,e;
a=Integer.parseInt(System.console().readLine("enter
value of a"));
b=Integer.parseInt(System.console().readLine("enter
value of b"));
c=Integer.parseInt(System.console().readLine("enter
value of c"));
d=Integer.parseInt(System.console().readLine("enter
value of d"));
Overload
o=new Overload();
o.add(a,b);
o.add(a,b,c);
o.add(a,b,c,d);
}
}
|
|
26
|
Create
an abstract base class titled ‘Shapes’. It should contain a method Area ()
which returns area of a particular shape. Derive two classes from Shapes
titled Rectangle(width*height) and
triangle(1/2*base*height). Implement the method Area () in both classes to
print the area.
PROGRAM: -
abstract
class Shape
{
String name;
double areaofshape;
abstract void area();
void diaplay()
{
System.out.println("area
of "+name+" is
"+areaofshape);
}
}
class
Triangle extends Shape
{
float base,height;
Triangle(float b,float h)
{
base=b;
height=h;
name="TRIANGLE";
}
void area()
{
areaofshape=((1.0/2.0)*base*height);
}
}
class
Rectangle extends Shape
{
float length,width;
Rectangle(float l,float w)
{
length=l;
width=w;
name="RECTANGLE";
}
void area()
{
areaofshape=(length*width);
}
}
class
Main
{
public static void main(String
ar[])
{
Triangle t=new
Triangle(1.5f,3.8f);
t.area();
t.diaplay();
Rectangle r=new
Rectangle(1.5f,3.8f);
r.area();
r.diaplay();
}
}
|
|
27
|
Write
a Program in Java to create a Swing Application Display your name using
label.
import
javax.swing.*;
public class
HelloWorldSwing {
private static
void createAndShowGUI()
{
JFrame frame = new
JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new
JLabel("Abhishek Kesharwani");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public static void
main(String[] args) {
{
createAndShowGUI();
};
}
}
|
|
28
|
Write
a Program in Java to create a user define Package.
package mypkg;
public class works
{
int a,b,c;
void input()
{
a=30;
b=33;
}
void sum()
{
c=a+b;
}
void show()
{
System.out.println("fist
no "+a);
System.out.println("second
no "+b);
System.out.println("sum
no "+c);
}
}
import mypkg.*;
class packagedemo
{
public static void
main(String [] args)
{
works x=new
works();
x.input();
x.sum();
x.show();
}
}
|
|
29
|
Write a program in java which creates
two threads, Main thread and Child Thread and print the even no using Main
Thread and odd no using Child Thread.
// Create a second
thread by extending Thread
class NewThread extends
Thread {
NewThread() {
// Create a new, second thread
super("Demo Thread");
start(); // Start the thread
}
// This is the entry point for the second
thread.
public void run() {
try {
for(int i = 19; i > 0; i=i-2) {
System.out.println("Child Thread:
" + i);
// Let the thread sleep for a
while.
Thread.sleep(2000);
}
} catch (InterruptedException e) {
System.out.println("Child
interrupted.");
}
System.out.println("Exiting child
thread.");
}
}
public class demothread
{
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 20; i > 0; i=i-2) {
System.out.println("Main
Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread
interrupted.");
}
System.out.println("Main thread
exiting.");
}
}
|
|
30
|
[Multi-Threading]
Write a Java program to
i)
Create a thread which can print multiplication table of any integer.
ii)
Print the name, priority, and group of the thread.
iii)
Change the name of the current thread to "JAVA"
iv)
Display the details of current thread.
|
|
31
|
Write
a program in java which creates an Applet and display your name in an html
page using that Applet.
import java.applet.*;
import java.awt.*;
public class web5
extends Applet
{
public void paint(Graphics g)
{
g.drawString("Shashank Bhushan
Vimal",100,100);
}
}
|
|
32
|
[Applet]
Write a Java applet to
i) Display
the name and address of your college. Use setBackgroud(), setFont() and
setForeground() method to
set
color and font of text, and color of background.
ii) Display an image.
PROGRAM: -
import
java.applet.*;
import
java.awt.*;
import
java.util.Date;
/*
<applet code=AppletDemo2 width=200
height=200 >
</applet>
*/
public
class AppletDemo2 extends Applet
{
Label l;
Thread t = new Thread()
{
public void run(){
while(true){
Date d = new Date();
String time = String.format("%tr",d);
l.setText(time);
try{Thread.sleep(1000);}catch(Exception
e){}
}
}
};
public void init()
{
setBackground(Color.BLACK);
setForeground(Color.WHITE);
setFont(new
Font("Arial",Font.BOLD,20));
l = new
Label("HH:MM:SS");
add(l);
t.start();
}
}
|
|
33
|
[Applet]
Write a Java applet to draw a traffic signal light.
import
java.applet.*;
import
java.awt.*;
/*<applet
code=Trafficlight width=200 height=300>
</applet>*/
public
class Trafficlight extends Applet
{
public
void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(50,50,60,200);
g.drawLine(90,250,90,320);
g.drawLine(70,250,70,320);
g.drawLine(70,320,90,320);
g.setColor(Color.RED);
g.fillOval(60,60,40,40);
g.setColor(Color.GREEN);
g.fillOval(60,120,40,40);
g.setColor(Color.YELLOW);
g.fillOval(60,180,40,40);
}
}
|
|
34
|
Write
a program in java which creates an Applet and draw a rectangle inside the
applet with a background of Red color. Display that applet in an html page.
import
java.applet.*;
import java.awt.*;
public class App
extends Applet
{
public void paint(Graphics g)
{
g.drawString("Abhishek
Kesharwani",100,100);
g.drawRect(0,0,400,400);
}
}
|
|
35
|
[Applet]
Write a Java program to create a calculator with (add, sub, div, mul, sqrt)
functionality using applet and test the program using <applet> HTML
tags.
import
java.awt.*;
import
java.awt.event.*;
class
Calculator extends Frame
{
static Label l1,l2,l3;
static TextField t1,t2,t3;
static Button b1,b2,b3,b4,b5;
MenuBar mb;
Menu file, edit, help;
MenuItem file1, file2,file3;
MenuItem edit1, edit2;
MenuItem help1;
Calculator()
{
setSize(200,300);
setVisible(true);
setTitle("CALC");
setLayout(new
FlowLayout());
mb = new MenuBar();
/*file = new
Menu("File");
edit = new
Menu("Edit");
help = new
Menu("Help");
file1 = new
MenuItem("new");
file2 = new
MenuItem("save");
file3 = new
MenuItem("save as");
file1.addActionListener(
new
ActionListener(){
public
void actionPerformed(ActionEvent ae)
{
Calculator
c = new Calculator();
c.setSize(200,100);
c.setVisible(true);
}
}
);
edit1 = new MenuItem("copy");
edit2 = new
MenuItem("paste");
help1 = new
MenuItem("about us");
help.add(help1);
edit.add(edit1); edit.add(edit2);
file.add(file1); file.add(file2);
file.add(file3);
mb.add(file);
mb.add(edit); mb.add(help);
setMenuBar(mb); */
l1 = new
Label("Number One");
l2 = new
Label("Number Two");
l3 = new
Label("Answer");
MouseHand mh = new
MouseHand();
l1.addMouseListener(mh);
l2.addMouseListener(mh);
//l3.addMouseListener(mh);
t1 = new TextField(20);
t2 = new
TextField(20);
t3 = new
TextField(20);
b1 = new
Button("ADD");
b2 = new
Button("SUB");
b3 = new
Button("MUL");
b4 = new
Button("DIV");
b5 = new
Button("SQRT");
ActionHand ah = new
ActionHand();
b1.addActionListener(ah);
b2.addActionListener(ah);
b3.addActionListener(ah);
b4.addActionListener(ah);
b5.addActionListener(ah);
KeyHand
kh = new KeyHand();
t1.addKeyListener(kh);
addWindowListener(new
WindowHand());
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(b1); add(b2);
add(b3); add(b4);
add(b5);
}
}
class
ActionHand implements ActionListener
{
public void
actionPerformed(ActionEvent ae)
{
if(ae.getSource() ==
Calculator.b1)
{
int n1 = Integer.parseInt(Calculator.t1.getText());
int n2 =
Integer.parseInt(Calculator.t2.getText());
int sum =
n1 + n2;
Calculator.t3.setText(String.valueOf(sum));
}
if(ae.getSource() ==
Calculator.b2)
{
int n1 =
Integer.parseInt(Calculator.t1.getText());
int n2 =
Integer.parseInt(Calculator.t2.getText());
Calculator.t3.setText(n1-n2+"");
}
if(ae.getSource() ==
Calculator.b3)
{
int n1 =
Integer.parseInt(Calculator.t1.getText());
int n2 =
Integer.parseInt(Calculator.t2.getText());
int sum =
n1*n2;
Calculator.t3.setText(String.valueOf(sum));
}
if(ae.getSource() ==
Calculator.b4)
{
int n1 =
Integer.parseInt(Calculator.t1.getText());
int n2 =
Integer.parseInt(Calculator.t2.getText());
float sum
= n1/n2;
Calculator.t3.setText(String.valueOf(sum));
}
if(ae.getSource() ==
Calculator.b5)
{
int n1 =
Integer.parseInt(Calculator.t1.getText());
int n2 =
Integer.parseInt(Calculator.t2.getText());
double sum
= Math.sqrt(n1);
Calculator.t3.setText(String.valueOf(sum));
}
}
}
class
MouseHand extends MouseAdapter
{
public void
mouseEntered(MouseEvent me)
{
Label l =
(Label)me.getSource();
String text =
l.getText();
text =
text.toUpperCase();
l.setText(text);
}
}
class
KeyHand extends KeyAdapter
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() ==
ke.VK_ESCAPE)
{
System.exit(0);
}
}
}
class
WindowHand extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
class
AWTDemo
{
public static void main(String [] ar)
{
Frame f = new Calculator();
}
}
|
|
36
|
Write
a program in java to create a dsn(Data Source Name) named ucer and database
student in ms access contains table school having columns name and city. Save
the value of name and city in the school database using JDBC.
import java.io.*;
import java.sql.*;
import
java.sql.Connection.*;
import
java.sql.PreparedStatement.*;
class save
{
public static void main(String
args[])
{
String n,c;
Connection cn;
PreparedStatement pst;
int x;
try
{
InputStreamReader ir=new
InputStreamReader(System.in);
BufferedReader br=new
BufferedReader(ir);
System.out.println("enter
name & city");
n=br.readLine();
c=br.readLine();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:ucer","","");
pst=cn.prepareStatement("insert into
school(name,city) values(?,?)");
pst.setString(1,n);
pst.setString(2,c);
x=pst.executeUpdate();
if(x==1)
{
System.out.println("Record
has been saved");
}
}
catch(Exception xx)
{
System.out.println("please
check the data "+xx.getMessage());
}
}
}
|
|
37
|
Write a program in java to create a
dsn(Data Source Name) named ucer and
database student in ms access contains table school having columns name and
city. Display the value of name and city in the school database using JDBC.
import java.io.*;
import java.sql.*;
import
java.sql.Statement.*;
import java.sql.ResultSet.*;
class show
{
public static void main(String args[])
{
String n,c;
Connection cn;
Statement st;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:ucer","","");
st=cn.createStatement();
rs=st.executeQuery("select * from
school");
while(rs.next())
{
n=rs.getString(1);
c=rs.getString(2);
System.out.println("name
"+n+" city "+c);
}
}
catch(Exception xx)
{
System.out.println("check data"+xx.getMessage());
}
}
}
|
|
38
|
Write a program in java to create a
dsn(Data Source Name) named ucer and
database student in ms access contains table school having columns name and
city .Delete the particular value of name and city in the school database
using JDBC.(take the value from the database)
import java.io.*;
import java.sql.*;
class del
{
public static void main(String args[])
{
String n;
int x;
Connection k;
PreparedStatement pp;
try
{
n=args[0];
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
k=DriverManager.getConnection("jdbc:odbc:ucer","","");
pp=k.prepareStatement("delete from school where
name=?");
pp.setString(1,n);
x=pp.executeUpdate();
if(x==1)
{
System.out.println("record
has been deleted");
}
}
catch(Exception xx)
{
System.out.println("check
data"+xx.getMessage());
}
}
}
|
|
39
|
[JDBC]
Ceate a database named College using MS Access with table student having
following fields:
- RollNumber (PK),
Branch, Name
1. Initialize this table with different
values. Write a program in Java to print the details in student table.
2.
[JDBC] Write a java program that
will provide the following utility to previous program:
- Add new student in Student
table.
-Delete existing student from
Student table
3. [JDBC] Write a java program that will
perform the search operation for Student by his/her roll number from Student
table.
import
java.sql.*;
class
jdbcdemo
{
static
Connection con;
static
Statement st;
public
static void main(String ar[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String
url="jdbc:odbc:parul";
con=DriverManager.getConnection(url);
st=con.createStatement();
addstudent
("106","anu","cs");
String
q="select * from student";
ResultSet
rs=st.executeQuery(q);
System.out.println("+----+----------+----+");
System.out.printf("|%4s|%-10s|%4s|\n","roll","name","brnc");
System.out.println("+----+----------+----+");
while(rs.next())
{
System.out.printf("|%-4s|",rs.getString(1));
System.out.printf("%-10s|",rs.getString(2));
System.out.printf("%4s|",rs.getString(3));
System.out.println();
}
System.out.println("+----+----------+----+");
}
static
void addstudent(String rn,String name,String br) throws Exception
{
String
g="insert into student
values('"+rn+"','"+name+"','"+br+"')";
int
ur=st.executeUpdate(g);
}
static
void searchstudent(String rn,String name,String br) throws Exception
{
String
g="select * from student where name='parul'";
ResultSet
rs=st.executeQuery(g);
System.out.println("+----+----------+----+");
System.out.printf("|%4s|%-10s|%4s|\n","roll","name","brnc");
System.out.println("+----+----------+----+");
while(rs.next())
{
System.out.printf("|%-4s|",rs.getString(1));
System.out.printf("%-10s|",rs.getString(2));
System.out.printf("%4s|",rs.getString(3));
System.out.println();
}
System.out.println("+----+----------+----+");
}
}
|
|
40
|
Write
a program in java to implement Network programming using Socket and Server
socket .Client send the number to Server and server return its square to the
client.
Client Program
import java.net.*;
import java.io.*;
class Client
{
public static void main(String []args)
{
Socket c;
BufferedReader brc,brs;
PrintWriter out;
String msg;
try
{
c=new
Socket("127.0.0.1",2000);
System.out.println("Connection
Established");
out=new
PrintWriter(c.getOutputStream(),true);
brc=new BufferedReader(new
InputStreamReader(c.getInputStream()));
brs=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Connection
Stream fetched");
System.out.print("Enter Any
Number ");
msg=brs.readLine();
out.println(msg);
msg=brc.readLine();
System.out.println("Message Received
:"+msg);
c.close();
}catch(Exception e){}
}
}
Server Program
import java.net.*;
import java.io.*;
class Server
{
public static void main(String []args)
{
ServerSocket s;
PrintWriter out;
BufferedReader brc;
Socket c;
String msg;
int a,b;
try
{
s=new ServerSocket(2000);
System.out.println("SERVER is UP
and RUNNING");
for(int x=0;x<5;x++)
{
c=s.accept();
System.out.println("Connection
Received");
brc=new BufferedReader(new
InputStreamReader(c.getInputStream()));
out=new
PrintWriter(c.getOutputStream(),true);
System.out.println("Stream
Fetched for R/W");
msg=brc.readLine();
System.out.println("Client Info
Received");
a=Integer.parseInt(msg);
b=a*a;
msg=String.valueOf(b);
out.println(msg);
System.out.println("Square of
"+a +" has been sent to client");
}
s.close();
}catch(Exception e){}
}
}
|
No comments:
Post a Comment