java practical file
java practical file
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……….…………………
………………………………………………….
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.
Simple.java
1. class HelloWorld {
2. public static void main (String args []) {
3. System.out.println("Hello world");
4. }
5. }
2. write java program to show if statement
Student.java
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. System.out.println("x + y is greater than 20");
7. }
8. }
9. }
Output:
x + y is greater than 20
Output:
x + y is greater than 20
4.Write program for Nested - if in java.
Student.java
1. public class Student {
2. public static void main(String[] args) {
3. String address = "Delhi, India";
4.
5. if(address.endsWith("India")) {
6. if(address.contains("Meerut")) {
7. System.out.println("Your city is Meerut");
8. }else if(address.contains("Noida")) {
9. System.out.println("Your city is Noida");
10. }else {
11. System.out.println(address.split(",")[0]);
12. }
13. }else {
14. System.out.println("You are not living in India");
15. }
16. }
17. }
Output:
Delhi
Student.java
1. public class Student implements Cloneable {
2. public static void main(String[] args) {
3. int num = 2;
4. switch (num){
5. case 0:
6. System.out.println("number is 0");
7. break;
8. case 1:
9. System.out.println("number is 1");
10. break;
11. default:
12. System.out.println(num);
13. } }}
Output: 2
6.Write program for loop statement in java.
Calculation.java
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. System.out.println("The sum of first 10 natural numbers is " + sum);
9. }
10. }
Output:
The sum of first 10 natural numbers is 55
Calculation.java
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. System.out.println("Printing the content of the array names:\n");
6. for(String name:names) {
7. System.out.println(name);
8. }
9. }
10. }
Output:
Java
C
C++
Python
JavaScript
8.Write 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. System.out.println("Printing the list of first 10 even numbers \n");
6. while(i<=10) {
7. System.out.println(i);
8. i = i + 2;
9. }
10. }
11. }
Output:
Printing the list of first 10 even numbers
0
2
4
6
8
10
Calculation .java
1. public class Calculation {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. int i = 0;
5. System.out.println("Printing the list of first 10 even numbers \n");
6. while(i<=10) {
7. System.out.println(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.
Calculation.java
1. public class Calculation {
2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. int i = 0;
5. System.out.println("Printing the list of first 10 even numbers \n");
6. do {
7. System.out.println(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<a.length;i++)//length is the property of array
13. System.out.println(a[i]);
14. }}
Output:
10
20
70
40
50
12. Show a program in java to declare, initialize and print the 2 Dimensional array.
Output:
123
245
445
14. write a java program to multiply two matrices with 3 rows and 3 columns.
StringExample.java
Output:
java
strings
example
Output:
string length is: 10
string length is: 6
17.write a program to show charAt() method string in java.
FileName: CharAtExample.java
Output:
t
File: TestInheritance.java
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output:
barking...
eating...
19. Write a java program to show multilevel Inheritance.
File: TestInheritance2.java
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output:
weeping...
barking...
eating...
20. Write a java program to show Hierarchical Inheritance.
File: TestInheritance3.java
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("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
c.meow();
c.eat();
d.bark();
d.eat();
}
}
Output:
meowing...
eating...
barking...
eating...
21.Write java program of AWT by Inheritance .
1. import java.awt.*;
2. class First extends Frame{
3. First(){
4. Button b=new Button("click me");
5. b.setBounds(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. }}
1. import java.awt.*;
2. import java.awt.event.*;
3. class AEvent extends Frame implements ActionListener{
4. TextField tf;
5. AEvent(){
6.
7. //create components
8. tf=new TextField();
9. tf.setBounds(60,50,170,20);
10. Button b=new Button("click me");
11. b.setBounds(100,120,80,30);
12.
13. //register listener
14. b.addActionListener(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. tf.setText("Welcome");
24. }
25. public static void main(String args[]){
26. new AEvent();
27. }
28. }
24. Java AWT button example with ActionListener
1. import java.awt.*;
2. import java.awt.event.*;
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. tf.setBounds(50,50, 150,20);
8. Button b=new Button("Click Here");
9. b.setBounds(50,100,60,30);
10. b.addActionListener(new ActionListener(){
11. public void actionPerformed(ActionEvent e){
12. tf.setText("Welcome to Javatpoint.");
13. }
14. });
15. f.add(b);f.add(tf);
16. f.setSize(400,400);
17. f.setLayout(null);
18. f.setVisible(true);
19. }
20. }
25. Java Label Example .
1. import java.awt.*;
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. l1.setBounds(50,100, 100,30);
8. l2=new Label("Second Label.");
9. l2.setBounds(50,150, 100,30);
10. f.add(l1); f.add(l2);
11. f.setSize(400,400);
12. f.setLayout(null);
13. f.setVisible(true);
14. }
15. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
3. public class LabelExample extends Frame implements ActionListener{
4. TextField tf; Label l; Button b;
5. LabelExample(){
6. tf=new TextField();
7. tf.setBounds(50,50, 150,20);
8. l=new Label();
9. l.setBounds(50,100, 250,20);
10. b=new Button("Find IP");
11. b.setBounds(50,150,60,30);
12. b.addActionListener(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=tf.getText();
21. String ip=java.net.InetAddress.getByName(host).getHostAddress();
22. l.setText("IP of "+host+" is: "+ip);
23. }catch(Exception ex){System.out.println(ex);}
24. }
25. public static void main(String[] args) {
26. new LabelExample();
27. }
28. }
Output:
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 java.awt.*;
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. t1.setBounds(50,100, 200,30);
8. t2=new TextField("AWT Tutorial");
9. t2.setBounds(50,150, 200,30);
10. f.add(t1); f.add(t2);
11. f.setSize(400,400);
12. f.setLayout(null);
13. f.setVisible(true);
14. }
15. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
3. public class TextFieldExample extends Frame implements ActionListener{
4. TextField tf1,tf2,tf3;
5. Button b1,b2;
6. TextFieldExample(){
7. tf1=new TextField();
8. tf1.setBounds(50,50,150,20);
9. tf2=new TextField();
10. tf2.setBounds(50,100,150,20);
11. tf3=new TextField();
12. tf3.setBounds(50,150,150,20);
13. tf3.setEditable(false);
14. b1=new Button("+");
15. b1.setBounds(50,200,50,50);
16. b2=new Button("-");
17. b2.setBounds(120,200,50,50);
18. b1.addActionListener(this);
19. b2.addActionListener(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=tf1.getText();
27. String s2=tf2.getText();
28. int a=Integer.parseInt(s1);
29. int b=Integer.parseInt(s2);
30. int c=0;
31. if(e.getSource()==b1){
32. c=a+b;
33. }else if(e.getSource()==b2){
34. c=a-b;
35. }
36. String result=String.valueOf(c);
37. tf3.setText(result);
38. }
39. public static void main(String[] args) {
40. new TextFieldExample();
41. }
42. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
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. l1.setBounds(50,50,100,30);
10. l2=new Label();
11. l2.setBounds(160,50,100,30);
12. area=new TextArea();
13. area.setBounds(20,100,300,300);
14. b=new Button("Count Words");
15. b.setBounds(100,400,100,30);
16. b.addActionListener(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=area.getText();
24. String words[]=text.split("\\s");
25. l1.setText("Words: "+words.length);
26. l2.setText("Characters: "+text.length());
27. }
28. public static void main(String[] args) {
29. new TextAreaExample();
30. }
31. }
Output:
1. import java.awt.*;
2. public class CheckboxExample
3. {
4. CheckboxExample(){
5. Frame f= new Frame("Checkbox Example");
6. Checkbox checkbox1 = new Checkbox("C++");
7. checkbox1.setBounds(100,100, 50,50);
8. Checkbox checkbox2 = new Checkbox("Java", true);
9. checkbox2.setBounds(100,150, 50,50);
10. f.add(checkbox1);
11. f.add(checkbox2);
12. f.setSize(400,400);
13. f.setLayout(null);
14. f.setVisible(true);
15. }
16. public static void main(String args[])
17. {
18. new CheckboxExample();
19. }
20. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
3. public class CheckboxExample
4. {
5. CheckboxExample(){
6. Frame f= new Frame("CheckBox Example");
7. final Label label = new Label();
8. label.setAlignment(Label.CENTER);
9. label.setSize(400,100);
10. Checkbox checkbox1 = new Checkbox("C++");
11. checkbox1.setBounds(100,100, 50,50);
12. Checkbox checkbox2 = new Checkbox("Java");
13. checkbox2.setBounds(100,150, 50,50);
14. f.add(checkbox1); f.add(checkbox2); f.add(label);
15. checkbox1.addItemListener(new ItemListener() {
16. public void itemStateChanged(ItemEvent e) {
17. label.setText("C++ Checkbox: "
18. + (e.getStateChange()==1?"checked":"unchecked"));
19. }
20. });
21. checkbox2.addItemListener(new ItemListener() {
22. public void itemStateChanged(ItemEvent e) {
23. label.setText("Java Checkbox: "
24. + (e.getStateChange()==1?"checked":"unchecked"));
25. }
26. });
27. f.setSize(400,400);
28. f.setLayout(null);
29. f.setVisible(true);
30. }
31. public static void main(String args[])
32. {
33. new CheckboxExample();
34. }
35. }
Output:
1. import java.awt.*;
2. public class ChoiceExample
3. {
4. ChoiceExample(){
5. Frame f= new Frame();
6. Choice c=new Choice();
7. c.setBounds(100,100, 75,75);
8. c.add("Item 1");
9. c.add("Item 2");
10. c.add("Item 3");
11. c.add("Item 4");
12. c.add("Item 5");
13. f.add(c);
14. f.setSize(400,400);
15. f.setLayout(null);
16. f.setVisible(true);
17. }
18. public static void main(String args[])
19. {
20. new ChoiceExample();
21. }
22. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
3. public class ChoiceExample
4. {
5. ChoiceExample(){
6. Frame f= new Frame();
7. final Label label = new Label();
8. label.setAlignment(Label.CENTER);
9. label.setSize(400,100);
10. Button b=new Button("Show");
11. b.setBounds(200,100,50,20);
12. final Choice c=new Choice();
13. c.setBounds(100,100, 75,75);
14. c.add("C");
15. c.add("C++");
16. c.add("Java");
17. c.add("PHP");
18. c.add("Android");
19. f.add(c);f.add(label); f.add(b);
20. f.setSize(400,400);
21. f.setLayout(null);
22. f.setVisible(true);
23. b.addActionListener(new ActionListener() {
24. public void actionPerformed(ActionEvent e) {
25. String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());
26. label.setText(data);
27. }
28. });
29. }
30. public static void main(String args[])
31. {
32. new ChoiceExample();
33. }
34. }
Output:
1. import java.awt.*;
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. menu.add(i1);
15. menu.add(i2);
16. menu.add(i3);
17. submenu.add(i4);
18. submenu.add(i5);
19. menu.add(submenu);
20. mb.add(menu);
21. f.setMenuBar(mb);
22. f.setSize(400,400);
23. f.setLayout(null);
24. f.setVisible(true);
25. }
26. public static void main(String args[])
27. {
28. new MenuExample();
29. }
30. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
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. d.setLayout( new FlowLayout() );
9. Button b = new Button ("OK");
10. b.addActionListener ( new ActionListener()
11. {
12. public void actionPerformed( ActionEvent e )
13. {
14. DialogExample.d.setVisible(false);
15. }
16. });
17. d.add( new Label ("Click button to continue."));
18. d.add(b);
19. d.setSize(300,300);
20. d.setVisible(true);
21. }
22. public static void main(String args[])
23. {
24. new DialogExample();
25. }
26. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
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. tf.setBounds(50,50, 150,20);
8. Button b=new Button("Click Here");
9. b.setBounds(50,100,60,30);
10.
11. b.addActionListener(new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. tf.setText("Welcome to Javatpoint.");
14. }
15. });
16. f.add(b);f.add(tf);
17. f.setSize(400,400);
18. f.setLayout(null);
19. f.setVisible(true);
20. }
21. }
Output:
1. import java.awt.*;
2. import java.awt.event.*;
3. public class MouseListenerExample extends Frame implements MouseListener{
4. Label l;
5. MouseListenerExample(){
6. addMouseListener(this);
7.
8. l=new Label();
9. l.setBounds(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. l.setText("Mouse Clicked");
17. }
18. public void mouseEntered(MouseEvent e) {
19. l.setText("Mouse Entered");
20. }
21. public void mouseExited(MouseEvent e) {
22. l.setText("Mouse Exited");
23. }
24. public void mousePressed(MouseEvent e) {
25. l.setText("Mouse Pressed");
26. }
27. public void mouseReleased(MouseEvent e) {
28. l.setText("Mouse Released");
29. }
30. public static void main(String[] args) {
31. new MouseListenerExample();
32. }
33. }
Output:
39.Java MouseMotionListener Example
1. import java.awt.*;
2. import java.awt.event.*;
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. g.setColor(Color.BLUE);
14. g.fillOval(e.getX(),e.getY(),20,20);
15. }
16. public void mouseMoved(MouseEvent e) {}
17.
18. public static void main(String[] args) {
19. new MouseMotionListenerExample();
20. }
21. }
Output: