Você está na página 1de 34

Prepared by: Ashok and Anil.

List of Java Programs

S.No Name of the program PageNo


01 Java application to find the roots for the equation ax^2+bx+c=0 2-3
02 Write a java program to print Fibonacci series up to n terms using
recursion
03 Write a java program that prompts the user for an integer and then
prints out all prime numbers up to that integer
04 Write a java program that checks whether a given string is
palindrome or not. Ex: MADAM is a palindrome.
5 Write a java program for sorting a given list of names passed from
the command line in ascending order
6 Write a java application to multiply two Matrices
7 Write a java application that reads a line of integers, and then
display each integers and the sum of all integers (Using
StringTokenizer class)
8 Write a java program that reads file and displays information about
whether the file is existing , readable, writable, type of file and
length of file in bytes
9 Write a java program that reads file and displays file on screen with
Line number before each line
10 Write a java program that text reads file and displays number of
characters ,lines and words
11. a Write a java program that implements Stack ADT
11. b Write a java program to converts infix expression into Postfix form
12 Write an applet that displays a simple message
13 write a applet that computes loan payment based on the amount of
the loan, the interest rate and the number of months
14 Java application for a simple calculator
15 Write a java program for handling mouse events
16 Write a java program creating multiple Threads
17 Producer Consumer Problem using Inter-Thread Communication
18 Write a java program that lets user to create pie charts.
19 Write a java program that allows the user to draw lines, rectangles
and ovals
20 Client-Server program where Client program to send radius to
server and the server returns area of circle
21 Write a java program that illustrates how run time polymorphism is
achieved

2 Prepared by: Ashok and Anil


/* 1.Java application to find the roots for the equation ax^2+bx+c=0 */
import java.util.*;
public class FirstDemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double a,b,c;
System.out.println("Enter a,b and c values");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
double d,r1,r2;
d=b*b-4*a*c;
if(d<0)
System.out.println("There are no real roots, having complex roots");
else if(d==0)
System.out.println("Both roots are equal: "+b+" and "+(-b));
else
{
r1=(-b+d)/2*a;
r2=(-b-d)/2*a;
System.out.println("Roots are: "+r1+" abd "+r2);
}
}
}

/* OUTPUT:
C:\>javac FirstDemo.java

C:\>java FirstDemo
Enter a,b and c values
257
There are no real roots, having complex roots

C:\>java FirstDemo
Enter a,b and c values
3 12 4
Roots are: 126.0 abd -162.0

C:\>java FirstDemo
Enter a,b and c values
121
Both roots are equal: 2.0 and -2.0
*/

3
/* 2.Write a java program to print fibonacci series up to n terms usinf recursion */
import javax.swing.JOptionPane;
import java.util.*;
class Fib
{
int a=0,b=1,c;
String s=" ";
void fibonacci(int n)
{
if(n==1)
JOptionPane.showMessageDialog(null, "Fibonacci Serice is\n0 1"+s);
else
{
c=a+b;
a=b;
b=c;
s=s+c+" ";
fibonacci(n-1);
}
}
}// class Fib
class Second
{
public static void main(String args[])
{
Fib f=new Fib();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the num value...");
int num=sc.nextInt();
f.fibonacci(num);
}
}
OUTPUT:

4
/* 3. Write a java program that prompts the user for an integer and then prints out all prime numbers up to that integer */
import java.util.*;
public class ThirdDemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter up to which number");
int n=sc.nextInt();
int count=0,i=1,j,c;
System.out.println("The prime numbers up to "+n+" numbers");
while(i<=n)
{
j=1;
c=0;
while(j<=i)
{
if(i%j==0)
c++;
j++;
}
if(c==2)
{
System.out.print(" "+i);
count++;
}
i++;
}
System.out.println("\nTotal count:"+count);
}
}
/* OUTPUT:
C:\>javac ThirdDemo.java

C:\>java ThirdDemo
Enter up to which number
20
The prime numbers up to 20 numbers
2 3 5 7 11 13 17 19
Total count:8
*/

5
/*4. Write a java program that checks whethera given string is palindrome or not. Ex:MADAM is a palindrome. */

import java.util.*;
public class Fourth
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string...");
String str=sc.nextLine();
StringBuffer sb=new StringBuffer(str);
String s=new String(sb.reverse());
if(str.compareTo(s)==0)
System.out.println(str+" is Palindrome");
else
System.out.println(str+" is not Polindrome");
}
}
/* OUTPUT:
C:\>javac Fourth.java

C:\>java Fourth
Enter a string...
MADAM
MADAM is Palindrome

C:\>java Fourth
Enter a string...
RAMA
RAMA is not Polindrome

C:\>java Fourth
Enter a string...
LIRIL
LIRIL is Palindrome
*/

