Object Oriented Programming MANUAL

LAB

BY JUVERIAH SUMAN II BTECH CSE

WEEK:1A
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. AIM: To print all real solutions to the quadratic equation ax2+bx+c=0 PROGRAM: import java.io.*; class quadratic { public static void main(String args[])throws IOException { double a,b,c,d,r1,r2; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a,b,c values"); a=Double.parseDouble(br.readLine()); b=Double.parseDouble(br.readLine()); c=Double.parseDouble(br.readLine()); d=(b*b)-(4*a*c); if(d==0) { System.out.println("Roots are real and equal "); r1=r2=-b/(2*a); System.out.println("The real solutions are "+r1+" and "+r2); } else if(d>0) { System.out.println("Roots are real and distinct"); r1=(-b+Math.sqrt(d))/(2*a); r2=(-b+Math.sqrt(d))/(2*a); System.out.println("The real solutions are "+r1+" and "+r2); } else System.out.println("There are no real solutions"); } } OUTPUT:

Enter a,b,c values: 1 -7 12 Roots are real and distinct The real solutions are 4.0 and 3.0

WEEK:1B
The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence are 1, 1. Every subsequent value is the sum of the 2 values preceding it.Write a Java program that uses both recursive and non-recursive functions to print the nth value of the Fibonacci sequence. AIM: To print the nth value of the Fibonacci sequence using both recursive and non-recursive functions PROGRAM: import java.io.*; class fibo { static void nonrec(int x,int f1,int f2) { int i=0,f3; while(i<x-2) { f3=f1+f2; System.out.print(“\t”+f3); f1=f2; f2=f3; i++; } } static int rec(int m) { if(m<=2) return 1; else return rec(m-1)+rec(m-2); } public static void main(String arg[])throws IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a=1,b=1,n,r; System.out.println(”Enter the value of n:”); n=Integer.parseInt(br.readLine()); System.out.println(”Fibonacci series using nonrecursive function”); System.out.print(a+”\t”+b); nonrec(n,a,b); System.out.println(”Fibonacci series using recursive function”); for(int i=1;i<=n;i++) { r=rec(i); System.out.print(r+”\t”); } } } OUTPUT: Enter the value of n:5 Fibonacci series using non-recursive function 1 1 2 3 5 Fibonacci series using recursive function 1 1 2 3 5

WEEK:2A
Write a Java program that prompts the user for an integer and then prints out all the prime numbers up to that Integer. AIM: To print all the prime numbers up to a given Integer PROGRAM: import java.io.*; class prime { public static void main(String args[])throws IOException { int i,j,n,count; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the limit(n):"); n=Integer.parseInt(br.readLine()); System.out.println("The prime numbers are"); for(i=1;i<=n;i++) {

count=0; for(j=1;j<=n;j++) { if(i%j==0) count++; } if(count==2) System.out.print(”\t”+i); } } } OUTPUT: Enter the limit(n): 5 The prime numbers are 2 3 5

WEEK:2B
Write a Java program to multiply two given matrices AIM: To multiply two given matrices and print the result PROGRAM: import java.io.*; class Matrix { public static void main(String arg[])throws IOException { int i,j,k,r1,r2,c1,c2; int a[ ][ ]=new int[10][10]; int b[ ][ ]=new int[10][10]; int c[ ][ ]=new int[10][10]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter no.of rows and columns for matrix a:"); r1=Integer.parseInt(br.readLine()); c1=Integer.parseInt(br.readLine()); System.out.println("Enter no.of rows and columns for matrix b:"); r2=Integer.parseInt(br.readLine()); c2=Integer.parseInt(br.readLine()); System.out.println("Enter elements of matrix a:"); for(i=0;i<r1;i++)

{ for(j=0;j<c1;j++) { a[i][j]=Integer.parseInt(br.readLine()); } } System.out.println("Enter elements of matrix b:"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { b[i][j]=Integer.parseInt(br.readLine()); } } if(c1==r2) { System.out.println(”Matrix multiplication is possible”); System.out.println(”Resultant matrix is”); System.out.println("Enter elements of matrix a:"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<r2;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { System.out.print(” ”+c[i][j]); } System.out.println(); } } else System.out.println(”Matrix multiplication is not possible”); } } OUTPUT: Enter no.of rows and columns for matrix a: 2 2

