SlideShare a Scribd company logo
Q1. Write a program in java to explain the concept of “THREADING”. 
Solution: - 
Threads can be implemented in two ways – 
1. By Extending the THREAD class. 
2. By Implementing the RUNNABLE class. 
Implementing the “Runnable” class. 
CODE – 
class NewThread implements Runnable 
{ 
Thread t; 
NewThread() 
{ 
t = new Thread(this, "Demo Thread"); 
System.out.println("Child thread: " + t); 
t.start(); 
}p 
ublic void run() 
{ 
try 
{ 
for(int i = 5; i > 0; i--) 
{ 
System.out.println("Child Thread: " + i); 
Thread.sleep(500); 
} 
}c 
atch (InterruptedException e) 
{ 
System.out.println("Child interrupted."); 
}S 
ystem.out.println("Exiting child thread."); 
} 
} 
class ThreadDemo 
{ 
public static void main(String args[]) 
{ 
new NewThread(); // create 
a new thread 
try 
{ 
for(int i = 5; i > 0; i--) 
{ 
System.out.println("Main Thread: " + i); 
Thread.sleep(1000); 
} 
}c 
atch (InterruptedException e) 
{ 
System.out.println("Main thread interrupted."); 
}S 
ystem.out.println("Main thread exiting."); 
} 
}
OUTPUT – 
Extending the “THREAD” class. 
CODE – 
class Share extends Thread 
{ 
static String msg[]={"This", "is", "a", "synchronized", "variable"}; 
Share(String threadname) 
{ 
super(threadname); 
}p 
ublic void run() 
{ 
display(getName()); 
}p 
ublic void display(String threadN) 
{ 
synchronized(this){ 
for(int i=0;i<=4;i++) 
{ 
System.out.println(threadN+msg[i]); 
}t 
ry 
{ 
this.sleep(1000); 
}c 
atch(Exception e) 
{} 
} 
}}c 
lass Sync 
{ 
public static void main(String[] args) 
{ 
Share t1=new Share("Thread One: "); 
t1.start(); 
Share t2=new Share("Thread Two: "); 
t2.start(); 
} 
}
OUTPUT – 
Q2. Write a program in java to implement Socket Programming. 
Solution: - 
A Client – Server Chat Program 
CODE – 
CLIENT.JAVA 
import java.net.*; 
import java.io.*; 
public class Client implements Runnable 
{ 
Socket s; 
BufferedReader br; 
BufferedWriter bw; 
BufferedReader ppp; 
String input = null; 
public static void main(String[] args) 
{ 
new Client(); 
}p 
ublic void run() 
{ 
try 
{ 
s.setSoTimeout(1); 
}c 
atch(Exception e) 
{}w 
hile (true) 
{ 
try 
{ 
System.out.println("Server: "+br.readLine()); 
}c 
atch (Exception h) 
{} 
} 
}p 
ublic Client()
{ 
try 
{ 
s = new Socket("127.0.0.1",100); 
br = new BufferedReader(new InputStreamReader(s.getInputStream())); 
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 
ppp = new BufferedReader(new InputStreamReader(System.in)); 
Thread th; 
th = new Thread(this); 
th.start(); 
bw.write("Hello Server"); 
bw.newLine(); 
bw.flush(); 
while(true) 
{ 
input = ppp.readLine(); 
bw.write(input); 
bw.newLine(); 
bw.flush(); 
} 
}c 
atch(Exception e) 
{} 
} 
} 
SERVER.JAVA 
import java.net.*; 
import java.io.*; 
public class ServerApp implements Runnable 
{ 
ServerSocket s; 
Socket s1; 
BufferedReader br; 
BufferedWriter bw; 
BufferedReader ppp; 
String input = null; 
public void run() 
{ 
try 
{ 
s1.setSoTimeout(1); 
}c 
atch(Exception e) 
{}w 
hile (true) 
{ 
try 
{ 
System.out.println("Client: "+br.readLine()); 
}c 
atch (Exception h) 
{} 
} 
}p 
ublic static void main(String arg[]) 
{ 
new ServerApp(); 
}p 
ublic ServerApp()
{ 
try 
{ 
s = new ServerSocket(100); 
s1=s.accept(); 
br = new BufferedReader(new InputStreamReader(s1.getInputStream())); 
bw = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream())); 
ppp = new BufferedReader(new InputStreamReader(System.in)); 
Thread th; 
th = new Thread(this); 
th.start(); 
while(true) 
{ 
input = ppp.readLine(); 
bw.write(input); 
bw.newLine(); 
bw.flush(); 
} 
}c 
atch(Exception e) 
{} 
} 
} 
OUTPUT – 
Q3. Write a program in java for Sending E-Mails. 
Solution: - 
import java.util.Date; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.MimeMessage;
public class Gmail 
{ 
public static void main(String ss[]) 
{ 
String host = "smtp.gmail.com"; 
String username = "somgaj"; 
String password = "qwaszx,.12/3"; 
try 
{ 
Properties props = new Properties(); 
props.put("mail.smtps.auth", "true"); 
props.put("mail.from","somgaj@gmail.com"); 
Session session = Session.getInstance(props, null); 
Transport t = session.getTransport("smtps"); 
t.connect(host, username, password); 
MimeMessage msg = new MimeMessage(session); 
msg.setFrom(); 
msg.setRecipients(Message.RecipientType.TO, "somgaj@gmail.com"); 
msg.setSubject("Hello Soumya!!"); 
msg.setSentDate(new Date()); 
msg.setText("Hi! FROM JAVA....If you recieve this, it is a success...."); 
t.sendMessage(msg, msg.getAllRecipients()); 
}c 
atch(Exception ee) 
{ 
System.out.println(ee.toString()); 
} 
} 
} 
OUTPUT –
Q4. Write a program in java for Implementing Calculator in an Applet. 
Solution: - 
CODE – 
CAL.JAVA 
import java.awt.*; 
import java.awt.event.*; 
import java.applet.*; 
class Cal extends Applet implements ActionListener 
{ 
String msg=" "; 
int v1,v2,result; 
TextField t1; 
Button b[]=new Button[10]; 
Button add,sub,mul,div,clear,mod,EQ; 
char OP; 
public void init() 
{ 
Color k=new Color(120,89,90); 
setBackground(k); 
t1=new TextField(10); 
GridLayout gl=new GridLayout(4,5); 
setLayout(gl); 
for(int i=0;i<10;i++) 
{ 
b[i]=new Button(""+i); 
}a 
d=new Button("add"); 
sub=new Button("sub"); 
mul=new Button("mul"); 
div=new Button("div"); 
mod=new Button("mod"); 
clear=new Button("clear"); 
EQ=new Button("EQ"); 
t1.addActionListener(this); 
add(t1); 
for(int i=0;i<10;i++) 
{ 
add(b[i]); 
}a 
dd(ad); 
add(sub); 
add(mul); 
add(div); 
add(mod); 
add(clear); 
add(EQ); 
for(int i=0;i<10;i++) 
{ 
b[i].addActionListener(this); 
}a 
d.addActionListener(this); 
sub.addActionListener(this); 
mul.addActionListener(this); 
div.addActionListener(this); 
mod.addActionListener(this); 
clear.addActionListener(this); 
EQ.addActionListener(this); 
}
public void actionPerformed(ActionEvent ae) 
{ 
String str=ae.getActionCommand(); 
char ch=str.charAt(0); 
if ( Character.isDigit(ch)) 
t1.setText(t1.getText()+str); 
else 
if(str.equals("ad")) 
{ 
v1=Integer.parseInt(t1.getText()); 
OP='+'; 
t1.setText(""); 
}e 
lse if(str.equals("sub")) 
{ 
v1=Integer.parseInt(t1.getText()); 
OP='-'; 
t1.setText(""); 
}e 
lse if(str.equals("mul")) 
{ 
v1=Integer.parseInt(t1.getText()); 
OP='*'; 
t1.setText(""); 
}e 
lse if(str.equals("div")) 
{ 
v1=Integer.parseInt(t1.getText()); 
OP='/'; 
t1.setText(""); 
}e 
lse if(str.equals("mod")) 
{ 
v1=Integer.parseInt(t1.getText()); 
OP='%'; 
t1.setText(""); 
}i 
f(str.equals("EQ")) 
{ 
v2=Integer.parseInt(t1.getText()); 
if(OP=='+') 
result=v1+v2; 
else if(OP=='-') 
result=v1-v2; 
else if(OP=='*') 
result=v1*v2; 
else if(OP=='/') 
result=v1/v2; 
else if(OP=='%') 
result=v1%v2; 
t1.setText(""+result); 
}i 
f(str.equals("clear")) 
{ 
t1.setText(""); 
} 
} 
}
CALCULATOR.HTML 
<html> 
<applet code=Cal.class height=500 width=400> 
</applet> 
</HTML> 
OUTPUT –
Q6. Write a program in java for Implementing RMI. 
Solution: - 
An applet using RMI implementing a calculator. 
CODE – 
CLIENT.JAVA 
import java.rmi.*; 
import java.rmi.registry.*; 
import java.awt.*; 
import java.awt.event.*; 
class mathClient extends Frame implements ActionListener 
{ 
Button B1=new Button("Sum"); 
Button B2=new Button("Subtract"); 
Button B3=new Button("Multiply"); 
Button B4=new Button("Divide"); 
Label l1=new Label("Number 1"); 
Label l2=new Label("Number 2"); 
Label l3=new Label("Result"); 
TextField t1=new TextField(20); 
TextField t2=new TextField(20); 
TextField t3=new TextField(20); 
public mathClient() 
{ 
super("Calculator"); 
setLayout(null); 
l1.setBounds(20,50,55,25); 
add(l1); 
l2.setBounds(20,100,55,25); 
add(l2); 
l3.setBounds(20,150,55,25); 
add(l3); 
t1.setBounds(150,50,100,25); 
add(t1); 
t2.setBounds(150,100,100,25); 
add(t2); 
t3.setBounds(150,150,100,25); 
add(t3); 
B1.setBounds(20,200,80,25); 
add(B1); 
B2.setBounds(100,200,80,25); 
add(B2); 
B3.setBounds(180,200,80,25); 
add(B3); 
B4.setBounds(260,200,80,25); 
add(B4); 
B1.addActionListener(this); 
B2.addActionListener(this); 
B3.addActionListener(this); 
B4.addActionListener(this); 
addWindowListener( 
new WindowAdapter() 
{ 
public void windowClosing(WindowEvent e) 
{ 
System.exit(0); 
} 
}
); 
} 
public void actionPerformed(ActionEvent AE) 
{ 
if(AE.getSource()==B1) 
{s 
um(); 
}e 
lse if(AE.getSource()==B2) 
{ 
subt(); 
}e 
lse if(AE.getSource()==B3) 
{ 
mult(); 
}e 
lse if(AE.getSource()==B4) 
{ 
div(); 
} 
} 
public void sum() 
{ 
int i=Integer.parseInt(t1.getText()); 
int j=Integer.parseInt(t2.getText()); 
int val; 
try 
{ 
String ServerURL="MathServ"; 
mathInterface MI=(mathInterface)Naming.lookup(ServerURL); 
val=MI.add(i,j); 
t3.setText(""+val); 
} 
catch(Exception ex) 
{ 
System.out.println("Exception:"+ex); 
} 
} 
public void subt() 
{ 
int i=Integer.parseInt(t1.getText()); 
int j=Integer.parseInt(t2.getText()); 
int val; 
try 
{ 
String ServerURL="MathServ"; 
mathInterface MI=(mathInterface)Naming.lookup(ServerURL); 
val=MI.subt(i,j); 
t3.setText(""+val); 
} 
catch(Exception ex) 
{ 
System.out.println("Exception:"+ex); 
} 
} 
public void mult() 
{ 
int i=Integer.parseInt(t1.getText()); 
int j=Integer.parseInt(t2.getText()); 
int val; 
try 
{ 
String ServerURL="MathServ"; 
mathInterface MI=(mathInterface)Naming.lookup(ServerURL); 
val=MI.mult(i,j);
t3.setText(""+val); 
} 
catch(Exception ex) 
{ 
System.out.println("Exception:"+ex); 
} 
} 
public void div() 
{ 
int i=Integer.parseInt(t1.getText()); 
int j=Integer.parseInt(t2.getText()); 
int val; 
try 
{ 
String ServerURL="MathServ"; 
mathInterface MI=(mathInterface)Naming.lookup(ServerURL); 
val=MI.div(i,j); 
t3.setText(""+val); 
} 
catch(Exception ex) 
{ 
System.out.println("Exception:"+ex); 
} 
} 
public static void main(String args[]) 
{ 
mathClient MC=new mathClient(); 
MC.setVisible(true); 
MC.setSize(600,500); 
}; 
} 
SERVER.JAVA 
import java.rmi.*; 
import java.rmi.Naming.*; 
import java.rmi.server.*; 
import java.rmi.registry.*; 
import java.net.*; 
import java.util.*; 
interface mathInterface extends Remote 
{ 
public int add(int a,int b) throws RemoteException; 
public int subt(int a,int b) throws RemoteException; 
public int mult(int a,int b) throws RemoteException; 
public int div(int a,int b) throws RemoteException; 
}c 
lass mathServer extends UnicastRemoteObject implements mathInterface 
{ 
public mathServer() throws RemoteException 
{ 
System.out.println("Initializing Server"); 
}p 
ublic int add(int a,int b) 
{ 
return(a+b); 
}p 
ublic int subt(int a,int b) 
{ 
return(a-b); 
}p 
ublic int mult(int a,int b) 
{ 
return(a*b); 
}
public int div(int a,int b) 
{ 
return(a/b); 
}p 
ublic static void main(String args[]) 
{ 
try 
{ 
mathServer ms=new mathServer(); 
java.rmi.Naming.rebind("MathServ",ms); 
System.out.println("Server Ready"); 
}c 
atch(RemoteException RE) 
{ 
System.out.println("Remote Server Error:"+ RE.getMessage()); 
System.exit(0); 
}c 
atch(MalformedURLException ME) 
{ 
System.out.println("Invalid URL!!"); 
} 
} 
} 
OUTPUT – 
Q7. Write a program in java for reading a file from a designated URL. 
Solution: - 
I have used my drop-box account for accessing a file stored in my DROPBOX server. 
CODE – 
import java.io.BufferedInputStream; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader;
import java.net.URL; 
class MainClass { 
public static void main(String[] args) throws Exception { 
URL u = new URL("https://dl.dropbox.com/u/94684259/Rajat.txt"); 
InputStream in = u.openStream(); 
in = new BufferedInputStream(in); 
Reader r = new InputStreamReader(in); 
int c; 
while ((c = r.read()) != -1) { 
System.out.print((char) c); 
} 
} 
} 
OUTPUT – 
Q8. Write a program in java implementing File input output functions. 
Solution: - 
CODE – 
import java.io.*; 
public class CopyFile 
{ 
private static void copyfile(String srFile, String dtFile) 
{ 
try 
{ 
File f1 = new File(srFile); 
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1); 
OutputStream out = new FileOutputStream(f2); 
byte[] buf = new byte[1024]; 
int len; 
while ((len = in.read(buf)) > 0) 
{ 
out.write(buf, 0, len); 
}i 
n.close(); 
out.close(); 
System.out.println("File copied."); 
}c 
atch(FileNotFoundException ex) 
{ 
System.out.println(ex.getMessage() + " in the specified directory."); 
System.exit(0); 
}c 
atch(IOException e) 
{ 
System.out.println(e.getMessage()); 
} 
}p 
ublic static void main(String[] args) 
{ 
switch(args.length) 
{ 
case 0: System.out.println("File has not mentioned."); 
System.exit(0); 
case 1: System.out.println("Destination file has not mentioned."); 
System.exit(0); 
case 2: copyfile(args[0],args[1]); 
System.exit(0); 
default : System.out.println("Multiple files are not allow."); 
System.exit(0); 
} 
} 
} 
OUTPUT – 
Q9. Write a program in java implementing Java Beans. 
Solution: - 
Implementing a counter with JSP on a server for counting number of website views. 
CODE – 
COUNTERBEAN.JAVA 
package form; 
public class CounterBean implements java.io.Serializable 
{ 
int coun = 0;
public CounterBean() 
{}p 
ublic int getCoun() 
{ 
coun++; 
return this.coun; 
}p 
ublic void setCoun(int coun) 
{ 
this.coun = coun; 
} 
} 
COUNTER.JSP 
<%@ page language="java" %> 
<jsp:useBean id="counter" scope="session" class="form.CounterBean" /> 
<HTML> 
<HEAD><TITLE>Use Bean Counter Example</TITLE> 
</HEAD> 
<BODY> 
<table><tr><td><b>The current count for the counter bean is: </b> 
<%=counter.getCoun() %></td></tr> 
</table 
</BODY> 
</HTML> 
OUTPUT –
Q10. Write a program in java implementing Swings. 
Solution: - 
CODE – 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.html.*; 
public class bar 
{ 
final static int interval = 1000; 
int i; 
JLabel label; 
JProgressBar pb; 
Timer timer; 
JButton button; 
public bar() 
{ 
JFrame frame = new JFrame("Progress Bar :: By - Rajat Suneja"); 
button = new JButton("Start"); 
button.addActionListener(new ButtonListener()); 
pb = new JProgressBar(0, 20); 
pb.setValue(0); 
pb.setStringPainted(true); 
label = new JLabel("Rajat Suneja"); 
JPanel panel = new JPanel(); 
panel.add(button); 
panel.add(pb); 
JPanel panel1 = new JPanel(); 
panel1.setLayout(new BorderLayout()); 
panel1.add(panel, BorderLayout.NORTH); 
panel1.add(label, BorderLayout.CENTER); 
panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
frame.setContentPane(panel1); 
frame.pack(); 
frame.setVisible(true); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
timer = new Timer(interval, new ActionListener() 
{ 
public void actionPerformed(ActionEvent evt) 
{ 
if (i == 20) 
{
Toolkit.getDefaultToolkit().beep(); 
timer.stop(); 
button.setEnabled(true); 
pb.setValue(0); 
String str = "<html>" + "<font color="#FF0000">" + "<b>" + 
"Downloading completed." + "</b>" + "</font>" + "</html>"; 
label.setText(str); 
}i 
= i + 1; 
pb.setValue(i); 
} 
}); 
} 
class ButtonListener implements ActionListener 
{ 
public void actionPerformed(ActionEvent ae) 
{ 
button.setEnabled(false); 
i = 0; 
String str = "<html>" + "<font color="#008000">" + "<b>" + "Downloading 
is in process......." + "</b>" + "</font>" + "</html>"; 
label.setText(str); 
timer.start(); 
} 
}p 
ublic static void main(String[] args) 
{ 
bar spb = new bar(); 
} 
} 
OUTPUT -