6
/*5. Write a java program for sorting a given list of names passed from the command line in ascending order */

public class Fifth


{
public static void main(String args[])
{
int n=args.length;
String temp;
for(int i=0; i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(args[j].compareTo(args[i])<0)
{
temp=args[i];
args[i]=args[j];
args[j]=temp;
}
}
}
System.out.println("After sorting the names are...");
for(int i=0;i<n;i++)
System.out.println(args[i]);
}
}
/* OUTPUT:

C:\>javac Fifth.java

C:\>java Fifth ashok ramesk raju narsimha nagesh srinivas


After sorting the names are...
ashok
nagesh
narsimha
raju
ramesk
srinivas
*/

7
/*6.Write a java application to multiply two Matrices */
import java.util.*;
public class SixthDemo
{
public static void main(String args[])
{
int a[][]=new int[3][3],b[][]=new int[3][3],c[][]=new int[3][3],i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("Enter mat A(3*3) elements");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=sc.nextInt();
System.out.println("Enter mat b(3*3) elements");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=sc.nextInt();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
System.out.println("The Result matrix C(3*3) is...");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.print("\n");
}
}
}
/* OUTPUT:
C:\>javac SixthDemo.java
C:\>java SixthDemo
Enter mat A(3*3) elements
123
456
789
Enter mat b(3*3) elements
100
010
001
The Result matrix C(3*3) is...
123
456
7 8 9*/

8
/* 7.Write a java application that reads a line of integers, and then display each integers and the sum of all integers (Using
StringTokenizer class) */

import java.util.*;
public class SeventhDemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbers....");
String str=sc.nextLine();
StringTokenizer st=new StringTokenizer(str);
int sum=0,i;
System.out.println("The Integers are......");
while(st.hasMoreTokens())
{
i=Integer.parseInt(st.nextToken());
sum+=i;
System.out.println(" "+i);
}
System.out.println("The sum:"+sum);
}
}
/* OUTPUT:
C:\>javac SeventhDemo.java

C:\>java SeventhDemo
Enter the numbers....
12425735
The Integers are......
1
2
4
2
5
7
3
5
The sum:29
*/

9
/*8. Write a java program that reads file and displays information about whether the file is existing
,readable,writable,type of file and length of file in bytes */
import java.io.*;
import java.util.*;
class Prog8
{
public static void main(String arg[])throws Exception
{
System.out.println("Enter File");
String nam=(new Scanner(System.in)).nextLine();
File f=new File(nam);
System.out.println(nam+" is"+((f.exists())?" ":" not ")+"Exists");
System.out.println(nam+" is"+((f.canRead())?" ":" not ")+"Read Only");
System.out.println(nam+" is"+((f.canWrite())?" ":" not ")+"Writable");
System.out.println(nam+" length is "+f.length());
}
}
/*
OUTPUT:
C:\>javac Prog8.java
C:\>java Prog8
Enter File
prog8.class
prog8.class is Exists
prog8.class is Read Only
prog8.class is Writable
prog8.class length is 1192
*/

10
/*9.Write a java program that reads file and displays file on screen with Line number before each line */

import java.io.*;
import java.util.*;
class Prog9
{
public static void main(String arg[])throws Exception
{
System.out.println("Enter File");
String nam=(new Scanner(System.in)).nextLine();
Scanner f=(new Scanner(new FileReader(nam)));
int i=1;
while(f.hasNext())
{
nam=f.nextLine();
System.out.println(i+" "+nam);
i++;
}
}
}
/*
OUTPUT:
C:\Lab>java Prog9
Enter File
hai.txt
1 india is my country
2 all indians are my brothres
3 hello world
4 my first program
5 dassdfdf
6 fjkhk hjyuui
*/

11
/*10.Write a java program that text reads file and displays number of characters ,lines and words */

import java.io.*;
import java.util.*;
class Prog10
{
public static void main(String arg[])throws Exception
{
int w=0,c=0,l=0,j;
System.out.println("Enter File");
String nam=(new Scanner(System.in)).nextLine();
Scanner f=(new Scanner(new FileReader(nam)));
while(f.hasNext())
{
nam=f.nextLine();
for(j=0;j<nam.length();j++)
{
if(nam.charAt(j)==' ')
w++;
c++;
}
l++;
}
System.out.println("no of characters"+c);
System.out.println("no of words"+(w+l));
System.out.println("no of lines"+l);
}
}
/*
OUTPUT:
C:\Lab>javac prog10.java
C:\Lab>java Prog10
Enter File
prog10.java
no of characters511
no of words48
no of lines26
*/