Enter no.of rows and columns for matrix b: 2 2 Enter elements of matrix a: 2 5 3 9 Enter elements of matrix b: 3 1 8 4 Matrix multiplication is possible Resultant matrix is 46 32 81 39

WEEK:2C
Write a java program that reads a line of integers and then displays each integer and sum of all integers AIM: To read a line of integers and then display each integer and sum of all integers PROGRAM: import java.io.*; class list { public static void main(String args[])throws IOException { int a[ ]=new int[10]; int i,n,s=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter size of array:"); n=Integer.parseInt(br.readLine()); System.out.println("Enter values:"); for(i=0;i<n;i++) a[i]=Integer.parseInt(br.readLine()); System.out.println(”Elements of array are”); for(i=0;i<n;i++) System.out.println(" "+a[i]); for(i=0;i<n;i++) s=s+a[i]; System.out.println(" Sum of all integers is "+s); } } OUTPUT:

Enter size of array: 4 Enter values: 5 9 10 13 Elements of array are 5 9 10 13 Sum of all integers is 37

WEEK:3A
Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome AIM: To check whether a given string is a palindrome or not PROGRAM: import java.io.*; import java.lang.*; class palex { public static void main(String ar[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s; System.out.println("Enter a string:"); s=br.readLine(); StringBuffer sb=new StringBuffer(s); sb.reverse(); String s1=sb.toString(); if(s.equals(s1)) System.out.println("Entered string is Palindrome"); else System.out.println("Entered string is not Palindrome"); } } OUTPUT: Enter a string: mom Entered string is Palindrome

WEEK:3B
Write a Java program for sorting a given list of names in ascending order. AIM: To sort a given list of names in ascending order PROGRAM: import java.io.*; class sortex { public static void main(String arg[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str[]=new String[5]; System.out.println("Enter list of names"); for(int i=0;i<5;i++) { str[i]=br.readLine(); } System.out.println("The list of names is:"); for(int i=0;i<5;i++) { System.out.println(str[i]); } int s=str.length; System.out.println("List of names in ascending order:"); for(int i=0;i<s;i++) { for(int j=i+1;j<s;j++) { if((str[j].compareTo(str[i]))<0) { String t=str[i]; str[i]=str[j]; str[j]=t; } } System.out.println(str[i]); } } }

OUTPUT: Enter list of names deepthi jayanthi chandrika juveriah fareesa The list of names is: deepthi jayanthi chandrika juveriah fareesa List of names in ascending order: chandrika deepthi fareesa jayanthi juveriah

WEEK:3C
Write a java program to make frequency count of words in a given text. AIM: To make frequency count of words in a given text PROGRAM: import java.util.*; import java.lang.*; import java.io.*; class freq { public static void main(String[ ] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the text:"); String str=br.readLine(); String temp; StringTokenizer st=new StringTokenizer(str); int n=st.countTokens( ); String a[ ]=new String[n]; int i=0,count=0; while(st.hasMoreTokens())

{ a[i]=st.nextToken(); i++; } for(int x=1;x<n;x++) { for(int y=0;y<n-x;y++) { if(a[y].compareTo(a[y+1])>0) { temp=a[y]; a[y]=a[y+1]; a[y+1]=temp; } } } System.out.println("The frequency count of words is"); for(i=0;i<n;i++) { count=1; if(i<n-1) { while(a[i].compareTo(a[i+1])==0) { count++; i++; if(i>=n-1) { System.out.println(a[i]+" "+count); System.exit(0); } } } System.out.println(a[i]+" "+count); } } } OUTPUT: Enter the text: hi hello hi hi The frequency count of words is hello 1 hi 3

WEEK:4A Write a java program that reads a filename from user then displays information from user then displays information about whether the file exists,whether file is readable, whether file is writable the type of file and length of file in bytes. AIM: To read a filename from user then displays information from user then displays information about whether the file exists,whether file is readable, whether file is writable the type of file and length of file in bytes PROGRAM: import java.io.*; class FileDemo { static void p(String s) { System.out.println(s); } public static void main(String args[ ])throws IOException { File f1 = new File(args[0]); p("File Name: " + f1.getName()); p("Path: " + f1.getPath()); p("Abs Path: " + f1.getAbsolutePath()); p("Parent: " + f1.getParent()); p(f1.exists() ? "exists" : "does not exist"); p(f1.canWrite() ? "is writeable" : "is not writeable"); p(f1.canRead() ? "is readable" : "is not readable"); p("is " + (f1.isDirectory() ? "" : "not" + " a directory")); p(f1.isFile() ? "is normal file" : "might be a named pipe"); p(f1.isAbsolute() ? "is absolute" : "is not absolute"); p("File last modified: " + f1.lastModified()); p("File size: " + f1.length() + " Bytes"); } } OUTPUT: D:\juveriah>java filex /juveriah/prime.java Filename: prime.java Path: \juveriah\prime.java Abs Path: D:\juveriah\prime.java

Parent: \juveriah Exists Is writable Is readable Is not a directory Is normal file Is not absolute File last modified: 1265263536000 File size: 493 bytes WEEK:4B Write a java program that reads a file and displays the file on the screen with a line number before each line. AIM: To read a file and display the file on the screen with a line number before each line. PROGRAM: import java.util.*; import java.io.*; import java.lang.*; class FileNo { public static void main(String arg[ ])throws IOException { int linenum=0; String line; try { File f=new File(arg[0] "); if(f.exists()) { Scanner infile=new Scanner(f); while(infile.hasNext()) { line=infile.nextLine(); System.out.println(++linenum+" "+line); } infile.close(); } } catch(Exception e) {

System.out.println(e); } } } OUTPUT: D:\juveriah>java FileNo myfile.txt 1 Java features: 2 simple 3 secure 4 portable 5 object-oriented WEEK:4C Write a java program that displays the number of characters,lines and words in a text file AIM: To display the number of characters,lines and words in a text file PROGRAM: import java.io.*; class word { public static void main(String arg[])throws IOException { int lines=0,words=0,chars=0; if(arg.length==0) { System.out.println("Give a file name in the command line"); System.exit(0); } File f=new File(arg[0]); int chr=0; FileInputStream fins=new FileInputStream(f); InputStreamReader insr=new InputStreamReader(fins); while((chr=insr.read()) != -1) { chars++; if((chr=='\t') || (chr=='')) words++; if(chr=='\n') { words++;

lines++; } } System.out.println(arg[0]+" File contains\n"+chars+" chars\n"+words+" words\n"+lines+" lines"); } } OUTPUT: D:\juveriah>java word features.txt features.txt file contains 59 chars 6 words 5 lines WEEK:5A Write a java program to implement stack ADT AIM: To implement stack ADT PROGRAM: import java.io.*; import java.util.Stack; import java.util.EmptyStackException; class stackex { public static void main(String arg[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int ch,top=-1,n; Stack stk=new Stack(); do { System.out.println("MENU"); System.out.print("1.push\t2.pop\t3.display\t4.exit\n"); System.out.print("Enter ur choice:"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1:System.out.print("Enter the element to be pushed:"); n=Integer.parseInt(br.readLine()); stk.push(n); top++;

break; case 2:if(top==-1) System.out.println("Stack is empty"); else { System.out.print("Element popped is:"); System.out.println(stk.pop()); top--; } break; case 3:if(top==-1) System.out.println("Stack is empty"); else { System.out.println("The elements in the stack are "); for(int i=top;i>=0;i--) System.out.println(stk.pop()); } break; case 4:System.exit(0); } } while(ch!=4); } } OUTPUT: MENU 1.push 2.pop 3.display 4.exit Enter ur choice:1 Enter the element to be pushed:5 MENU 1.push 2.pop 3.display 4.exit Enter ur choice:1 Enter the element to be pushed:9 MENU 1.push 2.pop 3.display 4.exit Enter ur choice:1 Enter the element to be pushed:22 MENU 1.push 2.pop 3.display 4.exit Enter ur choice:2 Element popped is:22 MENU 1.push 2.pop 3.display 4.exit

Enter ur choice:3 The elements in the stack are 9 5 MENU 1.push 2.pop 3.display 4.exit Enter ur choice:4

WEEK:5B
Write a java program that converts infix expression into postfix form AIM: To convert infix expression into postfix form PROGRAM: import java.lang.*; import java.io.*; import java.util.*; class IntoPo { public static void main(String arg[])throws IOException { Stack stk=new Stack(); int top=-1; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); char input; String output=" "; System.out.println("Enter infix expresion:"); String exp=br.readLine(); for(int pos=0;pos<exp.length();pos++) { input=exp.charAt(pos); switch(input) { case '+' : case '-' : case '*' : case '/' : stk.push(input); top++; break; default : output=output+input; break; } }

for(int i=top;i>=0;i--) { char ch=((Character)stk.pop()); output=output+ch; } System.out.println("Postfix form is "+output); } } OUTPUT: Enter infix expresion: 1+2*4/5-7+3/6 Postfix form is 1245736/+-/*+

WEEK:6A
Develop an applet that displays a simple message AIM: To develop an applet that displays a simple message PROGRAM: import java.awt.*; import java.applet.*; /*<applet code="helloapplet" width=800 height=800></applet>*/ public class helloapplet extends Applet { public void paint(Graphics g) { Font f=new Font("Arial",Font.BOLD,36); g.setFont(f); g.drawString("Hiiii!!!! Hello:-) Oops!!!!",200,300); } } OUTPUT:

WEEK:6B
Develop an applet that receives an integer in one text field, and computes as factorial value and returns it in another text field,when the button named “Compute” is clicked AIM: To develop an applet that receives an integer in one text field, and computes as factorial value and returns it in another text field,when the button named “Compute” is clicked PROGRAM: import java.awt.*; import java.applet.*; import java.awt.event.*;

public class fac extends Applet implements ActionListener { Label l1,l2; TextField t1,t2; Button b; String msg=""; public void init() { l1=new Label("Enter number"); b=new Button("Compute"); l2=new Label("Factorial"); t1=new TextField(20); t2=new TextField(20); add(l1); add(t1); add(b); b.addActionListener(this); add(l2); add(t2); } public void actionPerformed(ActionEvent ae) { int n,fact=1; n=Integer.parseInt(t1.getText()); for(int i=n;i>0;i--) fact*=i; msg=""+fact; t2.setText(msg); } } /*<applet code="fac" width=800 height=800></applet>*/ OUTPUT:

WEEK:7
Write a java program that works as a simple calculator.Use a grid layout to arrange buttons for the digits +,-,*,/,% operations.Adda text field to display the result. AIM: To develop a simple calculator PROGRAM: import java.awt.*; import java.applet.*; import java.awt.event.*; public class Calculator extends Applet implements ActionListener { TextField t1; String a="",b; String oper="",s="",p=""; int first=0,second=0,result=0; Panel p1;

Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; Button add,sub,mul,div,mod,res,space; public void init() { Panel p2,p3; p1=new Panel(); p2=new Panel(); p3=new Panel(); t1=new TextField(a,20); p1.setLayout(new BorderLayout()); p2.add(t1); b0=new Button("0"); b1=new Button("1"); b2=new Button("2"); b3=new Button("3"); b4=new Button("4"); b5=new Button("5"); b6=new Button("6"); b7=new Button("7"); b8=new Button("8"); b9=new Button("9"); add=new Button("+"); sub=new Button("-"); mul=new Button("*"); div=new Button("/"); mod=new Button("%"); res=new Button("="); space=new Button("c"); p3.setLayout(new GridLayout(4,4)); b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); add.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); mod.addActionListener(this); res.addActionListener(this); space.addActionListener(this);

p3.add(b0); p3.add(b1); p3.add(b2); p3.add(b3); p3.add(b4); p3.add(b5); p3.add(b6); p3.add(b7); p3.add(b8); p3.add(b9); p3.add(add); p3.add(sub); p3.add(mul); p3.add(div); p3.add(mod); p3.add(res); p3.add(space); p1.add(p2,BorderLayout.NORTH); p1.add(p3,BorderLayout.CENTER); add(p1); } public void actionPerformed(ActionEvent ae) { a=ae.getActionCommand(); if(a=="0"||a=="1"||a=="2"||a=="3"||a=="4"||a=="5"||a=="6"||a=="7"|| a=="8" ||a=="9") { t1.setText(t1.getText()+a); } if(a=="+"||a=="-"||a=="*"||a=="/"||a=="%") { first=Integer.parseInt(t1.getText()); oper=a; t1.setText(""); } if(a=="=") { if(oper=="+") result=first+Integer.parseInt(t1.getText()); if(oper=="-") result=first-Integer.parseInt(t1.getText()); if(oper=="*") result=first*Integer.parseInt(t1.getText()); if(oper=="/") result=first/Integer.parseInt(t1.getText()); if(oper=="%")

result=first%Integer.parseInt(t1.getText()); t1.setText(result+""); } if(a=="c") t1.setText(""); } } /*<applet code=Calculator width=200 height=200></applet>*/ OUTPUT:

WEEK:8
Write a java program for handling mouse events AIM: To handle mouse events PROGRAM:

import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="MouseEvents" width=300 height=100></applet>*/ public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int mouseX = 0, mouseY = 0; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent me) { mouseX = 0; mouseY = 10; msg = "Mouse clicked."; repaint(); } public void mouseEntered(MouseEvent me) { mouseX = 0; mouseY = 10; msg = "Mouse entered."; repaint(); } public void mouseExited(MouseEvent me) { mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint(); } public void mousePressed(MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); msg = "Mouse Pressed"; repaint(); } public void mouseReleased(MouseEvent me) { mouseX = me.getX(); mouseY = me.getY();

msg = "Mouse Released"; repaint(); } public void mouseDragged(MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); msg = "*"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); } public void mouseMoved(MouseEvent me) { showStatus("Moving mouse at " + me.getX() + ", " + me.getY()); } public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); } } OUTPUT:

