1.W.J.P.find the area of circle import java.io.DataInputStream; class circle { public static void main(String args [ ]) { DataInputStream in = new DataInputStream(System.in); int y = 0; double area; try { System.out.print("Enter redius : "); y = Integer.parseInt(in.readLine()); } catch (Exception e){System.out.println("Error......!"); } area = Math.PI*y*y; System.out.println("Area is " + area); } } Output: Enter redius : 4 Area is 50.26 2.W.J.P. that will display Factorial of the given number. import java.io.DataInputStream; class facto { public static void main(String args [ ]) { DataInputStream in = new DataInputStream(System.in); int y = 0; double fact=1.0; try { System.out.println("Enter the number "); y = Integer.parseInt(in.readLine()); } catch (Exception e){System.out.println("Error......!"); } for(int i=1;i<=y;i++) fact *= i; System.out.println("Factorial is " + fact); } } Output: Enter the number = 4 Factorial is 24 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1.W.J.P.find the area of circle
import java.io.DataInputStream;class circle{
public static void main(String args [ ]){DataInputStream in = new DataInputStream(System.in);int y = 0;double area;
Exit from BFrom ThreadA : i = 3From ThreadC : k = 1From ThreadA : i = 4From ThreadC : k = 2From ThreadC : k = 3From ThreadC : k = 4From ThreadC : k = 5
Exit from CFrom ThreadA : i = 5
Exit from A
20. W.J.P. which shows the use of yield(),stop() and sleep() methods.
class A extends Thread{
public void run(){
for(int i=1;i<=5;i++){
if(i==1) yield();System.out.println("\t From ThreadA : i = "+i);
}System.out.println("Exit from A");}
}
class B extends Thread{
public void run(){
for(int j=1;j<=5;j++){
if(j==3) stop();System.out.println("\t From ThreadB : j = "+j);
}System.out.println("Exit from B");}
}
class C extends Thread{
public void run(){
for(int k=1;k<=5;k++)
8
{System.out.println("\t From ThreadC : k = "+k);if(k==1)try{
Output :Start thread AStart thread BStart thread C
From ThreadB : j = 1 From ThreadA : i = 1 From ThreadA : i = 2 From ThreadA : i = 3 From ThreadA : i = 4 From ThreadA : i = 5Exit from A From ThreadC : k = 1 From ThreadB : j = 2 From ThreadC : k = 2 From ThreadC : k = 3 From ThreadC : k = 4 From ThreadC : k = 5Exit from C
9
21.W.J.P. which shows the priority in threads
class A extends Thread{
public void run(){
System.out.println("Thread A started");for(int i=1;i<=4;i++){
System.out.println("\t From ThreadA : i = "+i);}
System.out.println("Exit from A");}
}
class B extends Thread{
public void run(){
System.out.println("Thread B started");for(int j=1;j<=4;j++){
System.out.println("\t From ThreadB : j = "+j);}
System.out.println("Exit from B");}
}
class C extends Thread{
public void run(){
System.out.println("Thread C started");for(int k=1;k<=4;k++){
System.out.println("\t From ThreadC : k = "+k);}
System.out.println("Exit from C");}
}
class threadpriority{
public static void main(String args[]){
A threadA=new A();B threadB=new B();C threadC=new C();
From ThreadA : i = 1Thread C startedThread B started
From ThreadC : k = 1From ThreadC : k = 2From ThreadC : k = 3
From ThreadC : k = 4Exit from C From ThreadA : i = 2End of main thread From ThreadA : i = 3 From ThreadB : j = 1 From ThreadA : i = 4 From ThreadB : j = 2Exit from A From ThreadB : j = 3 From ThreadB : j = 4Exit from B
22.W.J.P. which use runnable interface.
class X implements Runnable{
public void run(){
for(int i=1;i<=10;i++){
System.out.println("threadX:"+i);}System.out.println("end of ThreadX");
}}
11
class runnableinterface{
public static void main(String rgs[]){
X runnable=new X();Thread threadx=new Thread(runnable);threadx.start();System.out.println("End of main Thread");
}}
Output :End of main ThreadthreadX: 1threadX: 2threadX: 3threadX: 4threadX: 5threadX: 6threadX: 7threadX: 8threadX: 9threadX: 10
end of ThreadX
23.W.J.P. which use try and catch for exception handling
class trycatch{
public static void main(String args[]){
int a=10;int b=5;int c=5;int x,y;try{
x=a/(b-c); // here is the exception}catch(ArithmeticException e){
System.out.println("Division by zero");}y=a/(b+c);System.out.println("Y = "+y);
}}
Output: Division by zeroY = 1
12
24.W.J.P. which use multiple catch blocks
class multiplecatch{
public static void main(String args[]){
int a[]={5,10};int b=5;
try{
int X=a[2]/b-a[1];}catch(ArithmeticException e){
System.out.println("Division by zero");}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index error");}catch(ArrayStoreException e){
System.out.println("Wrong data type");}
int y=a[1]/a[0];System.out.println("Y = "+y);
}}
Output : Array index error Y = 2
25. W.J.P. which shows throwing our own exception
import java.lang.Exception;
class myexception extends Exception{
myexception(String message){
super(message);}
}
class ownexception{
public static void main(String args[]){
int x=5,y=1000;
13
try{
float z=(float) x / (float) y;if(z<0.01){
throw new myexception("number is too small");}
}
catch(myexception e){
System.out.println("caught my exception");System.out.println(e.getMessage());
}finally{
System.out.println("I M always here");}
}}
Output: caught my exception number is too small I M always here
26. make an applet that create two bottons named “red”and “Blue” when a buttons is pressed the background color of the circle is set to the color named by the button’s label.
<html><head><title>wel come to java applets</title></head><body><center><APPLET code=appletredblue.class width=400 height=200> </APPLET></center></body></html>
Output :
15
27. Write java applet that creates some text fields and text areas to demonstrate features of each
public class WriteFile extends Applet{ Button write = new Button("WriteToFile"); Label label1 = new Label("Enter the file name:"); TextField text = new TextField(20); Label label2 = new Label("Write your text:"); TextArea area = new TextArea(10,20); public void init(){ add(label1); label1.setBackground(Color.lightGray); add(text); add(label2); label2.setBackground(Color.lightGray); add(area); add(write,BorderLayout.CENTER); write.addActionListener(new ActionListener (){ public void actionPerformed(ActionEvent e){ new WriteText(); } }); } public class WriteText { WriteText(){ try { String str = text.getText(); if(str.equals("")){ JOptionPane.showMessageDialog(null,"Please enter the file name!"); text.requestFocus(); } else{ File f = new File(str); if(f.exists()){ BufferedWriter out = new BufferedWriter(new FileWriter(f,true)); if(area.getText().equals("")){ JOptionPane.showMessageDialog(null,"Please enter your text!"); area.requestFocus(); }
16
else{ out.write(area.getText()); if(f.canWrite()){ JOptionPane.showMessageDialog(null,"Text is written in "+str); text.setText(""); area.setText(""); text.requestFocus(); } else{ JOptionPane.showMessageDialog(null,"Text isn't written in "+str); } out.close(); } } else{ JOptionPane.showMessageDialog(null,"File not found!"); text.setText(""); text.requestFocus(); } } } catch(Exception x){ x.printStackTrace(); } } }}
• HTML Coading *
<HTML><HEAD><TITLE> Write file example </TITLE><applet code="WriteFile.class", width="200",height="300"></applet></HEAD></HTML>
Output :
17
28. Create an applet with three text Fields and two buttons add and subtract. User will entertwo values in the Text Fields. When the button add is pressed, the addition of the twovalues should be displayed in the third Text Fields. Same the Subtract button shouldperform the subtraction operation.
<html><head><title>wel come to java applets</title></head><body><center><APPLET code= sumsub.class width=400 height=200> </APPLET></center></body></html>
Output :
19
29. Create an applet to display the scrolling text. The text should move from right to left. Whenit reaches to start of the applet border, it should stop moving and restart from the left. Whenthe applet is deactivated, it should stop moving. It should restart moving from the previouslocation when again activated.
public class scrollingtext extends Applet implements Runnable{ int X=290,Y=200,flag=1; String msg ="Lovely Scrolling Text"; Thread t1; public void init() { t1 = new Thread(this); t1.start(); } public void start(){} public void stop(){}
<html><head><title>wel come to java applets</title></head><body><center><APPLET code= scrollingtext.class width=400 height=200> </APPLET></center></body></html>
Output :
21
30. Write a program to create three scrollbar and a label. The background color of the lableshould be changed according to the values of the scrollbars (The combination of the values RGB).
<html><head><title>wel come to java applets</title></head><body><center><APPLET code= scrol.class width=400 height=200> </APPLET></center></body></html>