12
/*11.a Write a java program that implements Stack ADT */

import java.util.*;
class SDemo
{
int stack[]=new int[40],top=-1;
public void push(int n)
{
if(top==stack.length)
System.out.println("Stack is full insertion is not possiable...");
else
{
top++;
stack[top]=n;
System.out.println(n+" is inserted");
}

}
public void pop()
{
if(top==-1)
System.out.println("Stack is empty deletion is not possiable..");
else
{
System.out.println("Deleted element is:"+stack[top]);
top--;
}
}
public void display()
{
System.out.println("********Stack content is***********");
for(int i=0;i<=top;i++)
System.out.print(stack[i]+" ");
System.out.print("\n");
}

}
class StackDemo
{
public static void main(String args[])
{
SDemo obj=new SDemo();
int item,choice;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("Enter your choice\n1.push\t2.pop\t3.print\t4.exit");
choice=sc.nextInt();
switch(choice)
{
case 1: System.out.println("Enter data to insert");

13
item=sc.nextInt();
obj.push(item);
break;
case 2: obj.pop();
break;
case 3: obj.display();
break;
case 4: System.out.println("Program is exited...");
System.exit(0);
break;
default: System.out.println("Your choice is wrong.......!");
break;
}
}
while (choice<5);
}
}
/* OUTPUT:

C:\>javac StackDemo.java

C:\>java StackDemo Enter your choice


Enter your choice 1.push 2.pop 3.print 4.exit
1.push 2.pop 3.print 4.exit 3
1 ********Stack content is***********
Enter data to insert 20 30 40
20 Enter your choice
20 is inserted 1.push 2.pop 3.print 4.exit
Enter your choice 2
1.push 2.pop 3.print 4.exit Deleted element is:40
1 Enter your choice
Enter data to insert 1.push 2.pop 3.print 4.exit
30 1
30 is inserted Enter data to insert
Enter your choice 50
1.push 2.pop 3.print 4.exit 50 is inserted
1 Enter your choice
Enter data to insert 1.push 2.pop 3.print 4.exit
40 3
40 is inserted ********Stack content is***********
20 30 50
Enter your choice
1.push 2.pop 3.print 4.exit
4
Program is exited...
*/

14
/* 11.b Write a java program to converts infix expression into Postfix form */
import java.util.*;
class Exp
{
public static void main(String[] args) throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.println("enter infix exception");
String a=sc.nextLine();
a=new String(new StringBuffer(a).append(')'));
push('(');

for(int i=0;i<a.length();i++)
{
char c=a.charAt(i);

if(Character.isLetter(c))
op[++k]=c;
else if(c=='(')
push('(');
else if(c==')')
{
while(b[j]!='(')
op[++k]=pop();
j--;
}
else if(p(c)>p(b[j]))
push(c);
else
{
while(p(c)<=p(b[j]) && j>0)
op[++k]=pop();
push(c);
}
System.out.println(c+" "+new String(b)+new String(op));

}
System.out.println("postfix exp= "+new String(op));
}
static char b[]=new char[30];
static char op[]=new char[30];
static int j=-1,k=-1;
static char pop()
{
return(b[j--]);
}
static void push(char x)
{
b[++j]=x;
}
static int p(char x)

15
{
switch(x)
{
case'+':case'-':return 1;
case'*':case'/':return 2;
case'^':return 3;
}
return 0;
}
}

/* output:
C:\LAB>javac Exp.java

C:\LAB>java Exp
enter infix exception
a+b*c^d-e*f/h
a ( a
+ (+ a
b (+ ab
* (+* ab
c (+* abc
^ (+*^ abc
d (+*^ abcd
- (-*^ abcd^*+
e (-*^ abcd^*+e
* (-*^ abcd^*+e
f (-*^ abcd^*+ef
/ (-/^ abcd^*+ef*
h (-/^ abcd^*+ef*h
) (-/^ abcd^*+ef*h/-
postfix exp= abcd^*+ef*h/-
*/

16
/*12. Write an applet that displays a simple message */

public class Program12 extends java.applet.Applet