WEEK:9A
Write a Java program that creates 3 threads by extending Thread class. First thread displays “Good Morning” every 1 sec, the second thread displays “Hello” every 2 seconds and the third displays “Welcome” every 3 seconds. AIM: To create 3 threads by extending Thread class. First thread displays “Good Morning” every 1 sec, the second thread displays “Hello” every 2 seconds and the third displays “Welcome” every 3 seconds. PROGRAM: class Frst implements Runnable { Thread t; Frst() { t=new Thread(this); System.out.println("Good Morning"); t.start(); } public void run() { for(int i=0;i<5;i++) { System.out.println("Good Morning "+i); try { t.sleep(1000); } catch(Exception e) { System.out.println(e); } } } } class sec implements Runnable { Thread t; sec() { t=new Thread(this); System.out.println("hello"); t.start();

} public void run() { for(int i=0;i<5;i++) { System.out.println("hello "+i); try { t.sleep(2000); } catch(Exception e) { System.out.println(e); } } } } class third implements Runnable { Thread t; third() { t=new Thread(this); System.out.println("welcome"); t.start(); } public void run() { for(int i=0;i<5;i++) { System.out.println("welcome "+i); try { t.sleep(3000); } catch(Exception e) { System.out.println(e); } } } } public class Multithread { public static void main(String arg[]) {

new Frst(); new sec(); new third(); } } OUTPUT: Good Morning hello welcome Good Morning hello 0 welcome 0 Good Morning hello 1 Good Morning Good Morning welcome 1 hello 2 Good Morning welcome 2 hello 3 hello 4 welcome 3 welcome 4