More Related Content

What's hot

Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3
hasi071
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
ishan0019
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
RACHIT_GUPTA
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
Nishan Barot
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
Chih-Hsuan Kuo
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
Simone Federici
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
Christine Cheung
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
Ted Liang
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
Naveen Sagayaselvaraj
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88
Mahmoud Samir Fayed
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
sameer farooq
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diploma
mustkeem khan
 

What's hot (20)

Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
Mattbrenner
MattbrennerMattbrenner
Mattbrenner
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diploma
 

Viewers also liked

Revealation of Glorious Qur'an
Revealation of Glorious Qur'anRevealation of Glorious Qur'an
Revealation of Glorious Qur'an
Fahad Shaikh
 
Sunnahs of Prophet Mohammed SAW
Sunnahs of Prophet Mohammed SAWSunnahs of Prophet Mohammed SAW
Sunnahs of Prophet Mohammed SAW
Fahad Shaikh
 
Ubuntu Handbook
Ubuntu HandbookUbuntu Handbook
Ubuntu Handbook
Fahad Shaikh
 
Operating Systems
Operating Systems Operating Systems
Operating Systems
Fahad Shaikh
 
A Standard Dictionary of Muslim Names
A Standard Dictionary of Muslim NamesA Standard Dictionary of Muslim Names
A Standard Dictionary of Muslim Names
Caller To Islam / الداعية الإسلامي
 