{
String str="";
public void init()
{
str+="init\t";
System.out.println(str);
}
public void start()
{
str+="start\t";
System.out.println(str);
}
public void paint(java.awt.Graphics g)
{
str+="paint\t";
System.out.println(str);
}
public void stop()
{
str+="stop\t";
System.out.println(str);
}
public void destroy()
{
str+="desyroy\t";
System.out.println(str);
}
}
/* <applet code="Program12.class" width=400 height=400>
</applet>
*/
/* OUTPUT:

C:\>javac Program12.java

C:\>appletviewer Program12.java
init
init start
init start paint
init start paint paint
init start paint paint paint
init start paint paint paint paint
init start paint paint paint paint paint
init start paint paint paint paint paint paint
init start paint paint paint paint paint paint stop
init start paint paint paint paint paint paint stop start
init start paint paint paint paint paint paint paint stop destroy */

17
/*13. write a applet that computes loan payment based on the amount of the loan, the interest rate and the number of
months*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Program13 extends Applet implements ActionListener
{
TextField t1,t2,t3,t4;
Label l1,l2,l3,l4;
Button b;
int m;
float l,i,r;
public void init()
{}
public void start()
{
t1=new TextField(20);
l1=new Label("Loan Amount:");
add(l1);
add(t1);
t2=new TextField(20);
l2=new Label("Interest:");
add(l2);
add(t2);
t3=new TextField(20);
l3=new Label("No.Months:");
add(l3);
add(t3);
b=new Button("Result");
add(b);
b.addActionListener(this);
t4=new TextField(20);
l4=new Label("Total Loan Amount:");
add(l4);
add(t4);
}
public void actionPerformed(ActionEvent ae)
{
if(t1.getText().equals(" "))
JOptionPane.showMessageDialog(null,"Enter amount of loan");
else
l=Float.parseFloat(t1.getText());

if(t2.getText().equals(" "))
JOptionPane.showMessageDialog(null,"Enter amount of interest");
else
i=Float.parseFloat(t2.getText());

if(t3.getText().equals(" "))
m=12;

18
else
m=Integer.parseInt(t3.getText());

r=l+(l*m*i)/100;
t4.setText(r+" ");
}
}
/* <applet code="Program13.class" width=300 height=400>
</applet>
*/

OUTPUT:

19
//14. Java application for a simple calculator

import java.awt.*;
import java.awt.event.*;
class Calc implements ActionListener
{
TextField tf;
String a="",b="";
float r;
char op=' ';
Calc()
{
Frame f=new Frame("Calc");
f.setLayout(null);
Panel p=new Panel();
p.setBounds(20,90,200,200);
p.setSize(200,200);
p.setLayout(new GridLayout(4,4));
Button b[]=new Button[10],opb[]=new Button[6];
opb[0]=new Button("+");
opb[1]=new Button("-");
opb[2]=new Button("*");
opb[3]=new Button("/");
opb[4]=new Button("=");
opb[5]=new Button("c");
tf=new TextField("0");
tf.setBounds(20,50,200,30);
p.setVisible(true);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
p.add(b[i]);
b[i].addActionListener(this);
}
for(int i=0;i<6;i++)
{
p.add(opb[i]);
opb[i].addActionListener(this);
}
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
System.exit(0);
}}
);
f.add(p);
f.add(tf);
f.setSize(250,300);
f.setResizable(false);
f.setBackground(new Color(200,110,110));
f.setVisible(true);

20
}
public void actionPerformed(ActionEvent ae)
{
char c=(ae.getActionCommand()).charAt(0);
switch(c)
{
case '=':
switch(op)
{
case '+':r=Float.parseFloat(a)+Float.parseFloat(b);op=' ';
break;
case '-':r=Float.parseFloat(a)-Float.parseFloat(b);op=' ';
break;
case '*':r=Float.parseFloat(a)*Float.parseFloat(b);op=' ';
break;
case '/':r=Float.parseFloat(a)/Float.parseFloat(b);op=' ';
break;
case ' ':r=Float.parseFloat(a)+r;break;
}
tf.setText(r+"");
a=b="0";
break;
case '+':case '-':case '/':case '*':
op=c;
if(Integer.parseInt(a)==0) a=r+"";
break;
case 'c':a=b="0";r=0;
tf.setText("0");
break;
default: if(op==' ')
{
a+=c;
if(Integer.parseInt(a)==0)
a="0";
tf.setText(a);
}
else
{
b+=c;
if(Integer.parseInt(b)==0)
b="0";
tf.setText(b);
}
}
}

