0% found this document useful (0 votes)
69 views29 pages

Index: SR No Practical Name Date Signatur E

The document contains details of 10 programming practical assignments in Java: 1. Finding factorial of a number using loops 2. Calculating volume using classes and objects 3. Constructor overloading 4. Single inheritance 5. Using Applets 6. Command line arguments 7. Packages and interfaces 8. Input/output in Java 9. Multithreading 10. Event handling using mouse in JavaScript

Uploaded by

geetanjali_7800
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views29 pages

Index: SR No Practical Name Date Signatur E

The document contains details of 10 programming practical assignments in Java: 1. Finding factorial of a number using loops 2. Calculating volume using classes and objects 3. Constructor overloading 4. Single inheritance 5. Using Applets 6. Command line arguments 7. Packages and interfaces 8. Input/output in Java 9. Multithreading 10. Event handling using mouse in JavaScript

Uploaded by

geetanjali_7800
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

Roll No:

INDEX
Sr No 1. 2. 3. 4. 5. 6. 7. 8. 9. 1 0. PRACTICAL NAME Write a program in java using loop for finding factorial of any number Write a program in java using classes and objects for finding volume Write a program in java for constructor overloading. Write a program in java for single inheritance Write a program in java using Applet Write a program in java using Command line argument Write a program in java for package and interface Write a program to show input/output in java Write a program to show multithreading in java Write a program to show event handling using mouse in java script DATE SIGNATUR E

Roll No:

Practical No:-1

Objective:- Write a program in java using loop for finding factorial of any number. Algorithm:-

1. Start. 2. Create a class fact. 3. Declare main function in fact class. 4. Declare a variable fact and i. 5. Set fact=1 and i=1. 6. While fact<=5 repeat step 7 to 8 7. Fact=fact*i. 8. Increment value of i by 1. 9. Print value of fact. 10. Stop.

Roll No:

Program code:-

class fact { public static void main(String args[]) { int fact=1; for(int i=1;i<=5;i++) { fact=fact*i; } System.out.println("Factorial of 5 is "+fact); } }

OUTPUT

Roll No:

Practical No:-2

Objective:- Write a program in java using classes and objects for finding volume.

Algorithm:-

1. Start. 2. Create a class val. 3. Declare variables l, b and h. 4. Create a class volume. 5. Declare main function in volume class. 6. Create an object v1 of class val. 7. Initialize the variables of v1 as v1.l=2, v1.b=5 and v1.h=7. 8. Calculate volume as vol=v1.l*v1.b*v1.h. 9. Print the value of vol. 10. Stop.

Roll No:

Program code:-

class val { int l,b,h; } class volume { public static void main(String args[]) { val v1=new val(); v1.l=2; v1.b=5; v1.h=7; int vol=v1.l*v1.b*v1.h; System.out.println("Volume is "+vol); } }

OUTPUT

Roll No:

Practical No:-3

Objective:- Write a program in java for constructor overloading.

Algorithm:-

1. Start. 2. Create a class const1. 3. Declare variable I, b and h. 4. Create a constructor const1() and initialize variable of class with some value. 5. Create another constructor const1(int l1,int b1,int h1) and initialize variable l,b and h with l1,b1 and h1 respectively. 6. Create a class const2. 7. Create main function in class const2. 8. Declare two object of class const1 and initialize one with const() and another with const(2,3,4); 9. Calculate volume of both objects. 10. Print volume. 11. Stop.

Roll No:

Roll No:

Program code:-

class const1 { int l,b,h; const1() { l=2; b=3; h=5; } const1(int l1,int b1,int h1) { l=l1; b=b1; h=h1; } } class const2 { public static void main(String args[]) { const1 v=new const1(); const1 v1=new const1(3,8,10); int volume=v.l*v.b*v.h; int volume1=v1.l*v1.b*v1.h; System.out.println("Volume using default constructor is "+volume); System.out.println("Volume using parametrized constructor is "+volume1); } }

Roll No:

OUTPUT

Roll No: Practical No:-4

Objective:- Write a program in java for single inheritance.

Algorithm:-

1. Start. 2. Create a class abc. 3. Declare variable I, b and h. 4. Create a constructor abc() and initialize variable of class with some value. 5. Create a class xyz and extends it with class abc. 6. Create a class inherit. 7. Create main function in inherit class. 8. Create an object of class xyz. 9. Calculate volume as volume= v.b*v.l*v.h. 10. Print volume. 11. Stop.

Roll No:

Program code:class abc { int l,b,h; abc() { l=2; b=3; h=7; } } class xyz extends abc { } class inharit { public static void main(String args[]) { xyz v=new xyz(); int volume=v.b*v.l*v.h; System.out.println("volume is "+volume); } } OUTPUT

Roll No:

Roll No: Practical No:-5

Objective:- Write a program in java using Applet.

Algorithm:-

1. Start. 2. Import java.awt.* and java.applet.*. 3. Create a class applettest and extends it with Applet class. 4. Declare a variable msg of string type. 5. Initialize the applet using init() function. 6. Set the background by using setBackground() function as setBackground(Color.cyan); 7. Set msg=In init method. 8. Start the applet using start() function. 9. Set msg+=In start method. 10. Initialize the paint() function as paint(Graphics g). 11. Set msg+=In paint method. 12. Display the value of msg on applet using g.drawString() function as g.drawString(msg,10,30). 13. Display some message in status bar using showStatus() function as showStatus("this is status window"). 14. Stop.

