Sunday, 18 December 2022

write a program to count the number of clicks performed by the user in a frame window (using Swing in java )

 

write a program to count the number of clicks performed by the user in a frame window

(using Swing in java )



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CounterTest extends JFrame implements ActionListener {
    private int count = 0;
    JLabel lblData;
    CounterTest ()
    {setLayout(new FlowLayout());
        lblData = new JLabel("button clicked a times");
        JButton btn = new JButton("Click me");
        btn.addActionListener((ActionListener) this);
        add(lblData);
        add(btn);

    }
    public void actionPerformed(ActionEvent e) {
        count++;
        lblData.setText(" Button clicked " + count + "times");

    }

    public static void main(String args[]) {

        CounterTest  ex = new CounterTest();
        ex.setVisible(true);
    }
}




OUTPUT:  







Develop a program using label (swing) to display message “Welcome to java”;

 


SWING

Develop a program using label (swing)  to display message “Welcome to java”;

import java.awt.*;

class L {
        L() {
                Frame f = new Frame();
                Label l1 = new Label("Welcome to Java");
                l1.setBounds(100, 50, 120, 80);
                f.add(l1);
                f.setSize(500, 500);
                f.setLayout(null);
                f.setVisible(true);
        }

        public static void main(String a[]) {
                new L();
        }
}



output; 



Read develop a program in which read the file by using scanner class.

  READ A FILE 




import java.util.Scanner;
import java.io.File;

public class file {
  public static void main(String args[]) throws Exception {

    try {
      File x = new File("a.txt");
      Scanner sc = new Scanner(x);
      while (sc.hasNext()) {
        System.out.println(sc.next());
      }
      sc.close();
    } catch (Exception e) {
      System.out.println("Error");
 
    }
}

}


OUTPUT: SSS

File System Develop a program and write it to files by using Formatter class IN JVA

 

FILE SYSTEM IN JAVA 


import java.util.*;
public class file {
        public static void main(String args[]) {
            try {
                Formatter f = new Formatter("a.txt");
                f.format("avinash");
                f.format("rathod");
                f.close();
   
            } catch (Exception e) {
                System.out.println("Error");
            }
        }
    }
 

OUTPUT: avinashrathod


Write a program to check the arithmetic exception by using try and catch

 


EXCEPTION HANDLING 




import java.io.*;

class exception {
    public static void main(String[] args) {
        int a = 5;
        int b = 0;
        try {
            System.out.println(a / b);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }

}


OUTPUT: java.lang.ArithmeticException: / by zero

Exception Handling Write a program to show the arithmetic exception using throws.

 Exception Handling     



public class exception {
    static void checkAge(int age) throws ArithmeticException {
      if (age < 18) {
        throw new ArithmeticException("Access denied - You must be at least 18 years old.");
      } else {
        System.out.println("Access granted - You are old enough!");
      }
   }
   
   public static void main(String[] args) {
     checkAge(15);
   }
 
}




OUTPUT: Exception in thread "main" java.lang.ArithmeticException: Access denied - You must be at least 18 years old.
        

develop a program which implements concepts of overriding(any example )

OVERRIDEING



class Animal {
    public void makeSound() {
        System.out.println("Grr...");
    }
}
class Cat extends Animal {
    public void makeSound() {
        System.out.println("Meow");
    }
}

class Program {
    public static void main(String[] args) {
        Cat c = new Cat();
        c.makeSound();
    }
}


OUTPUT : Meow

GitHub Most Imp Command For Every Developer Learn:

 Top Command for GitHub:  1) git clone 2) git init and git status   3) git add file name  or git add .  4) git commit -m message  5) git rem...