public static void main(String args[])


{
new Calc();
}
}

21
OUTPUT:

22
/*15. Write a java program for handling mouse events */

import java.awt.*;
import java.awt.event.*;
class MouseDemo extends Frame
{
TextField tf;
int x,y;
MouseDemo()
{
setLayout(new FlowLayout(FlowLayout.CENTER));
tf=new TextField(30);
setSize(400,400);
setLocation(10,10);
setTitle("MouseHandling Frame");
add(tf);
setVisible(true);
Listener obj=new Listener();
addMouseListener(obj);
addMouseMotionListener(obj);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
}
class Listener implements MouseListener,MouseMotionListener
{
public void mouseEntered(MouseEvent me)
{
x=me.getX(); y=me.getY();
tf.setText("Mouse Entered at:["+x+","+y+"]");
}
public void mouseExited(MouseEvent me)
{
x=me.getX(); y=me.getY();
tf.setText("Mouse Exiated at:["+x+","+y+"]");
}
public void mouseClicked(MouseEvent me)
{
x=me.getX(); y=me.getY();
tf.setText("Mouse Clicked at:["+x+","+y+"]");
}
public void mousePressed(MouseEvent me)
{
x=me.getX(); y=me.getY();
tf.setText("Mouse Pressed at:["+x+","+y+"]");
}
public void mouseReleased(MouseEvent me)
{

23
x=me.getX(); y=me.getY();
tf.setText("Mouse Released at:["+x+","+y+"]");
}
/* MouseMotionListener interface methods */
public void mouseMoved(MouseEvent me)
{
x=me.getX(); y=me.getY();
tf.setText("Mouse Moved at:["+x+","+y+"]");
}
public void mouseDragged(MouseEvent me)
{
x=me.getX(); y=me.getY();
tf.setText("Mouse Dragged at:["+x+","+y+"]");
}
}

public static void main(String args[])


{
new MouseDemo();
}
}

OUTPUT:
C:\>javac MouseDemo.java
C:\>java MouseDemo

24
/*16. Write a java program creating multiple Threads */
import java.io.PrintWriter;
class MultiThreadDemo
{
public static void main(String args[])
{
new Thread(new T1()).start();
new Thread(new T2()).start();
for(int j=100;j>=95;j--)
{
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
}
System.out.println("main Thread:"+j);
}
}
}
class T1 implements Runnable
{
PrintWriter ou=new PrintWriter(System.out,true);
public void run()
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(500);
}
catch (Exception e)
{
}
ou.println("Thread a:"+i);
}
}
}
class T2 implements Runnable
{
PrintWriter ou=new PrintWriter(System.out,true);
public void run()
{
for(char i='a';i<'f';i++)
{
try
{
Thread.sleep(500);
}
catch (Exception e)

25
{
}
ou.println("Thread b:"+i);
}
}
}
/* OUTPUT:
C:\>javac MultiThreaddemo.java
C:\>java MultiThreadDemo
Thread a:0
Thread b:a
main Thread:100
Thread a:1
Thread b:b
Thread a:2
Thread b:c
main Thread:99
Thread a:3
Thread b:d
Thread a:4
Thread b:e
main Thread:98
main Thread:97
main Thread:96
main Thread:95
*/

26
/*17. Producer Consumer Problem using Inter-Thread Communication */
import java.util.*;
class Q
{
int q;
boolean f;
synchronized public void put( int x)
{
try{
if(f==true)
wait();
q=x;
System.out.println("produced is"+q);
f=true;
notify();
}catch(Exception e){}
}
synchronized public void get( )
{
try{
if(f==false)
wait();
System.out.println("consumed is"+q);
f=false;
notify();
}catch(Exception e){}
}
}
class P extends Thread
{
Q q;
P(Q x)
{
q=x;
}
public void run()
{
int i=0;
while(true)
{
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
}
q.put(i);
i++;
}
}