0 1 2 3 4

WEEK:9B
Write a java program that correctly implements producer consumer problem using the concept of inter thread communication AIM: To correctly implement producer consumer problem using the concept of inter thread communication PROGRAM: class Q { int n; boolean flag = false; synchronized int get() { if (!flag) try {

wait(); } catch (InterruptedException e) { } System.out.println("Got: " + n); flag = false; notify(); return n; } synchronized void put(int n) { if (flag) try { wait(); } catch (InterruptedException e) { } this.n = n; flag = true; System.out.println("Put: " + n); notify(); } } class Producer implements Runnable { Q q; Producer(Q q) { this.q = q; new Thread(this, "Producer").start(); } public void run() { int i = 0; while(i<10) { q.put(i++); } } } class Consumer implements Runnable { Q q; Consumer(Q q) {

this.q = q; new Thread(this, "Consumer").start(); } public void run() { int k=0; while(k<10) { q.get(); k++; } } } class PC { public static void main (String args[ ]) { Q q = new Q(); new Producer(q); new Consumer(q); } } OUTPUT: Put: 0 Got: 0 Put: 1 Got: 1 Put: 2 Got: 2 Put: 3 Got: 3 Put: 4 Got: 4 Put: 5 Got: 5 Put: 6 Got: 6 Put: 7 Got: 7 Put: 8 Got: 8 Put: 9 Got: 9

