Q1. Write A Program in Java To Explain The Concept of "THREADING". Solution
Q1. Write A Program in Java To Explain The Concept of "THREADING". Solution
// create
OUTPUT
OUTPUT
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();
}
public void run()
{
try
{
s.setSoTimeout(1);
}
catch(Exception e)
{
}
while (true)
{
try
{
System.out.println("Server: "+br.readLine());
}
catch (Exception h)
{
}
}
}
public 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();
}
}
catch(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);
}
catch(Exception e)
{
}
while (true)
{
try
{
System.out.println("Client: "+br.readLine());
}
catch (Exception h)
{
}
}
}
public static void main(String arg[])
{
new ServerApp();
}
public 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();
}
}
catch(Exception e)
{
}
OUTPUT
java.util.Date;
java.util.Properties;
javax.mail.Message;
javax.mail.Session;
javax.mail.Transport;
javax.mail.internet.MimeMessage;
OUTPUT
}
catch(Exception ee)
{
System.out.println(ee.toString());
}
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);
}
ad=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]);
}
add(ad);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
ad.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
CALCULATOR.HTML
<html>
<applet code=Cal.class height=500 width=400>
</applet>
</HTML>
OUTPUT
CODE
CLIENT.JAVA
import
import
import
import
java.rmi.*;
java.rmi.registry.*;
java.awt.*;
java.awt.event.*;
);
}
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);
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);
}
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
}
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
CODE
COUNTERBEAN.JAVA
package form;
public class CounterBean implements java.io.Serializable
{
int coun = 0;
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
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 -