27
}
class C extends Thread
{
Q q;
C(Q x)
{
q=x;
}
public void run()
{
while(true)
{
q.get();
}
}
}
class PC
{
Q q;
PC()
{
q=new Q();
new Thread(new P(q)).start();
new Thread(new C(q)).start();
}
public static void main(String args[])
{
System.out.println("Press Enter to exit");
new PC();
Thread t=new Thread(new Zz());
t.setDaemon(true);
t.start();

}
}
class Zz extends Thread
{
public void run()
{
String s=new Scanner(System.in).nextLine();
if(s!=null)
System.exit(0);
} consumed is1
} produced is2
/*OUTPUT: consumed is2
C:\Lab>javac PC.java produced is3
C:\Lab>java PC consumed is3
Press Enter to exit produced is4
produced is0 consumed is4
consumed is0 produced is5
produced is1 consumed is5
*/
28
/* 18.Write a java program that lets user to create pie charts.*/
import java.awt.*;
import java.awt.event.*;
class arc extends Frame implements ActionListener
{
Color c[]={new Color(0,255,255),new Color(255,0,255),new Color(255,255,0),new Color(0,0,255),new
Color(0,255,0),new Color(255,0,0)};
double m[]=new double[6],s[]=new double[6],
e[]=new double[6];
double an[]=new double[6];
Label lb[]=new Label[6];
TextField t[]=new TextField[6];
Button b=new Button("submit");
arc()
{
setLayout(new FlowLayout(FlowLayout.CENTER));
setSize(1000,800);
setLocation(20,20);
for(int i=0;i<6;i++)
{

t[i]=new TextField(9);
int j=i+1;
lb[i]=new Label("Subject"+j);
add(lb[i]);
add(t[i]);
}
b.setBounds(70,70,60,40);
add(b);
setResizable(false);
setVisible(true);
b.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
double sm=0;
for(int i=0;i<6;i++)
{
m[i]=Double.parseDouble(t[i].getText());
sm+=m[i];
}
for(int i=0;i<6;i++)
an[i]=m[i]/sm*360.0;

29
s[0]=0;e[0]=(int)an[0];
for(int i=1;i<6;i++)
{
s[i]=e[i-1];
e[i]=s[i]+an[i];
}
repaint();
}
public void paint(Graphics g)
{
setBackground(Color.black);
for(int i=0;i<6;i++)
{
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{ }
g.setColor(c[i]);
g.fillArc(240,120,500,500,(int)s[i],(int)an[i]);
}
}
public static void main(String args[]) throws Exception
{
new arc();
}
}
OUTPUT:

30
/* 19. Write a java program that allows the user to draw lines, rectangles and ovals */

import java.applet.Applet;
import java.awt.Graphics;
public class Prog19 extends Applet
{
public void init(){ }
public void start() { }
public void paint(Graphics g)
{
g.drawLine(10,10,210,10);
g.drawRect(20,20, 180,180);
g.drawOval(40,40,130,140);
g.drawOval(220,220,70,50);
}
public void stop() { }
public void destroy() { }
}
/*
<applet code="Prog19.class" width=400 height=400 >
</applet>
*/
OUTPUT:

31
//20. Client program to send radius to server

import java.io.*;
import java.util.*;
import java.net.*;
public class Client
{
public static void main(String args[]) throws IOException
{
Socket s=new Socket(InetAddress.getLocalHost(),8000);
Scanner sc=new Scanner(System.in);
Scanner scr=new Scanner(s.getInputStream());
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
while(true)
{
System.out.println("Enter radius of circle:");
double r=sc.nextDouble();
System.out.println("Radius sent to client is:"+r);
out.println(r);
System.out.println("Ariea is recieved..");
System.out.println("Area is:"+scr.nextDouble());
}
}
}

// server program

import java.io.*;
import java.util.*;
import java.net.*;
public class Server
{
public static void main(String args[]) throws IOException
{
System.out.println("Server is started");
ServerSocket ss=new ServerSocket(8000);
Socket s=ss.accept();
System.out.println("Connection from***: "+s+" *** ");
Scanner in=new Scanner(s.getInputStream());
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
while(true)
{
double r=in.nextDouble();
System.out.println("Radius recived from client is:"+r);
double a=Math.PI*r*r;
pw.println(a);
System.out.println("Area is sent to client...:"+a);
}

32
}
}

OUTPUT:

33
/*21. Write a java program that illustrates how run time polymorphism is achieved */

class A
{
public void show()
{
System.out.println("Inside class A and method show");
}
}
class B extends A
{
public void show()
{
System.out.println("Inside class B and method show");
}
}
class C extends B
{
public void show()
{
System.out.println("Inside class C and method show");
}
}
class RunPoly
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
a.show();
b.show();
c.show();
A ref;
ref=b;
ref.show();
ref=c;
ref.show();
}
}
/* OUTPUT:
C:\>javac RunPoly.java
C:\>java RunPoly
Inside class A and method show
Inside class B and method show
Inside class C and method show
Inside class B and method show
Inside class C and method show
*/

34

Você também pode gostar