WEEK:10

Write a program that creates a user interface to perform integer divisions.The user enters two numbers int the textfields,Num1 and Num2.The divison 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. AIM: To create a user interface to perform integer divisions PROGRAM: import java.awt.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.*; /*<applet code="AddEvent.class" height=100 width=500></applet>*/ public class AddEvent extends Applet implements ActionListener { TextField tf1; TextField tf2; Button b; TextField tf3; Label l; public void init() { l=new Label("enter the numbers and press divide button"); tf1=new TextField("",5); tf2=new TextField("",5); tf3=new TextField("",5); b=new Button("Divide"); add(l); add(tf1); add(tf2); add(b); add(tf3); b.addActionListener(this); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand()=="Divide") { try {

int n1=Integer.parseInt(tf1.getText()); int n2=Integer.parseInt(tf2.getText()); int n=n1/n2; tf3.setText(""+n); } catch(ArithmeticException e1) { JOptionPane.showMessageDialog(null,"Arthimetic Exception"); } catch(NumberFormatException e2) { JOptionPane.showMessageDialog(null,"NumberFormatException"); } } } } OUTPUT:

WEEK:11
Write a java program that implements a simple client/server application. The client sends a 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) AIM: To implement a simple client/server application. PROGRAM: //Server Program import java.io.*; import java.net.*; class Server { public static void main(String ar[])throws Exception { ServerSocket ss=new ServerSocket(10000); Socket s=ss.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); int n=Integer.parseInt(br.readLine()); PrintStream ps=new PrintStream(s.getOutputStream()); double d=(3.14*n*n); ps.println(d); System.out.println("radius received from client:"+n);

