You are on page 1of 15

St. Philomena s 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

B.Sc 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 = args.length; System.out.println("a = " + a); int b = 42 / a; int c[] = { 1 }; c[42] = 99; } catch(ArithmeticException e) { System.out.println("Divide by 0: " + e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index oob: " + e); } System.out.println("After try/catch blocks."); } } Output D:\6bscJava>javac MultiCatch.java D:\6bscjava>java MultiCatch A=0 Divide by 0: Java.lang.ArithmeticException: / by zero After try /catch blocks D:\6bscjava>java MulltiCatch 45 A=1 Array Index oob: java.lang.ArrayIndexOutOfBoundsException: 42 After try/catch blocks D:\6bscjava>

B.Sc 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 java.awt.*; import java.awt.event.*; public class nine extends java.applet.Applet 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); }

B.Sc in CS VI Semester, C++ & Java Lab Manual

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

B.Sc in CS VI Semester, C++ & Java Lab Manual

OUTPUT

B.Sc in CS VI Semester, C++ & Java Lab Manual

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

B.Sc in CS VI Semester, C++ & Java Lab Manual

//11. Java applet program to demonstrate simple calculator import java.awt.*; import java.awt.event.*; public class calci extends java.applet.Applet 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 = Float.parseFloat(tf_num1.getText()); num2 = Float.parseFloat(tf_num2.getText()); if(action.getSource() == bt_add) { B.Sc in CS VI Semester, C++ & Java Lab Manual 7

num3 = num1 + num2; tf_result.setText(String.valueOf(num3)); } if(action.getSource() == bt_sub) { num3 = num1 - num2; tf_result.setText(String.valueOf(num3)); } if(action.getSource() == bt_mul) { num3 = num1 * num2; tf_result.setText(String.valueOf(num3)); } if(action.getSource() == bt_div) { num3 = num1 / num2; tf_result.setText(String.valueOf(num3)); } } } Calculator.html <html> <head> <title> Simple calculator </title> </head> <body> <applet code=calci.class width=200 height=800> </applet> </body> </html> OUTPUT

B.Sc in CS VI Semester, C++ & Java Lab Manual

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

B.Sc 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) { this.animationApplet = a; this.delay=delay; } public void run()

{
while(true) { try { sleep(delay); animationApplet.animate(); } catch(Exception e) { e.printStackTrace(); } }

}
}

}
Animation.html <html> <head> <title> animation..... </title> </head> <body> <h1> JAVA ANIMATION </h1> <applet code=ANI.class height=600 width=600> </applet> </body> </html>

B.Sc in CS VI Semester, C++ & Java Lab Manual

10

OUTPUT

B.Sc in CS VI Semester, C++ & Java Lab Manual

11

//18. Java program to demonstrate multiple threading import java.io.*; class multithread { Priority t1,t2,t3; public multithread() { t1=new Priority(); t1.start(); t2=new Priority(); t2.start(); t3=new Priority(); t3.start(); } 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 { Thread.sleep(sleep); System.out.println("Name :"+getName() + " Priority :" + getPriority()); } catch(InterruptedException e) { System.out.println("Main thread interrupted"); } } }

B.Sc in CS VI Semester, C++ & Java Lab Manual

12

OUTPUT D:\6bscJava>javac multithread.java 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>

B.Sc in CS VI Semester, C++ & Java Lab Manual

13

//15. Java applet to demonstrate free handwriting import java.applet.*; import java.awt.*; 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(); g.drawLine(last_x, last_y, x, y); last_x=x; last_y=y; return true; } } freehand.html <html> <head> <title> This is free hand drawing </title> </head> <body> <Applet code=freehand.class height=600 width=600> </applet> </body> </html>

B.Sc in CS VI Semester, C++ & Java Lab Manual

14

OUTPUT

B.Sc in CS VI Semester, C++ & Java Lab Manual

15

You might also like