OOPS Lab Assignment (ECS-354)
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 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);
}
}
|
4.
|
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);
}
}
|
5.
|
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 );
}
}
|
6.
|
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);
}
}
|
7.
|
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");
}
}
}
|
8.
|
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();
}
}
|
9.
|
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();
}
}
|
10.
|
Write
a Program in java to implement method overriding.
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("Hello World");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public
static void main(String[] args) {
{
createAndShowGUI();
};
}
}
|
11
|
Write a Program in Java
to implement an interface.
interface Animal {
public void
eat();
public void
travel();
}
Save the above program as Animal.java and compile
public class Mammal implements Animal{
public void
eat(){
System.out.println("Mammal eats");
}
public void
travel(){
System.out.println("Mammal
travels");
}
public static
void main(String args[]){
Mammal m =
new Mammal();
m.eat();
m.travel();
}
}
|
12
|
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();
};
}
}
|
13
|
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();
}
}
|
14
|
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.");
}
}
|
15
|
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);
}
}
|
16
|
Write a program in
java which creates an Applet and draw a rectangle inside the applet. 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);
}
}
|
17
|
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());
}
}
}
|
18
|
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());
}
}
}
|
19
|
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());
}
}
}
|
20
|
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