Java 10
Java 10
package javaprograms;
import java.util.*;
void print()
{
System.out.println("Student ID= "+id+", Student age= "+age+",
Student name= "+name);
}
package javaprograms;
import java.util.*;
package javaprograms;
import java.io.*;
for(int i=0;i<x;i++)
{
System.out.println(b[i]);
}
x=f.available();
f.skip(2);
x=f.available();
byte b1[]=new byte[x];
f.read(b1,0,x);
}
Output:
H
101
108
108
111
32
Q46.) Write a function which write “ABCD” to file alphabet.txt which resides
in temp directory under c:\\ . After writing, it attempts to read the contents.
After reading, the program converts it to lowercase and prints the string on
screen.
import java.io.*;
Output:
abcd
Q47.)Write a program which attempts to read using ByteArrayInputStream
the contents of byte array twice. Let the contents be “ABC”.
import java.io.ByteArrayInputStream;
for(int i=0;i<b1.length;i++)
{
System.out.println((char)bais.read());
}
bais.reset();
Output:
A
B
C
Q48.) Write a program which uses a BufferedInputStream by wrapping a
ByteArrayInputStream. The ByteArrayInputStream stores an input which is
thereafter used for marking, skipping and resetting.
package javaprograms;
import java.io.*;
public class buffered
{
int x=b.length-3;
for(int i=0;i<b.length;i++)
{
System.out.println((char)bis.read());
if(i==x) {bis.mark(3);}
}
bis.reset();
for(x=0;x<b.length;x++)
{
System.out.println((char)x);
}
}
}
}
Output:
h
i
s
i
s
a
&
c
o
p
y
;
c
o
p
y
w
r
i
g
h
t
s
y
m
b
o
l
b
u
t
t
h
i
s
i
s
&
c
o
p
y
n
o
t
?
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/
0
1
2
3
4
5
6
7
8
9
Q49.) Write a program to show the concept of object serialization
package javaprograms;
import java.io.*;
class Point implements Serializable
{
int x,y;
Point(int a,int b)
{
x=a;
y=b;
}
Output: 1,2
Q50.) Write a program which has a try resource statement which attempts to
write double, int and Boolean values to file called test.txt. After writing, the
data is read back and attempted to be printed on the screen.
package javaprograms;
import java.io.*;
public class Demo3 {
Output:
package javaprograms;
Output:
89.98
89.975000
Q52.) Write a program which tries to obtain the Ip Address of the machine
you are working on and also tries to get the Ip address of domain name
“starworld.com” and get all the Ip addresses which are trying to access that
domain name.
package javaprograms;
import java.net.*;
public class X1 {
Output:
DELL-PC/192.168.1.37
Q53.) Write a program to show Client and Server Communication
client.java
package javaprograms;
import java.net.*;
import java.io.*;
public class client {
}catch(Exception e) {}
server.java
package javaprograms;
import java.io.*;
import java.net.*;
public class server implements Runnable
{
Socket s;
public static void main(String[] args)
{
try {
ServerSocket ss=new ServerSocket(7268);
System.out.println("Server Started");
while(true)
{
Socket s1=ss.accept();
server s2=new server(s1);
Thread t1=new Thread(s2);
t1.start();
}
}catch(Exception e) {}
}
public server(Socket s)
{
this.s=s;
}
public void run()
{
try
{
InputStream in=s.getInputStream();
InputStreamReader inr=new InputStreamReader(in);
BufferedReader br=new BufferedReader(inr);
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println("Joyesh Malik");
String logname=br.readLine();
System.out.println("Logname is= "+logname);
}catch(Exception e) {}
Output:
Server Started
Logname is= Joyesh Malik
Q54.) Write a program using UDP protocol wherein the server constructs a
datagram packet which contains ‘Hello World’ and sends it to a datagram
client. The client receives the packet and prints it on the screen.
UDPClient.java
package javaprograms;
import java.io.IOException;
import java.net.*;
UDPServer.java
package javaprograms;
import java.io.IOException;
import java.net.*;
Output:
Hello World
Q55.) Write a program which adds a frame titled by ‘MyfirstAwtPrograms’.
The frame window is set to a dimension size of your choice. Later on we create
three buttons(yes, no, maybe) then the components are added to the container
window and window is made visible.
package javaprograms;
import java.awt.*;
Output:
Q56.) Write a program which adds three buttons ‘yes’, ‘no’ and ‘maybe’ to
the frame window. The same class can also register for the action events
which are associated by pressing the buttons. Write a suitable event handling
mechanism which displays which button was pressed for generating the event.
package javaprograms;
import java.awt.*;
import java.awt.event.*;
public class action implements ActionListener{
action()
{
Frame f=new Frame("My First AWT Program");
f.setSize(400,100);
Button y=new Button("Yes");
y.addActionListener(this);
Button y1=new Button("No");
y1.addActionListener(this);
Button y2=new Button("Maybe");
y2.addActionListener(this);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.add(y);
f.add(y1);
f.add(y2);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
System.out.println(str);
}
public static void main(String args[])
{
new action();
}
}
Output:
Console:
Maybe
Q57.) Write a program which creates four checkboxes based on four
operating systems. The initial state of first box is checked. You can change the
state of your checkbox and the message is displayed on the window whether
you selected or deselected the option.
package javaprograms;
import java.awt.*;
import java.awt.event.*;
}
public static void main(String[] args)
{
new X4();
}
}
Output:
Console:
package javaprograms;
import java.awt.*;
import java.awt.event.*;
public class TF extends Frame implements ActionListener{
TextField tf1,tf2,tf3;
Button b1,b2;
TF(){
tf1=new TextField();
tf1.setBounds(70,50,150,20);
tf2=new TextField();
tf2.setBounds(70,100,150,20);
b1=new Button("Click Here");
b1.setBounds(100,200,70,40);
b1.addActionListener(this);
add(tf1);add(tf2);add(b1);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
String s2=tf2.getText();
int b=Integer.parseInt(s2);
if(e.getSource()==b1){
System.out.println("Your Name is =" +s1+" and Roll no.= "+b);
}
}
public static void main(String[] args) {
new TF();
}
}
Output:
Console:
package javaprograms;
import java.awt.*;
import java.applet.*;
/*<applet code="myApplet" width=300 height=500>*/
public class myApplet extends Applet
{ String msg;
public void init()
{
setBackground(Color.CYAN);
setForeground(Color.red);
msg="Inside init()";
}
}
Output:
Q60.) Write an applet program which adds a label on the north portion of the
border layout, a button called ‘write’ on the last and a text area in the center
part.
package javaprograms;
import java.awt.*;
import java.applet.*;
/*<applet code="myApplet" width=300 height=500>*/
public class myApplet1 extends Applet {
String msg;
Button b1;
public void init()
{
setLayout(new BorderLayout());
Label l=new Label("BorderLayout()");
add(l,BorderLayout.NORTH);
b1=new Button("Write");
add(b1,BorderLayout.AFTER_LAST_LINE);
TextField tf1=new TextField();
add(tf1,BorderLayout.CENTER);
msg="Inside init()";
}
}
Output:
Q61.) Write an applet program which sets the layout manager to flow layout.
Add three buttons ‘yes’, ‘no’ and ‘maybe’. Then all these three buttons are
event handled by the applet class with the appropriate messages.
package javaprograms;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
Output: