LAB MANUAL
(SUBJECT CODE: MCA-352)
SUBJECT NAME: Java Programming Lab
SEMESTER-III
YEAR- 2024
BACHELOR OF COMPUTER APPLICATION
ODD SEMESTER 2024
DEPARTMENT OF COMPUTER APPLICATION
KERAL VERMA SUBHARTI COLLEGE OF SCIENCE
Submitted to Submitted By
Faculty Name Enrollment No: -2301000001437
Mr. Ankit Kumar Student Name: -Khyoda Tacho
Father Name: -Khyoda Tallar
CERTIFICATE
Mr./Km…………………………...................Father Name……….…………………
Enrollment No………………………… a student of ………………year of the
course……………….as a part of practical as prescribed by the Swami
Vivekanand Subharti University for the subject
………………………………………………….
in the computer lab during the academic year 2024-25 and that I have instructed
him/her for the said work, from time to time and I found him/her to be
satisfactory progressive.
Date: ……………… Signature & Name
of Subject Teacher
1. Write simple java program. Output:-
[Link]
1. class HelloWorld {
2. public static void main (String args []) {
3. [Link]("Hello world");
4. }
5. }
2. write java program to show if statement
[Link]
1. public class Student {
2. public static void main (String [] args) {
3. int x = 10;
4. int y = 12;
5. if (x+y > 20) {
6. [Link]("x + y is greater than 20");
7. }
8. }
9. }
Output:
x + y is greater than 20
[Link] program for if-else in java.
[Link]
1. public class Student {
2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y < 10) {
6. [Link]("x + y is less than 10");
7. } else {
8. [Link]("x + y is greater than 20");
9. }
10. }
11. }
Output:
x + y is greater than 20
[Link] program for Nested - if in java.
[Link]
1. public class Student {
2. public static void main(String[] args) {
3. String address = "Delhi, India";
4.
5. if([Link]("India")) {
6. if([Link]("Meerut")) {
7. [Link]("Your city is Meerut");
8. }else if([Link]("Noida")) {
9. [Link]("Your city is Noida");
10. }else {
11. [Link]([Link](",")[0]);
12. }
13. }else {
14. [Link]("You are not living in India");
15. }
16. }
17. }
Output:
Delhi
[Link] program for Switch statement in java.
[Link]
1. public class Student implements Cloneable {
2. public static void main(String[] args) {
3. int num = 2;
4. switch (num){
5. case 0:
6. [Link]("number is 0");
7. break;
8. case 1:
9. [Link]("number is 1");
10. break;
11. default:
12. [Link](num);
13. } }}
Output: 2
[Link] program for loop statement in java.
[Link]
1. public class Calculattion {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. int sum = 0;
5. for(int j = 1; j<=10; j++) {
6. sum = sum + j;
7. }
8. [Link]("The sum of first 10 natural numbers is " + sum);
9. }
10. }
Output:
The sum of first 10 natural numbers is 55
[Link] program for-each loop statement in java.
[Link]
1. public class Calculation {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. String[] names = {"Java","C","C++","Python","JavaScript"};
5. [Link]("Printing the content of the array names:\n");
6. for(String name:names) {
7. [Link](name);
8. }
9. }
10. }
Output:
Printing the content of the array names:
Java
C
C++
Python
JavaScript
[Link] program for-each loop statement in java.
Calculation .java
1. public class Calculation {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. int i = 0;
5. [Link]("Printing the list of first 10 even numbers \n");
6. while(i<=10) {
7. [Link](i);
8. i = i + 2;
9. }
10. }
11. }
Output:
Printing the list of first 10 even numbers
0
2
4
6
8
10
9. Write program while loop statement in java.
Calculation .java
1. public class Calculation {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. int i = 0;
5. [Link]("Printing the list of first 10 even numbers \n");
6. while(i<=10) {
7. [Link](i);
8. i = i + 2;
9. }
10. }
11. }
Output:
Printing the list of first 10 even numbers
0 2 4 6 8 10
10. Write program do while loop statement in java.
[Link]
1. public class Calculation {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. int i = 0;
5. [Link]("Printing the list of first 10 even numbers \n");
6. do {
7. [Link](i);
8. i = i + 2;
9. }while(i<=10);
10. }
11. }
Output:
Printing the list of first 10 even numbers
0 2 4 6 8 10
11. Show simple example of java array, to declare, initialize and traverse an array.
1. //Java Program to illustrate how to declare, instantiate, initialize
2. //and traverse the Java array.
3. class Testarray{
4. public static void main(String args[]){
5. int a[]=new int[5];//declaration and instantiation
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11. //traversing array
12. for(int i=0;i<[Link];i++)//length is the property of array
13. [Link](a[i]);
14. }}
Output:
10
20
70
40
50
12. Show a program in java to declare, initialize and print the 2 Dimensional array.
1. //Java Program to illustrate the use of multidimensional array
2. class Testarray3{
3. public static void main(String args[]){
4. //declaring and initializing 2D array
5. int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6. //printing 2D array
7. for(int i=0;i<3;i++){
8. for(int j=0;j<3;j++){
9. [Link](arr[i][j]+" ");
10. }
11. [Link]();
12. }
13. }}
Output:
123
245
445
13. write a java program to adds two matrices.
1. //Java Program to demonstrate the addition of two matrices in Java
2. class Testarray5{
3. public static void main(String args[]){
4. //creating two matrices
5. int a[][]={{1,3,4},{3,4,5}};
6. int b[][]={{1,3,4},{3,4,5}};
7.
8. //creating another matrix to store the sum of two matrices
9. int c[][]=new int[2][3];
10.
11. //adding and printing addition of 2 matrices
12. for(int i=0;i<2;i++){
13. for(int j=0;j<3;j++){
14. c[i][j]=a[i][j]+b[i][j];
15. [Link](c[i][j]+" ");
16. }
17. [Link]();//new line
18. }
19.
20. }}
Output:
21. 2 6 8
22. 6 8 10
14. write a java program to multiply two matrices with 3 rows and 3 columns.
1. //Java Program to multiply two matrices
2. public class MatrixMultiplicationExample{
3. public static void main(String args[]){
4. //creating two matrices
5. int a[][]={{1,1,1},{2,2,2},{3,3,3}};
6. int b[][]={{1,1,1},{2,2,2},{3,3,3}};
7.
8. //creating another matrix to store the multiplication of two matrices
9. int c[][]=new int[3][3]; //3 rows and 3 columns
10.
11. //multiplying and printing multiplication of 2 matrices
12. for(int i=0;i<3;i++){
13. for(int j=0;j<3;j++){
14. c[i][j]=0;
15. for(int k=0;k<3;k++)
16. {
17. c[i][j]+=a[i][k]*b[k][j];
18. }//end of k loop
19. [Link](c[i][j]+" "); //printing matrix element
20. }//end of j loop
21. [Link]();//new line
22. }
23. }}
Output:
24. 6 6 6
25. 12 12 12
26. 18 18 18
[Link] a program to show string in java.
[Link]
public class StringExample{
1. public static void main(String args[]){
2. String s1="java";//creating string by Java string literal
3. char ch[]={'s','t','r','i','n','g','s'};
4. String s2=new String(ch);//converting char array to string
5. String s3=new String("example");//creating Java string by new keyword
6. [Link](s1);
7. [Link](s2);
8. [Link](s3);
9. }}
Output:
java
strings
example
[Link] a program to show length() method string in java.
1. public class LengthExample{
2. public static void main(String args[]){
3. String s1="javaprograming";
4. String s2="python";
5. [Link]("string length is: "+[Link]());//10 is the length of javatpoint string
6. [Link]("string length is: "+[Link]());//6 is the length of python string
7. }}
Output:
string length is: 10
string length is: 6
[Link] a program to show charAt() method string in java.
FileName: [Link]
1. public class CharAtExample{
2. public static void main(String args[]){
3. String name="javatpoint";
4. char ch=[Link](4);//returns the char value at the 4th index
5. [Link](ch);
6. }}
Output:
t
18. Write a java program to show single Inheritance.
File: [Link]
class Animal
{
void eat()
{
[Link]("eating...");
}
}
class Dog extends Animal
{
void bark()
{
[Link]("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
[Link]();
[Link]();
}
}
Output:
barking...
eating...
19. Write a java program to show multilevel Inheritance.
File: [Link]
class Animal
{
void eat()
{
[Link]("eating...");
}
}
class Dog extends Animal
{
void bark()
{
[Link]("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
[Link]("weeping...");
}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
[Link]();
[Link]();
[Link]();
}
}
Output:
weeping...
barking...
eating...
20. Write a java program to show Hierarchical Inheritance.
File: [Link]
class Animal
{
void eat()
{
[Link]("eating...");
}
}
class Dog extends Animal
{
void bark()
{
[Link]("barking...");
}
}
class Cat extends Animal
{
void meow()
{
[Link]("meowing...");
}
}
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat(); // creating object for cat subclass
Dog d=new Dog(); // creating object for Dog subclass
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output:
meowing...
eating...
barking...
eating...
[Link] java program of AWT by Inheritance .
1. import [Link].*;
2. class First extends Frame{
3. First(){
4. Button b=new Button("click me");
5. [Link](30,100,80,30);// setting button position
6. add(b);//adding button into frame
7. setSize(300,300);//frame size 300 width and 300 height
8. setLayout(null);//no layout manager
9. setVisible(true);//now frame will be visible, by default not visible
10. }
11. public static void main(String args[]){
12. First f=new First();
13. }}
22. Write java program of AWT by Association
1. import [Link].*;
2. class First2{
3. First2(){
4. Frame f=new Frame();
5. Button b=new Button("click me");
6. [Link](30,50,80,30);
7. [Link](b);
8. [Link](300,300);
9. [Link](null);
10. [Link](true);
11. }
12. public static void main(String args[]){
13. First2 f=new First2();
14. }}
23. Java evet handling by implementing Action Listener
1. import [Link].*;
2. import [Link].*;
3. class AEvent extends Frame implements ActionListener{
4. TextField tf;
5. AEvent(){
6.
7. //create components
8. tf=new TextField();
9. [Link](60,50,170,20);
10. Button b=new Button("click me");
11. [Link](100,120,80,30);
12.
13. //register listener
14. [Link](this);//passing current instance
15.
16. //add components and set size, layout and visibility
17. add(b);add(tf);
18. setSize(300,300);
19. setLayout(null);
20. setVisible(true);
21. }
22. public void actionPerformed(ActionEvent e){
23. [Link]("Welcome");
24. }
25. public static void main(String args[]){
26. new AEvent();
27. }
28. }
24. Java AWT button example with ActionListener
1. import [Link].*;
2. import [Link].*;
3. public class ButtonExample {
4. public static void main(String[] args) {
5. Frame f=new Frame("Button Example");
6. final TextField tf=new TextField();
7. [Link](50,50, 150,20);
8. Button b=new Button("Click Here");
9. [Link](50,100,60,30);
10. [Link](new ActionListener(){
11. public void actionPerformed(ActionEvent e){
12. [Link]("Welcome to Javatpoint.");
13. }
14. });
15. [Link](b);[Link](tf);
16. [Link](400,400);
17. [Link](null);
18. [Link](true);
19. }
20. }
25. Java Label Example .
1. import [Link].*;
2. class LabelExample{
3. public static void main(String args[]){
4. Frame f= new Frame("Label Example");
5. Label l1,l2;
6. l1=new Label("First Label.");
7. [Link](50,100, 100,30);
8. l2=new Label("Second Label.");
9. [Link](50,150, 100,30);
10. [Link](l1); [Link](l2);
11. [Link](400,400);
12. [Link](null);
13. [Link](true);
14. }
15. }
Output:
[Link] AWT Label Example with ActionListener
1. import [Link].*;
2. import [Link].*;
3. public class LabelExample extends Frame implements ActionListener{
4. TextField tf; Label l; Button b;
5. LabelExample(){
6. tf=new TextField();
7. [Link](50,50, 150,20);
8. l=new Label();
9. [Link](50,100, 250,20);
10. b=new Button("Find IP");
11. [Link](50,150,60,30);
12. [Link](this);
13. add(b);add(tf);add(l);
14. setSize(400,400);
15. setLayout(null);
16. setVisible(true);
17. }
18. public void actionPerformed(ActionEvent e) {
19. try{
20. String host=[Link]();
21. String ip=[Link](host).getHostAddress();
22. [Link]("IP of "+host+" is: "+ip);
23. }catch(Exception ex){[Link](ex);}
24. }
25. public static void main(String[] args) {
26. new LabelExample();
27. }
28. }
Output:
27. Java AWT TextField .
The object of a TextField class is a text component that allows the editing of a single line
text. It inherits TextComponent class.
AWT TextField Class Declaration
public class TextField extends TextComponent
Java AWT TextField Example
1. import [Link].*;
2. class TextFieldExample{
3. public static void main(String args[]){
4. Frame f= new Frame("TextField Example");
5. TextField t1,t2;
6. t1=new TextField("Welcome to Javatpoint.");
7. [Link](50,100, 200,30);
8. t2=new TextField("AWT Tutorial");
9. [Link](50,150, 200,30);
10. [Link](t1); [Link](t2);
11. [Link](400,400);
12. [Link](null);
13. [Link](true);
14. }
15. }
Output:
28. Java AWT TextField Example with ActionListener
1. import [Link].*;
2. import [Link].*;
3. public class TextFieldExample extends Frame implements ActionListener{
4. TextField tf1,tf2,tf3;
5. Button b1,b2;
6. TextFieldExample(){
7. tf1=new TextField();
8. [Link](50,50,150,20);
9. tf2=new TextField();
10. [Link](50,100,150,20);
11. tf3=new TextField();
12. [Link](50,150,150,20);
13. [Link](false);
14. b1=new Button("+");
15. [Link](50,200,50,50);
16. b2=new Button("-");
17. [Link](120,200,50,50);
18. [Link](this);
19. [Link](this);
20. add(tf1);add(tf2);add(tf3);add(b1);add(b2);
21. setSize(300,300);
22. setLayout(null);
23. setVisible(true);
24. }
25. public void actionPerformed(ActionEvent e) {
26. String s1=[Link]();
27. String s2=[Link]();
28. int a=[Link](s1);
29. int b=[Link](s2);
30. int c=0;
31. if([Link]()==b1){
32. c=a+b;
33. }else if([Link]()==b2){
34. c=a-b;
35. }
36. String result=[Link](c);
37. [Link](result);
38. }
39. public static void main(String[] args) {
40. new TextFieldExample();
41. }
42. }
Output:
29. Java AWT TextArea Example
1. import [Link].*;
2. public class TextAreaExample
3. {
4. TextAreaExample(){
5. Frame f= new Frame();
6. TextArea area=new TextArea("Welcome to javatpoint");
7. [Link](10,30, 300,300);
8. [Link](area);
9. [Link](400,400);
10. [Link](null);
11. [Link](true);
12. }
13. public static void main(String args[])
14. {
15. new TextAreaExample();
16. }
17. }
Output:
30. Java AWT TextArea Example with ActionListener
1. import [Link].*;
2. import [Link].*;
3. public class TextAreaExample extends Frame implements ActionListener{
4. Label l1,l2;
5. TextArea area;
6. Button b;
7. TextAreaExample(){
8. l1=new Label();
9. [Link](50,50,100,30);
10. l2=new Label();
11. [Link](160,50,100,30);
12. area=new TextArea();
13. [Link](20,100,300,300);
14. b=new Button("Count Words");
15. [Link](100,400,100,30);
16. [Link](this);
17. add(l1);add(l2);add(area);add(b);
18. setSize(400,450);
19. setLayout(null);
20. setVisible(true);
21. }
22. public void actionPerformed(ActionEvent e){
23. String text=[Link]();
24. String words[]=[Link]("\\s");
25. [Link]("Words: "+[Link]);
26. [Link]("Characters: "+[Link]());
27. }
28. public static void main(String[] args) {
29. new TextAreaExample();
30. }
31. }
Output:
31. Java AWT Checkbox Example
1. import [Link].*;
2. public class CheckboxExample
3. {
4. CheckboxExample(){
5. Frame f= new Frame("Checkbox Example");
6. Checkbox checkbox1 = new Checkbox("C++");
7. [Link](100,100, 50,50);
8. Checkbox checkbox2 = new Checkbox("Java", true);
9. [Link](100,150, 50,50);
10. [Link](checkbox1);
11. [Link](checkbox2);
12. [Link](400,400);
13. [Link](null);
14. [Link](true);
15. }
16. public static void main(String args[])
17. {
18. new CheckboxExample();
19. }
20. }
Output:
32. Java AWT Checkbox Example with ItemListener
1. import [Link].*;
2. import [Link].*;
3. public class CheckboxExample
4. {
5. CheckboxExample(){
6. Frame f= new Frame("CheckBox Example");
7. final Label label = new Label();
8. [Link]([Link]);
9. [Link](400,100);
10. Checkbox checkbox1 = new Checkbox("C++");
11. [Link](100,100, 50,50);
12. Checkbox checkbox2 = new Checkbox("Java");
13. [Link](100,150, 50,50);
14. [Link](checkbox1); [Link](checkbox2); [Link](label);
15. [Link](new ItemListener() {
16. public void itemStateChanged(ItemEvent e) {
17. [Link]("C++ Checkbox: "
18. + ([Link]()==1?"checked":"unchecked"));
19. }
20. });
21. [Link](new ItemListener() {
22. public void itemStateChanged(ItemEvent e) {
23. [Link]("Java Checkbox: "
24. + ([Link]()==1?"checked":"unchecked"));
25. }
26. });
27. [Link](400,400);
28. [Link](null);
29. [Link](true);
30. }
31. public static void main(String args[])
32. {
33. new CheckboxExample();
34. }
35. }
Output:
33. Java AWT Choice Example
1. import [Link].*;
2. public class ChoiceExample
3. {
4. ChoiceExample(){
5. Frame f= new Frame();
6. Choice c=new Choice();
7. [Link](100,100, 75,75);
8. [Link]("Item 1");
9. [Link]("Item 2");
10. [Link]("Item 3");
11. [Link]("Item 4");
12. [Link]("Item 5");
13. [Link](c);
14. [Link](400,400);
15. [Link](null);
16. [Link](true);
17. }
18. public static void main(String args[])
19. {
20. new ChoiceExample();
21. }
22. }
Output:
34. Java AWT Choice Example with ActionListener
1. import [Link].*;
2. import [Link].*;
3. public class ChoiceExample
4. {
5. ChoiceExample(){
6. Frame f= new Frame();
7. final Label label = new Label();
8. [Link]([Link]);
9. [Link](400,100);
10. Button b=new Button("Show");
11. [Link](200,100,50,20);
12. final Choice c=new Choice();
13. [Link](100,100, 75,75);
14. [Link]("C");
15. [Link]("C++");
16. [Link]("Java");
17. [Link]("PHP");
18. [Link]("Android");
19. [Link](c);[Link](label); [Link](b);
20. [Link](400,400);
21. [Link](null);
22. [Link](true);
23. [Link](new ActionListener() {
24. public void actionPerformed(ActionEvent e) {
25. String data = "Programming language Selected: "+ [Link]([Link]());
26. [Link](data);
27. }
28. });
29. }
30. public static void main(String args[])
31. {
32. new ChoiceExample();
33. }
34. }
Output:
[Link] Menu Item and Menu example.
1. import [Link].*;
2. class MenuExample
3. {
4. MenuExample(){
5. Frame f= new Frame("Menu and MenuItem Example");
6. MenuBar mb=new MenuBar();
7. Menu menu=new Menu("Menu");
8. Menu submenu=new Menu("Sub Menu");
9. MenuItem i1=new MenuItem("Item 1");
10. MenuItem i2=new MenuItem("Item 2");
11. MenuItem i3=new MenuItem("Item 3");
12. MenuItem i4=new MenuItem("Item 4");
13. MenuItem i5=new MenuItem("Item 5");
14. [Link](i1);
15. [Link](i2);
16. [Link](i3);
17. [Link](i4);
18. [Link](i5);
19. [Link](submenu);
20. [Link](menu);
21. [Link](mb);
22. [Link](400,400);
23. [Link](null);
24. [Link](true);
25. }
26. public static void main(String args[])
27. {
28. new MenuExample();
29. }
30. }
Output:
36. Java AWT Dialog Example
1. import [Link].*;
2. import [Link].*;
3. public class DialogExample {
4. private static Dialog d;
5. DialogExample() {
6. Frame f= new Frame();
7. d = new Dialog(f , "Dialog Example", true);
8. [Link]( new FlowLayout() );
9. Button b = new Button ("OK");
10. [Link] ( new ActionListener()
11. {
12. public void actionPerformed( ActionEvent e )
13. {
14. [Link](false);
15. }
16. });
17. [Link]( new Label ("Click button to continue."));
18. [Link](b);
19. [Link](300,300);
20. [Link](true);
21. }
22. public static void main(String args[])
23. {
24. new DialogExample();
25. }
26. }
Output:
37. ActionListener Example: On Button click
1. import [Link].*;
2. import [Link].*;
3. public class ActionListenerExample {
4. public static void main(String[] args) {
5. Frame f=new Frame("ActionListener Example");
6. final TextField tf=new TextField();
7. [Link](50,50, 150,20);
8. Button b=new Button("Click Here");
9. [Link](50,100,60,30);
10.
11. [Link](new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. [Link]("Welcome to Javatpoint.");
14. }
15. });
16. [Link](b);[Link](tf);
17. [Link](400,400);
18. [Link](null);
19. [Link](true);
20. }
21. }
Output:
38. Java MouseListener Example
1. import [Link].*;
2. import [Link].*;
3. public class MouseListenerExample extends Frame implements MouseListener{
4. Label l;
5. MouseListenerExample(){
6. addMouseListener(this);
7.
8. l=new Label();
9. [Link](20,50,100,20);
10. add(l);
11. setSize(300,300);
12. setLayout(null);
13. setVisible(true);
14. }
15. public void mouseClicked(MouseEvent e) {
16. [Link]("Mouse Clicked");
17. }
18. public void mouseEntered(MouseEvent e) {
19. [Link]("Mouse Entered");
20. }
21. public void mouseExited(MouseEvent e) {
22. [Link]("Mouse Exited");
23. }
24. public void mousePressed(MouseEvent e) {
25. [Link]("Mouse Pressed");
26. }
27. public void mouseReleased(MouseEvent e) {
28. [Link]("Mouse Released");
29. }
30. public static void main(String[] args) {
31. new MouseListenerExample();
32. }
33. }
Output:
[Link] MouseMotionListener Example
1. import [Link].*;
2. import [Link].*;
3. public class MouseMotionListenerExample extends Frame implements MouseMotionListe
ner{
4. MouseMotionListenerExample(){
5. addMouseMotionListener(this);
6.
7. setSize(300,300);
8. setLayout(null);
9. setVisible(true);
10. }
11. public void mouseDragged(MouseEvent e) {
12. Graphics g=getGraphics();
13. [Link]([Link]);
14. [Link]([Link](),[Link](),20,20);
15. }
16. public void mouseMoved(MouseEvent e) {}
17.
18. public static void main(String[] args) {
19. new MouseMotionListenerExample();
20. }
21. }
Output: