0% found this document useful (0 votes)
63 views15 pages

Java Laboratory Manual Overview

The document provides an overview of C++ and Java programs that are part of a lab manual for a 6th semester B.Sc Computer Science course. It lists 18 programs covering topics like exceptions, applets, threading, databases and more. For each program, it lists the program number, brief description and sample output.

Uploaded by

Pavan Gurunayak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views15 pages

Java Laboratory Manual Overview

The document provides an overview of C++ and Java programs that are part of a lab manual for a 6th semester B.Sc Computer Science course. It lists 18 programs covering topics like exceptions, applets, threading, databases and more. For each program, it lists the program number, brief description and sample output.

Uploaded by

Pavan Gurunayak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

St. Philomenas College, Mysore B.

Sc 6TH SEMESTER COMPUTER SCIENCE C++ AND JAVA LAB PROGRAMS


TABLE OF CONTENTS Sl. No Name of the Program C++ PROGRAMS 1 2 3 4 5 6 7 To perform Arithmetic Operations using Classes and Objects To simulate a banking system with the following operations a) Deposit b) Withdrawel c) Compound Interest d) Display balance

To find volume of Cube, Cylinder and Rectangular box using Function overloading To concatenate two string using operator overloading To demonstrate virtual function To declare result of a Student using multiple inheritance To declare result of a Student using multilevel inheritance

JAVA PROGRAMS 8 9 10 11 12 13 14 15 16 17 18 To demonstrate array-index-out-of bounds and arithmetic exception Java applet to calculate area and circumference of a circle using radio button and check box To display the IP Address of your working Computer Java applet to demonstrate simple calculator Java applet to demonstrate animation using image files Java program to demonstrate access control using package IO String Java program to count the number of words in a file using IO String Java applet to demonstrate free handwriting Program to create a Database and connect using JDBC Connectivity Java program to demonstrate Client Server Java program to demonstrate Multiple Threading

[Link] in CS VI Semester, C++ & Java Lab Manual

8. To demonstrate array-index-out-of bounds and arithmetic exception //8. Demonstrate multiple catch statements. class MultiCatch { public static void main(String args[]) { try { int a = [Link]; [Link]("a = " + a); int b = 42 / a; int c[] = { 1 }; c[42] = 99; } catch(ArithmeticException e) { [Link]("Divide by 0: " + e); } catch(ArrayIndexOutOfBoundsException e) { [Link]("Array index oob: " + e); } [Link]("After try/catch blocks."); } } Output D:\6bscJava>javac [Link] D:\6bscjava>java MultiCatch A=0 Divide by 0: [Link]: / by zero After try /catch blocks D:\6bscjava>java MulltiCatch 45 A=1 Array Index oob: [Link]: 42 After try/catch blocks D:\6bscjava>

[Link] in CS VI Semester, C++ & Java Lab Manual

//9. Java applet program to calculate area and circumference of a circle using radio //and check box import [Link].*; import [Link].*; public class nine extends [Link] implements ItemListener { Label lab1; TextField tf_radius, tf_result, tf_areasq, tf_circumsq; Checkbox cb_area, cb_circum, cb_areasq, cb_circumsq; CheckboxGroup cbg; public void init() { lab1=new Label("Enter Radius"); tf_radius=new TextField(15); tf_result=new TextField(30); tf_areasq=new TextField(15); tf_circumsq=new TextField(15); cbg=new CheckboxGroup(); cb_area=new Checkbox("Area of Circle"); cb_circum=new Checkbox("Circumference of Circle"); cb_area.setCheckboxGroup(cbg); cb_circum.setCheckboxGroup(cbg); cb_areasq=new Checkbox("Area Squared"); cb_circumsq=new Checkbox("Circumference Squared"); cb_areasq.setCheckboxGroup(cbg); cb_circumsq.setCheckboxGroup(cbg); add(lab1); add(tf_radius); add(cb_area); add(cb_circum); add(tf_result); add(cb_areasq); add(tf_areasq); add(cb_circumsq); add(tf_circumsq); cb_area.addItemListener(this); cb_circum.addItemListener(this); cb_areasq.addItemListener(this); cb_circumsq.addItemListener(this); }

[Link] in CS VI Semester, C++ & Java Lab Manual

public void itemStateChanged(ItemEvent item) { float radius, result; radius=[Link](tf_radius.getText()); if([Link]()==cb_area) { result=(float)(3.14*radius*radius); tf_result.setText([Link](result)); } if([Link]()==cb_circum) { result=(float)(2.0*3.14*radius); tf_result.setText([Link](result)); } if([Link]()==cb_areasq) { result=(float)(3.14*radius*radius); tf_areasq.setText([Link](result*result)); } if([Link]()==cb_circumsq) { result=(float)(2.0*3.142*radius); tf_circumsq.setText([Link](result*result)); } } } [Link] <html> <head> <title> calculate area of circle and circumference </title> </head> <body> <h1> OUT PUT </H1> <applet code = [Link] width=300 height=300> </applet> </body> </html>

[Link] in CS VI Semester, C++ & Java Lab Manual

OUTPUT

[Link] in CS VI Semester, C++ & Java Lab Manual

//10. Java program to display IP Address of working machine. import [Link].*; class ip { public static void main(String args[]) { try { InetAddress localhost=[Link](); String localhostName=[Link](); String localhostAddress=[Link]().toString(); [Link]("LocalHostName:"+localhostName); [Link]("LocalHost Address:"+localhostAddress); } catch(Exception e) { [Link]("Unable to Perform the Operation"); } } } OUTPUT D:\6bscJava>javac [Link] D:\6bscJava>java ip LocalHostName: TIMOTHY-PC LocalHost Address : [Link] D:\6bscJava>