Roll No:

Program code:-

import java.awt.*; import java.applet.*; public class applettest extends Applet { String msg; public void init() { setBackground(Color.cyan); msg="In init method"; } public void start() { msg+="In start method"; } public void paint(Graphics g) { msg+="In paint method"; g.drawString(msg,10,30); showStatus("this is status window"); } } HTML Code:<html> <applet code="applettest" width=200 height=300></applet> </html>

Roll No:

OUTPUT

Roll No:

Roll No: Practical No:-6

Objective:- Write a program in java using Command line argument.

Algorithm:-

1. Start. 2. Create a class cmdlnargs. 3. Create main function in cmdlnargs class. 4. Declare a variable count and initialize it with the length of args[] array as count= args.length. 5. Declare a variable I and set it to 0. 6. While i<count repeat step 7 and 8. 7. Print the value of args[i]. 8. Increment the value of i by 1. 9. Stop.

Roll No:

Program code:-

class cmdlnargs { public static void main (String args[]) { int count=args.length; for(int i=0;i<count;i++) { System.out.println("Value="+args[i]); } } } OUTPUT

Roll No:

Practical No:-7

Objective:- Write a program in java for package and interface.

Algorithm:-

1. Start. 2. Create a package mypack. 3. Create an interface callback. 4. Declare a function call(). 5. Create a class bal implements it with callback interface. 6. Declare two variables a and b. 7. Create an constructor bal(int x, int y) and initialize a and b with x and y respectively. 8. Create a function show() and print the value of a and b. 9. Redefine the function call() ant print Implementation of interface. 10. Create a class pkgs. 11. Create main function in pkgs class. 12. Create an object of class bal and initialize it with bal(2,4). 13. Call the functions show() and call() by using object. 14. Stop.

Roll No:

Program code:-

package mypack; interface callback { void call(); } class bal implements callback { int a,b; bal(int x, int y) { a=x; b=y; } void show() { System.out.println("a="+a+" b="+b); } public void call() { System.out.println("Implementation of interface"); } } class pkgs { public static void main(String args[]) { bal b=new bal(2,4); b.show(); b.call(); } }

Roll No:

OUTPUT

Roll No:

Practical No:-8

Objective:- Write a program to show input/output in java.

Algorithm:-

1. Start. 2. Import java.io.* 3. Create a class input. 4. Create main function in input class. 5. Declare an variable name of string type. 6. Create an object of class BufferedReader as BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)). 7. Read a line from buffer and store into name as name=reader.readLine(). 8. Print the value of name. 9. Stop.

Roll No:

Program code:-

import java.io.*; class input { public static void main(String args[]) { String name; BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name "); try { name=reader.readLine(); System.out.println("Welcome "+name); } catch(IOException e) { System.out.println("Exception"); } } } OUTPUT

Roll No:

Roll No: Practical No:-9

Objective:- Write a program to show multithreading in java.

Algorithm:-

1. Start. 2. Create a class a and extends it with Thread class. 3. Define run() function. 4. Create a class b and extends it with Thread class. 5. Define run() function. 6. Create c class a and extends it with Thread class. 7. Define run() function. 8. Create a class mltytrdng. 9. Create main function in mltytrdng class. 10. Create an object a1of class a. 11. Start the thread of a1 object as a1.start(); 12. Create an object b1of class b. 13. Set the priority of b1 to maximum as b1.setPriority(Thread.MAX_PRIORITY). 14. Start the thread of b1 object as b1.start(); 15. Create an object c1of class c. 16. Set the priority of c1 to normal+2 as c1.setPriority(Thread.NORM_PRIORITY+2). 17. Start the thread of c1 object as c1.start(); 18. Stop.

Roll No:

Program code:-

class a extends Thread { public void run() { for(int i=1;i<=4;i++) System.out.println("Thread A:i"+i); System.out.println("Exit A"); } } class b extends Thread { public void run() { for(int i=1;i<=4;i++) System.out.println("Thread B:i"+i); System.out.println("Exit B"); } } class c extends Thread { public void run() { for(int i=1;i<=4;i++) System.out.println("Thread C:i"+i); System.out.println("Exit C"); } } class mltytrdng { public static void main(String args[]) { a a1=new a(); a1.start();

Roll No: b b1=new b(); b1.setPriority(Thread.MAX_PRIORITY); b1.start(); c c1=new c(); c1.setPriority(Thread.NORM_PRIORITY+2); c1.start(); } }

OUTPUT

Roll No:

Practical No:-10

Objective:- Write a program to show event handling using mouse in java script.

Html code:-

<html> <head> <script> function display(event) { var m=window.document.form1.textarea2.value; m=m+event; window.document.form1.textarea2.value=m; } </script> </head> <body> <form name=form1> <textarea cols=12 rows=20 name=textarea1 onmousedown="display('onmousedown\n');" onclick="display('onclick\n');" onmouseup="display('onmouseup\n');"></textarea> <textarea cols=12 rows=20 name=textarea2 ></textarea> <input type="button" value="clear" name=bt1 onclick="window.form1.textarea1.value=' '"> <input type="button" value="clear1" name=bt2 onclick="window.form1.textarea2.value=' '"> </form> </body> </html>

Roll No:

OUTPUT

You might also like