System.out.println("Area found:"+d); } } //Client Program import java.io.*; import java.net.*; class Client { public static void main(String ar[])throws Exception { Socket s=new Socket(InetAddress.getLocalHost(),10000); System.out.println("enter the radius of the circle "); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); PrintStream ps=new PrintStream(s.getOutputStream()); ps.println(n); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); System.out.println("area of the circle received from server:"+br1.readLine()); } } OUTPUT:

WEEK:12A
Write a java program that stimulates 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. AIM: To stimulate a traffic light PROGRAM: import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code="CBGroup" width=400 height=350></applet>*/ public class CBGroup extends Applet implements ItemListener { String msg=""; Checkbox Red,Green,Yellow; CheckboxGroup cbg; public void init() { cbg=new CheckboxGroup(); Red=new Checkbox("RED",cbg,false); Green=new Checkbox("GREEN",cbg,false); Yellow=new Checkbox("YELLOW",cbg,false);

add(Red); add(Yellow); add(Green); Red.addItemListener(this); Green.addItemListener(this); Yellow.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { if(cbg.getSelectedCheckbox().getLabel()=="RED") { g.setColor(Color.red); g.fillOval(10,10,50,50); g.setColor(Color.black); g.drawString("Red Light ON",60,100); } if(cbg.getSelectedCheckbox().getLabel()=="YELLOW") { g.setColor(Color.yellow); g.fillOval(10,10,50,50); g.setColor(Color.black); g.drawString("Yellow Light ON",60,100); } if(cbg.getSelectedCheckbox().getLabel()=="GREEN") { g.setColor(Color.green); g.fillOval(10,10,50,50); g.setColor(Color.black); g.drawString("Green Light ON",60,100); } } } OUTPUT:

WEEK:12B
Write a java program that allows the user to draw lines,rectangles and ovals. AIM: To allows the user to draw lines,rectangles and ovals. PROGRAM: import java.awt.*; import java.applet.*; public class Draw extends Applet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.drawLine(3,4,10,23); g.drawOval(195,100,90,55); g.drawRect(100,40,90,5); g.drawRoundRect(140,30,90,90,60,30);

} } /*<applet code="Draw" width=300 height=300></applet>*/ OUTPUT:

