The document discusses different ways to implement threading in Java programs. It provides code examples to demonstrate creating threads by extending the Thread class and implementing the Runnable interface. The code examples show printing output from both the main thread and child threads to illustrate threading concepts. Socket programming and RMI examples are also provided with code to implement client-server applications using threads.
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)
{
sum();
}
else if(AE.getSource()==B2)
{
subt();
}
else if(AE.getSource()==B3)
{
mult();
}
else 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;
}
class mathServer extends UnicastRemoteObject implements mathInterface
{
public mathServer() throws RemoteException
{
System.out.println("Initializing Server");
}
public int add(int a,int b)
{
return(a+b);
}
public int subt(int a,int b)
{
return(a-b);
}
public int mult(int a,int b)
{
return(a*b);
}
13. public int div(int a,int b)
{
return(a/b);
}
public static void main(String args[])
{
try
{
mathServer ms=new mathServer();
java.rmi.Naming.rebind("MathServ",ms);
System.out.println("Server Ready");
}
catch(RemoteException RE)
{
System.out.println("Remote Server Error:"+ RE.getMessage());
System.exit(0);
}
catch(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);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex)
{
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
public 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()
{
}
public int getCoun()
{
coun++;
return this.coun;
}
public 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();
}
}
public static void main(String[] args)
{
bar spb = new bar();
}
}
OUTPUT -