Islamic Names
Islamic NamesIslamic Names
Islamic Names
Fahad Shaikh
 
Cryptography & Network Security
Cryptography & Network SecurityCryptography & Network Security
Cryptography & Network Security
Fahad Shaikh
 

Viewers also liked (7)

Revealation of Glorious Qur'an
Revealation of Glorious Qur'anRevealation of Glorious Qur'an
Revealation of Glorious Qur'an
 
Sunnahs of Prophet Mohammed SAW
Sunnahs of Prophet Mohammed SAWSunnahs of Prophet Mohammed SAW
Sunnahs of Prophet Mohammed SAW
 
Ubuntu Handbook
Ubuntu HandbookUbuntu Handbook
Ubuntu Handbook
 
Operating Systems
Operating Systems Operating Systems
Operating Systems
 
A Standard Dictionary of Muslim Names
A Standard Dictionary of Muslim NamesA Standard Dictionary of Muslim Names
A Standard Dictionary of Muslim Names
 
Islamic Names
Islamic NamesIslamic Names
Islamic Names
 
Cryptography & Network Security
Cryptography & Network SecurityCryptography & Network Security
Cryptography & Network Security
 

Similar to Advanced Java - Practical File

Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Java practical
Java practicalJava practical
Java practical
william otto
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
Fia fabila
Fia fabilaFia fabila
Fia fabila
fiafabila
 
201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian
KhairunnisaPekanbaru
 
Tugas 2
Tugas 2Tugas 2
Tugas 2
Novi_Wahyuni
 
You are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdfYou are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdf
JUSTSTYLISH3B2MOHALI
 
Distributed systems
Distributed systemsDistributed systems
Distributed systems
Sonali Parab
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
Andres Almiray
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 SpringKiyotaka Oku
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
Assignment no39
Assignment no39Assignment no39
Assignment no39Jay Patel
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
Lezione03
Lezione03Lezione03
Lezione03
robynho86
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
The Software House
 

Similar to Advanced Java - Practical File (20)

Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Java practical
Java practicalJava practical
Java practical
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Fia fabila
Fia fabilaFia fabila
Fia fabila
 
201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian
 
Tugas 2
Tugas 2Tugas 2
Tugas 2
 
You are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdfYou are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdf
 
Distributed systems
Distributed systemsDistributed systems
Distributed systems
 
Dotnet 18
Dotnet 18Dotnet 18
Dotnet 18
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Assignment no39
Assignment no39Assignment no39
Assignment no39
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 

Recently uploaded

The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

Advanced Java - Practical File

  • 1. Q1. Write a program in java to explain the concept of “THREADING”. Solution: - Threads can be implemented in two ways – 1. By Extending the THREAD class. 2. By Implementing the RUNNABLE class. Implementing the “Runnable” class. CODE – class NewThread implements Runnable { Thread t; NewThread() { t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); }p ublic void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } }c atch (InterruptedException e) { System.out.println("Child interrupted."); }S ystem.out.println("Exiting child thread."); } } class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } }c atch (InterruptedException e) { System.out.println("Main thread interrupted."); }S ystem.out.println("Main thread exiting."); } }
  • 2. OUTPUT – Extending the “THREAD” class. CODE – class Share extends Thread { static String msg[]={"This", "is", "a", "synchronized", "variable"}; Share(String threadname) { super(threadname); }p ublic void run() { display(getName()); }p ublic void display(String threadN) { synchronized(this){ for(int i=0;i<=4;i++) { System.out.println(threadN+msg[i]); }t ry { this.sleep(1000); }c atch(Exception e) {} } }}c lass Sync { public static void main(String[] args) { Share t1=new Share("Thread One: "); t1.start(); Share t2=new Share("Thread Two: "); t2.start(); } }
  • 3. OUTPUT – Q2. Write a program in java to implement Socket Programming. Solution: - A Client – Server Chat Program CODE – CLIENT.JAVA import java.net.*; import java.io.*; public class Client implements Runnable { Socket s; BufferedReader br; BufferedWriter bw; BufferedReader ppp; String input = null; public static void main(String[] args) { new Client(); }p ublic void run() { try { s.setSoTimeout(1); }c atch(Exception e) {}w hile (true) { try { System.out.println("Server: "+br.readLine()); }c atch (Exception h) {} } }p ublic Client()
  • 4. { try { s = new Socket("127.0.0.1",100); br = new BufferedReader(new InputStreamReader(s.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); ppp = new BufferedReader(new InputStreamReader(System.in)); Thread th; th = new Thread(this); th.start(); bw.write("Hello Server"); bw.newLine(); bw.flush(); while(true) { input = ppp.readLine(); bw.write(input); bw.newLine(); bw.flush(); } }c atch(Exception e) {} } } SERVER.JAVA import java.net.*; import java.io.*; public class ServerApp implements Runnable { ServerSocket s; Socket s1; BufferedReader br; BufferedWriter bw; BufferedReader ppp; String input = null; public void run() { try { s1.setSoTimeout(1); }c atch(Exception e) {}w hile (true) { try { System.out.println("Client: "+br.readLine()); }c atch (Exception h) {} } }p ublic static void main(String arg[]) { new ServerApp(); }p ublic ServerApp()
  • 5. { try { s = new ServerSocket(100); s1=s.accept(); br = new BufferedReader(new InputStreamReader(s1.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream())); ppp = new BufferedReader(new InputStreamReader(System.in)); Thread th; th = new Thread(this); th.start(); while(true) { input = ppp.readLine(); bw.write(input); bw.newLine(); bw.flush(); } }c atch(Exception e) {} } } OUTPUT – Q3. Write a program in java for Sending E-Mails. Solution: - import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.MimeMessage;
  • 6. public class Gmail { public static void main(String ss[]) { String host = "smtp.gmail.com"; String username = "somgaj"; String password = "qwaszx,.12/3"; try { Properties props = new Properties(); props.put("mail.smtps.auth", "true"); props.put("mail.from","somgaj@gmail.com"); Session session = Session.getInstance(props, null); Transport t = session.getTransport("smtps"); t.connect(host, username, password); MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, "somgaj@gmail.com"); msg.setSubject("Hello Soumya!!"); msg.setSentDate(new Date()); msg.setText("Hi! FROM JAVA....If you recieve this, it is a success...."); t.sendMessage(msg, msg.getAllRecipients()); }c atch(Exception ee) { System.out.println(ee.toString()); } } } OUTPUT –
  • 7. Q4. Write a program in java for Implementing Calculator in an Applet. Solution: - CODE – CAL.JAVA import java.awt.*; import java.awt.event.*; import java.applet.*; class Cal extends Applet implements ActionListener { String msg=" "; int v1,v2,result; TextField t1; Button b[]=new Button[10]; Button add,sub,mul,div,clear,mod,EQ; char OP; public void init() { Color k=new Color(120,89,90); setBackground(k); t1=new TextField(10); GridLayout gl=new GridLayout(4,5); setLayout(gl); for(int i=0;i<10;i++) { b[i]=new Button(""+i); }a d=new Button("add"); sub=new Button("sub"); mul=new Button("mul"); div=new Button("div"); mod=new Button("mod"); clear=new Button("clear"); EQ=new Button("EQ"); t1.addActionListener(this); add(t1); for(int i=0;i<10;i++) { add(b[i]); }a dd(ad); add(sub); add(mul); add(div); add(mod); add(clear); add(EQ); for(int i=0;i<10;i++) { b[i].addActionListener(this); }a d.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); mod.addActionListener(this); clear.addActionListener(this); EQ.addActionListener(this); }
  • 8. public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); char ch=str.charAt(0); if ( Character.isDigit(ch)) t1.setText(t1.getText()+str); else if(str.equals("ad")) { v1=Integer.parseInt(t1.getText()); OP='+'; t1.setText(""); }e lse if(str.equals("sub")) { v1=Integer.parseInt(t1.getText()); OP='-'; t1.setText(""); }e lse if(str.equals("mul")) { v1=Integer.parseInt(t1.getText()); OP='*'; t1.setText(""); }e lse if(str.equals("div")) { v1=Integer.parseInt(t1.getText()); OP='/'; t1.setText(""); }e lse if(str.equals("mod")) { v1=Integer.parseInt(t1.getText()); OP='%'; t1.setText(""); }i f(str.equals("EQ")) { v2=Integer.parseInt(t1.getText()); if(OP=='+') result=v1+v2; else if(OP=='-') result=v1-v2; else if(OP=='*') result=v1*v2; else if(OP=='/') result=v1/v2; else if(OP=='%') result=v1%v2; t1.setText(""+result); }i f(str.equals("clear")) { t1.setText(""); } } }
  • 9. CALCULATOR.HTML <html> <applet code=Cal.class height=500 width=400> </applet> </HTML> OUTPUT –
  • 10. Q6. Write a program in java for Implementing RMI. Solution: - An applet using RMI implementing a calculator. CODE – CLIENT.JAVA import java.rmi.*; import java.rmi.registry.*; import java.awt.*; import java.awt.event.*; class mathClient extends Frame implements ActionListener { Button B1=new Button("Sum"); Button B2=new Button("Subtract"); Button B3=new Button("Multiply"); Button B4=new Button("Divide"); Label l1=new Label("Number 1"); Label l2=new Label("Number 2"); Label l3=new Label("Result"); TextField t1=new TextField(20); TextField t2=new TextField(20); TextField t3=new TextField(20); public mathClient() { super("Calculator"); setLayout(null); l1.setBounds(20,50,55,25); add(l1); l2.setBounds(20,100,55,25); add(l2); l3.setBounds(20,150,55,25); add(l3); t1.setBounds(150,50,100,25); add(t1); t2.setBounds(150,100,100,25); add(t2); t3.setBounds(150,150,100,25); add(t3); B1.setBounds(20,200,80,25); add(B1); B2.setBounds(100,200,80,25); add(B2); B3.setBounds(180,200,80,25); add(B3); B4.setBounds(260,200,80,25); add(B4); B1.addActionListener(this); B2.addActionListener(this); B3.addActionListener(this); B4.addActionListener(this); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }
  • 11. ); } public void actionPerformed(ActionEvent AE) { if(AE.getSource()==B1) {s um(); }e lse if(AE.getSource()==B2) { subt(); }e lse if(AE.getSource()==B3) { mult(); }e lse if(AE.getSource()==B4) { div(); } } public void sum() { int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); int val; try { String ServerURL="MathServ"; mathInterface MI=(mathInterface)Naming.lookup(ServerURL); val=MI.add(i,j); t3.setText(""+val); } catch(Exception ex) { System.out.println("Exception:"+ex); } } public void subt() { int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); int val; try { String ServerURL="MathServ"; mathInterface MI=(mathInterface)Naming.lookup(ServerURL); val=MI.subt(i,j); t3.setText(""+val); } catch(Exception ex) { System.out.println("Exception:"+ex); } } public void mult() { int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); int val; try { String ServerURL="MathServ"; mathInterface MI=(mathInterface)Naming.lookup(ServerURL); val=MI.mult(i,j);
  • 12. t3.setText(""+val); } catch(Exception ex) { System.out.println("Exception:"+ex); } } public void div() { int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); int val; try { String ServerURL="MathServ"; mathInterface MI=(mathInterface)Naming.lookup(ServerURL); val=MI.div(i,j); t3.setText(""+val); } catch(Exception ex) { System.out.println("Exception:"+ex); } } public static void main(String args[]) { mathClient MC=new mathClient(); MC.setVisible(true); MC.setSize(600,500); }; } SERVER.JAVA import java.rmi.*; import java.rmi.Naming.*; import java.rmi.server.*; import java.rmi.registry.*; import java.net.*; import java.util.*; interface mathInterface extends Remote { public int add(int a,int b) throws RemoteException; public int subt(int a,int b) throws RemoteException; public int mult(int a,int b) throws RemoteException; public int div(int a,int b) throws RemoteException; }c lass mathServer extends UnicastRemoteObject implements mathInterface { public mathServer() throws RemoteException { System.out.println("Initializing Server"); }p ublic int add(int a,int b) { return(a+b); }p ublic int subt(int a,int b) { return(a-b); }p ublic int mult(int a,int b) { return(a*b); }
  • 13. public int div(int a,int b) { return(a/b); }p ublic static void main(String args[]) { try { mathServer ms=new mathServer(); java.rmi.Naming.rebind("MathServ",ms); System.out.println("Server Ready"); }c atch(RemoteException RE) { System.out.println("Remote Server Error:"+ RE.getMessage()); System.exit(0); }c atch(MalformedURLException ME) { System.out.println("Invalid URL!!"); } } } OUTPUT – Q7. Write a program in java for reading a file from a designated URL. Solution: - I have used my drop-box account for accessing a file stored in my DROPBOX server. CODE – import java.io.BufferedInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader;
  • 14. import java.net.URL; class MainClass { public static void main(String[] args) throws Exception { URL u = new URL("https://dl.dropbox.com/u/94684259/Rajat.txt"); InputStream in = u.openStream(); in = new BufferedInputStream(in); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } } } OUTPUT – Q8. Write a program in java implementing File input output functions. Solution: - CODE – import java.io.*; public class CopyFile { private static void copyfile(String srFile, String dtFile) { try { File f1 = new File(srFile); File f2 = new File(dtFile);
  • 15. InputStream in = new FileInputStream(f1); OutputStream out = new FileOutputStream(f2); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); }i n.close(); out.close(); System.out.println("File copied."); }c atch(FileNotFoundException ex) { System.out.println(ex.getMessage() + " in the specified directory."); System.exit(0); }c atch(IOException e) { System.out.println(e.getMessage()); } }p ublic static void main(String[] args) { switch(args.length) { case 0: System.out.println("File has not mentioned."); System.exit(0); case 1: System.out.println("Destination file has not mentioned."); System.exit(0); case 2: copyfile(args[0],args[1]); System.exit(0); default : System.out.println("Multiple files are not allow."); System.exit(0); } } } OUTPUT – Q9. Write a program in java implementing Java Beans. Solution: - Implementing a counter with JSP on a server for counting number of website views. CODE – COUNTERBEAN.JAVA package form; public class CounterBean implements java.io.Serializable { int coun = 0;
  • 16. public CounterBean() {}p ublic int getCoun() { coun++; return this.coun; }p ublic void setCoun(int coun) { this.coun = coun; } } COUNTER.JSP <%@ page language="java" %> <jsp:useBean id="counter" scope="session" class="form.CounterBean" /> <HTML> <HEAD><TITLE>Use Bean Counter Example</TITLE> </HEAD> <BODY> <table><tr><td><b>The current count for the counter bean is: </b> <%=counter.getCoun() %></td></tr> </table </BODY> </HTML> OUTPUT –
  • 17. Q10. Write a program in java implementing Swings. Solution: - CODE – import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.html.*; public class bar { final static int interval = 1000; int i; JLabel label; JProgressBar pb; Timer timer; JButton button; public bar() { JFrame frame = new JFrame("Progress Bar :: By - Rajat Suneja"); button = new JButton("Start"); button.addActionListener(new ButtonListener()); pb = new JProgressBar(0, 20); pb.setValue(0); pb.setStringPainted(true); label = new JLabel("Rajat Suneja"); JPanel panel = new JPanel(); panel.add(button); panel.add(pb); JPanel panel1 = new JPanel(); panel1.setLayout(new BorderLayout()); panel1.add(panel, BorderLayout.NORTH); panel1.add(label, BorderLayout.CENTER); panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); frame.setContentPane(panel1); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); timer = new Timer(interval, new ActionListener() { public void actionPerformed(ActionEvent evt) { if (i == 20) {
  • 18. Toolkit.getDefaultToolkit().beep(); timer.stop(); button.setEnabled(true); pb.setValue(0); String str = "<html>" + "<font color="#FF0000">" + "<b>" + "Downloading completed." + "</b>" + "</font>" + "</html>"; label.setText(str); }i = i + 1; pb.setValue(i); } }); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent ae) { button.setEnabled(false); i = 0; String str = "<html>" + "<font color="#008000">" + "<b>" + "Downloading is in process......." + "</b>" + "</font>" + "</html>"; label.setText(str); timer.start(); } }p ublic static void main(String[] args) { bar spb = new bar(); } } OUTPUT -