[Link] in CS VI Semester, C++ & Java Lab Manual

//11. Java applet program to demonstrate simple calculator import [Link].*; import [Link].*; public class calci extends [Link] implements ActionListener { Label lab1,lab2,lab3; TextField tf_num1, tf_num2, tf_result; Button bt_add, bt_sub,bt_mul, bt_div; public void init() { //Initialize all labels lab1=new Label("Num1:"); lab2=new Label("Num2:"); lab3=new Label("Result:"); tf_num1 = new TextField(15); tf_num2 = new TextField(15); tf_result = new TextField(15); bt_add = new Button("ADD"); bt_sub = new Button("SUBTRACT"); bt_mul = new Button("MULTIPLY"); bt_div = new Button("DIVIDE"); add(lab1); add(tf_num1); add(lab2); add(tf_num2); add(lab3); add(tf_result); add(bt_add); add(bt_sub); add(bt_mul); add(bt_div); bt_add.addActionListener(this); bt_sub.addActionListener(this); bt_mul.addActionListener(this); bt_div.addActionListener(this); } public void actionPerformed(ActionEvent action) { float num1, num2, num3; num1 = [Link](tf_num1.getText()); num2 = [Link](tf_num2.getText()); if([Link]() == bt_add) { [Link] in CS VI Semester, C++ & Java Lab Manual 7

num3 = num1 + num2; tf_result.setText([Link](num3)); } if([Link]() == bt_sub) { num3 = num1 - num2; tf_result.setText([Link](num3)); } if([Link]() == bt_mul) { num3 = num1 * num2; tf_result.setText([Link](num3)); } if([Link]() == bt_div) { num3 = num1 / num2; tf_result.setText([Link](num3)); } } } [Link] <html> <head> <title> Simple calculator </title> </head> <body> <applet code=[Link] width=200 height=800> </applet> </body> </html> OUTPUT

[Link] in CS VI Semester, C++ & Java Lab Manual

//12. Java applet to demonstrate animation using image files. import [Link].*; import [Link].*; import [Link].*; import [Link].*; public class ANI extends Applet { Image[] img; int index=0; int maxImg; MediaTracker tracker; public void init() { img = new Image[2]; maxImg = [Link]-1; tracker = new MediaTracker(this); try { img[0] = getImage(new URL(getDocumentBase(),"[Link]")); img[1] = getImage(new URL(getDocumentBase(),"[Link]")); [Link](img[0],0); [Link](img[1],1); [Link](); } catch(Exception e) { [Link](); } AnimationThread at = new AnimationThread(); [Link](this, 500); [Link](); } public void paint(Graphics g) { if(img[0]!=null) { [Link](img[index],0,0,this); index = (index<maxImg) ? index + 1:0; } }

[Link] in CS VI Semester, C++ & Java Lab Manual

public void animate() { repaint(); } class AnimationThread extends Thread { ANI animationApplet; int delay; public void delayedAnimation(ANI a, int delay) { [Link] = a; [Link]=delay; } public void run()

{
while(true) { try { sleep(delay); [Link](); } catch(Exception e) { [Link](); } }

}
}

}
[Link] <html> <head> <title> animation..... </title> </head> <body> <h1> JAVA ANIMATION </h1> <applet code=[Link] height=600 width=600> </applet> </body> </html>

[Link] in CS VI Semester, C++ & Java Lab Manual

10

OUTPUT

[Link] in CS VI Semester, C++ & Java Lab Manual

11

//18. Java program to demonstrate multiple threading import [Link].*; class multithread { Priority t1,t2,t3; public multithread() { t1=new Priority(); [Link](); t2=new Priority(); [Link](); t3=new Priority(); [Link](); } public static void main(String args[]) { new multithread(); } } class Priority extends Thread implements Runnable { int sleep; static int prio=2; public Priority() { sleep=sleep+100; prio++; setPriority(prio); } public void run() { try { [Link](sleep); [Link]("Name :"+getName() + " Priority :" + getPriority()); } catch(InterruptedException e) { [Link]("Main thread interrupted"); } } }

[Link] in CS VI Semester, C++ & Java Lab Manual

12

OUTPUT D:\6bscJava>javac [Link] D:\6bscJava>java multithread Name : Thread-1 Priority : 4 Name : Thread-2 Priority : 5 Name : Thread-0 Priority : 3 D:\6bscJava>java multithread Name : Thread-2 Priority : 5 Name : Thread-1 Priority : 4 Name : Thread-0 Priority : 3 D:\6bscJava>java multithread Name : Thread-1 Priority : 4 Name : Thread-0 Priority : 3 Name : Thread-2 Priority : 5 D:\6bscJava>

[Link] in CS VI Semester, C++ & Java Lab Manual

13

//15. Java applet to demonstrate free handwriting import [Link].*; import [Link].*; public class freehand extends Applet { private int last_x=0; private int last_y=0; public boolean mouseDown(Event e, int x, int y) { last_x=x; last_y=y; return true; } public boolean mouseDrag(Event e, int x, int y) { Graphics g=getGraphics(); [Link](last_x, last_y, x, y); last_x=x; last_y=y; return true; } } [Link] <html> <head> <title> This is free hand drawing </title> </head> <body> <Applet code=[Link] height=600 width=600> </applet> </body> </html>

[Link] in CS VI Semester, C++ & Java Lab Manual

14

OUTPUT

[Link] in CS VI Semester, C++ & Java Lab Manual

15

You might also like