Friday, February 25, 2011
Sunday, February 20, 2011
Java complete lab programs
OBJECT ORIENTED PROGRAMMING LAB
Objectives:
· To make the student learn a object oriented way of solving problems.
· To teach the student to write programs in Java to solve the problems
Recommended Systems/Software Requirements:
· Intel based desktop PC with minimum of 166 MHZ or faster processor with atleast 64 MB RAM and 100 MB free disk space
· JDK Kit. Recommended
Week1 :
a) Write a Java program that prints all real solutions to the quadratic equation ax2 + bx + c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display a message stating that there are no real solutions.
b) The Fibonacci sequence is defined by the following rule:
The fist two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write a Java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci sequence.
Week 2 :
a) Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer.
b) Write a Java program to multiply two given matrices.
c) Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util)
Week 3 :
a) Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome.
b) Write a Java program for sorting a given list of names in ascending order.
c) Write a Java program to make frequency count of words in a given text.
Week 4 :
a) Write a Java program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes.
b) Write a Java program that reads a file and displays the file on the screen, with a line number before each line.
c) Write a Java program that displays the number of characters, lines and words in a text file.
Week 5 :
a) Write a Java program that:
i) Implements stack ADT.
ii) Converts infix expression into Postfix form
iii) Evaluates the postfix expression
Week 6 :
a) Develop an applet that displays a simple message.
b) Develop an applet that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked.
Week 7 :
Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result.
Week 8 :
a) Write a Java program for handling mouse events.
Week 9 :
a) Write a Java program that creates three threads. First thread displays “Good Morning” every one second, the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds.
b) Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication.
Week 10 :
Write a program that creates a user interface to perform integer divisions. The user enters two numbers in the textfields, Num1 and Num2. The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a NumberFormatException. If Num2 were Zero, the program would throw an ArithmeticException Display the exception in a message dialog box.
Week 11 :
Write a Java program that implements a simple client/server application. The client sends data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. For ex: The data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle. (Use java.net)
Week 12 :
a) Write a java program that simulates a traffic light. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one light can be on at a time No light is on when the program starts.
b) Write a Java program that allows the user to draw lines, rectangles and ovals.
Week 13 :
a) Write a java program to create an abstract class named Shape that contains an empty method named numberOfSides ( ).Provide three classes named Trapezoid, Triangle and Hexagon such that each one of the classes extends the class Shape. Each one of the classes contains only the method numberOfSides ( ) that shows the number of sides in the given geometrical figures.
b) Suppose that a table named Table.txt is stored in a text file. The first line in the file is the header, and the remaining lines correspond to rows in the table. The elements are seperated by commas. Write a java program to display the table using JTable component.
TEXT BOOKS :
1. Java How to Program, Sixth Edition, H.M.Dietel and P.J.Dietel, Pearson
Education/PHI
2. Introduction to Java programming, Sixth edition, Y.Daniel Liang, Pearson Education
3. Big Java, 2nd edition, Cay Horstmann, Wiley Student Edition, Wiley India Private Limited.
Week1
a) Write a Java program that prints all real solutions to the quadratic equation ax2 + bx + c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display a message stating that there are no real solutions.
b) The Fibonacci sequence is defined by the following rule:
The fist two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write a Java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci sequence.
Aim :a) Write a Java program that prints all real solutions to the quadratic equation ax2 + bx + c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display a message stating that there are no real solutions.
Program :
import java.lang.*;
import java.io.*;
class Roots
{
public static void main(String args[])
{
double a,b,c,root1,root2;
a=Double.parseDouble(args[0]);
b=Double.parseDouble(args[1]);
c=Double.parseDouble(args[2]);
System.out.println("\n value of A is ="+a+"\n Value of B is ="+b+"\n Value of C is ="+c);
/*checking condition*/
if(b*b>=4*a*c)
{
root1=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
root2=(-b-Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println("\n*****ROOTS ARE*****\n");
System.out.println("\n root1="+root1+"\n root2="+root2);
}
else
System.out.println("\n Imaginary Roots.");
} }
Output:
javac Roots.java
java Roots 1.000 4.000 4.000
value of A is =1.0
Value of B is =4.0
Value of C is =4.0
*****ROOTS ARE*****
root1=-2.0
root2=-2.0
Aim :b) The Fibonacci sequence is defined by the following rule:
The fist two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write a Java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci sequence.
Program :
import java.io.*;
class Fibonacci
{
int c,z,n,m;
int recursive(int a,int b)
{
if(n==2)
return 0;
else
{
c=a+b;
a=b;
b=c;
n--;
recursive(a,b);
return c;
}}
int nonrecursive(int x, int y)
{
while((m-2)>0)
{
z=x+y;
x=y;
y=z;
m--;
}return z;
} }
class mainclass
{
public static void main(String[] args)
{
Fibonacci f=new Fibonacci();
f.n=f.m=Integer.parseInt(args[0]);
System.out.println("The Fibnocci series number of "+f.n+" Recursive funtion is"+f.recursive(1,1));
System.out.println("The Fibnocci series number of "+f.m+" Non-Recursive funtion is "+f.nonrecursive(1,1));
} }
Output:
javac mainclass.java
java mainclass 8
The Fibnocci series number of 8 Recursive funtion is21
The Fibnocci series number of 8 Non-Recursive funtion is 21
Week 2
a) Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer.
b) Write a Java program to multiply two given matrices.
c) Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util)
Aim :a) Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer.
Program :
import java.io.*;
class Primenumbers
{
public static void main(String[ ] args)
{
int i,j,count,number;
number=Integer.parseInt(args[0]);
System.out.println("Prime numbers upto "+ number +" are :");
for(i=1;i<=number;i++)
{
count=0;
for(j=1;j<=number;j++)
{
if(i%j==0)
count++;
}
if(count==2)
System.out.println(i);
} } }
Output :
javac Prime.java
java Primenumbers 6
Prime numbers upto 6 are :
2
3
5
Aim :b) Write a Java program to multiply two given matrices.
Program:
class matrixmul
{
public static void main(String args[])
{
int i,j,k,y=2,row,column;
row=Integer.parseInt(args[0]);
column=Integer.parseInt(args[1]);
if(row==column)
{
int a[ ] [ ]=new int[row] [column];
int b[ ] [ ]=new int[row] [column];
int c[ ] [ ]=new int[row] [column];
System.out.println("Elements in Array A are:");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
a[i] [j]=Integer.parseInt(args[y]);
y++;
System.out.print(" "+a[i] [j]);
}
System.out.println( );
}
System.out.println("Elements in Array B are:");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
b[i] [j]=Integer.parseInt(args[y]);
y++;
System.out.print(" "+b[i] [j]);
}
System.out.println( );
}
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
c[i] [j]=0;
for(k=0;k<column;k++)
c[i] [j]=c[i] [j]+a[i] [k]*b[k] [j];
} }
System.out.println(" ");
System.out.println("Array Multiplication is :");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
System.out.print(c[i] [j]+" ");
}
System.out.println(" ");
} }
else
{
System.out.println("Error Multuplication is not possible");
} } }
Output:
javac matrixmul.java
java matrixmul 2 2 2 2 2 2 3 3 3 3
Elements in Array A are:
2 2
2 2
Elements in Array B are:
3 3
3 3
Array Multiplication is :
12 12
12 12
Aim :(c) Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util)
Program:
import java.lang.*;
import java.util.*;
class tokendemo {
public static void main(String args[ ]) {
String s="10,20,30,40,50";
int sum=0;
StringTokenizer a=new StringTokenizer(s,",",false);
System.out.println("integers are ");
while(a.hasMoreTokens()) {
int b=Integer.parseInt(a.nextToken());
sum=sum+b;
System.out.println(" "+b);
}
System.out.println("sum of integers is "+sum);
}
}
Output:
javac Palind.java
java tokendemo
integers are
10
20
30
40
50
sum of integers is 150
Week 3
a) Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome.
b) Write a Java program for sorting a given list of names in ascending order.
c) Write a Java program to make frequency count of words in a given text.
Aim : a) Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome.
Program :
b) Write a Java program for sorting a given list of names in ascending order.
c) Write a Java program to make frequency count of words in a given text.
Aim : a) Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome.
Program :
class palindrom
{
public static void main(String args[])
{
String s="madam";
int i;
int n=s.length();
String str="";
System.out.println("*****To Find Given String Palindrom Or Not*****");
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);
if(str.equals(s))
System.out.println(s+ " is palindrome");
else
System.out.println(s+ " is not a palindrome");
}
}
{
public static void main(String args[])
{
String s="madam";
int i;
int n=s.length();
String str="";
System.out.println("*****To Find Given String Palindrom Or Not*****");
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);
if(str.equals(s))
System.out.println(s+ " is palindrome");
else
System.out.println(s+ " is not a palindrome");
}
}
Output:
javac palindrom.java
java palindrom
*****To Find Given String Palindrom Or Not*****
madam is palindrome
Aim :b) Write a Java program for sorting a given list of names in ascending order.
Program :
class Sorting
{
static String s[ ]={"K.Deepika",
"M.Srinivas",
"CH.Panchamukesh",
"Gayathri",
"N.Srinivas",
"T.Guptha"};
public static void main(String args[ ])
{
int i,j;
int n=s.length;
String t=null;
System.out.println("******Sorted Order of Names Are******");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(s[j].compareTo(s[i])<0)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
System.out.println(s[i]);
} } }
Output:
javac sorting.java
java sorting
******Sorted Order of Names Are******
CH.Panchamukesh
Gayathri
K.Deepika
M.Srinivas
N.Srinivas
T.Guptha
Aim: c) Write a Java program to make frequency count of words in a given text.
Program :
javac sorting.java
java sorting
******Sorted Order of Names Are******
CH.Panchamukesh
Gayathri
K.Deepika
M.Srinivas
N.Srinivas
T.Guptha
Aim: c) Write a Java program to make frequency count of words in a given text.
Program :
class countwords
{
public static void main(String args[])
{
int i,c=0;
System.out.println(" ***** To Count Number Of Words In The Given Text *****");
for(i=0;i<args.length;i++)
{ System.out.println(args[i]);
c++;
}
System.out.println("number of words="+c);
}
}
Output:
javac countwords.java
java countwords Hai IT Students This Is Mukesh
***** To Count Number Of Words In The Given Text *****
Hai
IT
Students
This
Is
Mukesh
number of words=6
Week 4
a) Write a Java program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes.
b) Write a Java program that reads a file and displays the file on the screen, with a line number before each line.
c) Write a Java program that displays the number of characters, lines and words in a text file.
Aim : a) Write a Java program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes.
Program :
import java.io.*;
class FileDemo
{
public static void main(String args[])
{
File f1=new File("/myjava","palindrom.java");
System.out.println("file name"+f1.getName());
System.out.println("path"+f1.getPath());
System.out.println("parent"+f1.getParent());
System.out.println(f1.exists());
System.out.println(f1.canRead());
System.out.println(f1.canWrite());
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
System.out.println(f1.lastModified());
System.out.println(f1.length());
System.out.println(f1.delete());
}
}
Output:
javac FileDemo.java
java FileDemo
file : namepalindrom.java
path :\myjava\palindrom.java
parent :\myjava
true
true
true
false
true
1298093080109
321
true
class FileDemo
{
public static void main(String args[])
{
File f1=new File("/myjava","palindrom.java");
System.out.println("file name"+f1.getName());
System.out.println("path"+f1.getPath());
System.out.println("parent"+f1.getParent());
System.out.println(f1.exists());
System.out.println(f1.canRead());
System.out.println(f1.canWrite());
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
System.out.println(f1.lastModified());
System.out.println(f1.length());
System.out.println(f1.delete());
}
}
Output:
javac FileDemo.java
java FileDemo
file : namepalindrom.java
path :\myjava\palindrom.java
parent :\myjava
true
true
true
false
true
1298093080109
321
true
Aim: b) Write a Java program that reads a file and displays the file on the screen, with a line number before each line.
Program :
import java.io.*;
class linenum
{
public static void main(String[ ] args)throws IOException
{
FileInputStream fil;
LineNumberInputStream line;
int i;
try
{
fil=new FileInputStream(args[0]);
line=new LineNumberInputStream(fil);
}
catch(FileNotFoundException e)
{
System.out.println("No such file found");
return;
}
do
{
i=line.read();
if(i=='\n')
{
System.out.println();
System.out.print(line.getLineNumber()+" ");
}
else
System.out.print((char)i);
}while(i!=-1);
fil.close();
line.close();
} }
Output:
javac linenum.java
java linenum linenum.java
1 class linenum
2 {
3 public static void main(String[] args)throws IOException
4 {
5 FileInputStream fil;
6 LineNumberInputStream line;
7 int i;
8 try
9 {
10 fil=new FileInputStream(args[0]);
11 line=new LineNumberInputStream(fil);
12 }
13 catch(FileNotFoundException e)
14 {
15 System.out.println("No such file found");
16 return;
17 }
18 do
19 {
20 i=line.read();
21 if(i=='\n')
22 {
23 System.out.println();
24 System.out.print(line.getLineNumber()+" ");
25 }
26 else
27 System.out.print((char)i);
28 }while(i!=-1);
29 fil.close();
30 line.close();
31 }
32 }
Aim : c) Write a Java program that displays the number of characters, lines and words in a text file.
Program :
import java.io.*;
import java.util.*;
class FileStat
{
public static void main(String args[ ])throws IOException
{
long nl=0,nw=0,nc=0;
String line;
BufferedReader br=new BufferedReader(new FileReader(args[0]));
while ((line=br.readLine())!=null)
{
nl++;
nc=nc+line.length();
StringTokenizer st = new StringTokenizer(line);
nw += st.countTokens();
}
System.out.println("Number of Characters"+nc);
System.out.println("Number of Words"+nw);
System.out.println("Number of Lines"+nl);
}
}
Output:
javac FileStat.java
java FileStat FileStat.java
Number of Characters468
Number of Words48
Number of Lines21
WEEK 5
AIM :a) Write a Java program that:
i) Implements stack ADT.
Program:
import java.util.*;
import javax.swing.JOptionPane;
class Stackdemo
{
public static void main(String[] args)
{
Stack st=new Stack();
int j;
do
{
String s=JOptionPane.showInputDialog("enter ur choice\n1->input\n2->delete\n3->exit");
j=Integer.parseInt(s);
if(j==1)
{
s=JOptionPane.showInputDialog("enter an element");
st.push(Integer.parseInt(s));
JOptionPane.showMessageDialog(null,"stack elements:"+st,"stack elements",JOptionPane.INFORMATION_MESSAGE);
}
try
{
if(j==2)
{
Integer a=(Integer)st.pop();
JOptionPane.showMessageDialog(null,"stack elements:"+st,"stack elements",JOptionPane.INFORMATION_MESSAGE);
}
}
catch(EmptyStackException e)
{
JOptionPane.showMessageDialog(null,"STACK IS EMPTY"+e,"stack elements",JOptionPane.INFORMATION_MESSAGE);
}
}while(j<3);
}
}
Output :
javac Stackdemo.java
java Stackdemo
Week 6 :
a) Develop an applet that displays a simple message.
b) Develop an applet that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked. Aim:- Develop an applet that displays a simple message.
Program :
/* JAVA CODE */
import java.applet.*;import java.awt.*;
public class Simple extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(10,10,100,10);
g.setColor(Color.blue);
g.drawLine(100,10,100,100);
g.setColor(Color.green);
g.drawLine(10,10,100,100);
}}
/*APPLET CODE*/
<Applet code="Simple.class" width=500 height=500>
</Applet>
Output :
javac Simple.java
appletviewer Simple.html
Aim: Develop an applet that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked.
Program:
/*JAVA CODE*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class fact extends Applet implements ActionListener
{
int n;
TextField t1, t2;
Label l1;
Button b;
public void init()
{
l1=new Label("enter n value",Label.LEFT);
t1=new TextField(20);
b=new Button("compute");
t2=new TextField(20);
add(l1);
add(t1);
add(b);
add(t2);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s1=t1.getText();
int f=1;
int n=Integer.parseInt(s1);
for(int i=1;i<=n;i++)
f=f*i;
String s="fact="+f;
t2.setText(s);
}
}
import java.awt.event.*;
import java.applet.*;
public class fact extends Applet implements ActionListener
{
int n;
TextField t1, t2;
Label l1;
Button b;
public void init()
{
l1=new Label("enter n value",Label.LEFT);
t1=new TextField(20);
b=new Button("compute");
t2=new TextField(20);
add(l1);
add(t1);
add(b);
add(t2);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s1=t1.getText();
int f=1;
int n=Integer.parseInt(s1);
for(int i=1;i<=n;i++)
f=f*i;
String s="fact="+f;
t2.setText(s);
}
}
/*APPLET CODE*/
<applet code="fact.class" width=300 height=300></applet>
Output:
javac fact.java
appletviewer fact.html
Week 7 :
Aim : Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result.Program :
/* JAVA CODE */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Calculator 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);
}
add=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(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.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("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(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);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}/*APPLET CODE */
<applet code="Calculator.class" width=300 height=300>
</applet>
Output :
javac Calculator.java
appletviewer Calculator.html
week 8
Aim :Write a Java program for handling mouse events
Program :
/*JAVA CODE*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Mouse extends Applet
implements MouseListener,MouseMotionListener
{
int X=0,Y=20;
String msg="MouseEvents";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.black);
setForeground(Color.red);
}
public void mouseEntered(MouseEvent m)
{
setBackground(Color.magenta);
showStatus("Mouse Entered");
repaint();
}
public void mouseExited(MouseEvent m)
{
setBackground(Color.black);
showStatus("Mouse Exited");
repaint();
}
public void mousePressed(MouseEvent m)
{
X=10;
Y=20;
msg="NEC";
setBackground(Color.green);
repaint();
}
public void mouseReleased(MouseEvent m)
{
X=10;
Y=20;
msg="Engineering";
setBackground(Color.blue);
repaint();
}
public void mouseMoved(MouseEvent m)
{
X=m.getX();
Y=m.getY();
msg="College";
setBackground(Color.white);
showStatus("Mouse Moved");
repaint();
}
public void mouseDragged(MouseEvent m)
{
msg="CSE";
setBackground(Color.yellow);
showStatus("Mouse Moved"+m.getX()+" "+m.getY());
repaint();
}
public void mouseClicked(MouseEvent m)
{
msg="Students";
setBackground(Color.pink);
showStatus("Mouse Clicked");
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}/*APPLET CODE*/
<applet code="Mouse.class" width=300 height=300>
</applet>
Output:
javac Mouse.java
appletviewer Mouse.html
Week 9 :
Aim: Program :
class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New Thread:"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println(name+ ":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name+" Interrupted");
}
System.out.println(name+" exiting");
}
}
class MultiThreadDemo
{
public static void main(String[] args)
{
new NewThread("One");
new NewThread("Two");
new NewThread("Three");
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
}
}
Output :
javac NewThread.java
java MultiThreadDemo
New Thread:Thread[One,5,main]
New Thread:Thread[Two,5,main]
One:5
New Thread:Thread[Three,5,main]
Two:5
Three:5
Two:4
One:4
Three:4
Two:3
One:3
Three:3
Two:2
One:2
Three:2
Two:1
One:1
Three:1
Two exiting
One exiting
Three exiting
Main Thread ExitingWeek 10 :
Aim : Write a program that creates a user interface to perform integer divisions. The user enters two numbers in the textfields, Num1 and Num2. The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a NumberFormatException. If Num2 were Zero, the program would throw an ArithmeticException Display the exception in a message dialog boxProgram :
/*JAVA CODE*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ap extends Applet implements ActionListener
{
Button b1;
TextField t1, t2,r ;
Label l1,l2;
String s,e;
public void init()
{
l1=new Label("enter x");
l2=new Label("enter y");
t1=new TextField(10);
t2=new TextField(10);
r=new TextField(10);
b1=new Button("division");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(r);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
s="division="+(x/y);
r.setText(s);
}}
import java.awt.event.*;
import java.applet.*;
public class ap extends Applet implements ActionListener
{
Button b1;
TextField t1, t2,r ;
Label l1,l2;
String s,e;
public void init()
{
l1=new Label("enter x");
l2=new Label("enter y");
t1=new TextField(10);
t2=new TextField(10);
r=new TextField(10);
b1=new Button("division");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(r);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
s="division="+(x/y);
r.setText(s);
}}
/*APPLET CODE*/
<Applet code="ap.class" width=500 height=500>
</Applet>
<Applet code="ap.class" width=500 height=500>
</Applet>
Output:
javac ap.java
appletviewer ap.html
Week 12
Aim : Write a Java program that allows the user to draw lines, rectangles and ovals.Program :
/*JAVA CODE*/
import java.awt.*;
import java.applet.*;
public class Figures extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<=250;i++)
{
Color c1=new Color(35-i,55-i,110-i);
g.setColor(c1);
g.drawRect(250+i,250+i,100+i,100+i);
g.drawOval(100+i,100+i,50+i,50+i);
g.drawLine(50+i,20+i,10+i,10+i);
}
}
} /*APPLET CODE*/
<applet code="Figures.class" width=200 height=200>
</applet>
Output:
javac Figures.java
appletviewer Figures.html
import
}
}
Subscribe to:
Posts (Atom)