WEEK:13A
Write a Java program to create an abstract class named Shape that contains an empty method name numberOfSides().Provide three classes named Trapezoid,Traingle and Hexagon such that each 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. AIM: To create an abstract class PROGRAM: import java.lang.*; abstract class Shape { abstract void numberOfSides();

} class Traingle extends Shape { public void numberOfSides() { System.out.println("No.of sides in a triangle is three"); } } class Trapezoid extends Shape { public void numberOfSides() { System.out.println("No.of sides in a trapezoid is four"); } } class Hexagon extends Shape { public void numberOfSides() { System.out.println("No.of sides in a hexagon is six"); } } public class Sides { public static void main(String arg[]) { Traingle T=new Traingle(); Trapezoid Tz=new Trapezoid(); Hexagon H=new Hexagon(); T.numberOfSides(); Tz.numberOfSides(); H.numberOfSides(); } } OUTPUT: No.of sides in a triangle is three No.of sides in a trapezoid is four No.of sides in a hexagon is six

WEEK:13B
Write a Java program to display the tabe using JTable Component

AIM: To display the tabe using JTable Component PROGRAM: import java.awt.*; import javax.swing.*; /*<applet code="tabledemo" height=400 width=400></applet>*/ public class tabledemo extends JApplet { JTable jt; JScrollPane scrollpn; Container c; public void init() { c=getContentPane(); c.setLayout(new BorderLayout()); String[] columnheadings={"Roll No","Name","Branch"}; Object[][] data={{"08AT1A0505","Juveriah","CSE"}, {"08AT1A0403","Firdose","EEE"},{"008AT1A1207","Prathyusha","IT"}}; jt=new JTable(data,columnheadings); int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; scrollpn=new JScrollPane(jt,v,h); c.add(scrollpn,BorderLayout.CENTER); } } OUTPUT:

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.