├── Arrays.java ├── Bean └── PersonBean.java ├── DatabaseConnect.java ├── Exception ├── Custom │ ├── MyException.java │ └── TestMyException.java └── ExceptionHandling.java ├── FileHandling.java ├── HelloWorld.java ├── Inheritance ├── Person.java └── Student.java ├── Interface ├── Circle.java ├── Rectangle.java └── Shape.java ├── MultiThreading.java ├── Networking ├── InetAddressClass.java ├── Tcp socket │ ├── TCPClient.java │ └── TCPServer.java ├── Udp socket │ ├── UDPReceiver.java │ └── UDPSender.java ├── UrlClass.java └── UrlConnectionClass.java ├── README.md ├── RMI ├── AdderInterface.java ├── AdderRemote.java ├── Client.java ├── Server.java └── how-to-run ├── Swing ├── AddNumber.java ├── Form.java ├── Layout │ ├── Border.java │ ├── Grid.java │ └── GridBag.java ├── List.java ├── ProgressBar.java ├── SimpleInterest.java ├── Slider.java └── Table.java └── loop.jsp /Arrays.java: -------------------------------------------------------------------------------- 1 | public class Arrays { 2 | public static void main(String[] args) { 3 | /* Declaration */ 4 | int intArr[] = new int[5]; 5 | /* Initialization */ 6 | intArr[0] = 5; intArr[1] = 3; intArr[2] = 20; intArr[3] = 14; intArr[4] = 23; 7 | 8 | /* Declaration and initialization */ 9 | double doubleArr[] = {2.5, 1.33, 6.89, 17.33, 43.10}; 10 | 11 | /* Multi dimensional array */ 12 | int a[][] = {{1,2}, {3,4}}; 13 | int b[][] = new int[2][2]; 14 | b[0][0] = 3; b[0][1] = 5; 15 | b[1][0] = 1; b[1][1] = 21; 16 | 17 | for (int i=0; i<2; i++) 18 | for (int j=0; j<2; j++) 19 | System.out.println(a[i][j] + b[i][j]); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Bean/PersonBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | Java bean is a special class that follows certain standards. They are: 3 | 1. It should have no argument constructor (default constructor). 4 | 2. Should be serializable (implements java.io.Serializable interface). 5 | 3. All properties should be private. 6 | 4. Should have getter and setter methods for each properties. 7 | 8 | How bean differ from class? 9 | - All beans are classes, all classes are not beans. 10 | - Beans must have a no argument constructor, classes may not have. 11 | - Beans must implements Serializable, classes may not. 12 | - Beans have only private properties, classes may have private, public, protected or default properties. 13 | - Beans should have getter and setter for all properties, not compulsory for classes. 14 | */ 15 | 16 | public class PersonBean implements java.io.Serializable /* Implements Serializable */ { 17 | /* All properties are private */ 18 | private String name; 19 | private int age; 20 | 21 | /* No argument Constructor */ 22 | public PersonBean() { 23 | name = ""; 24 | age = 0; 25 | } 26 | 27 | /* Setter methods */ 28 | public void setName (String name) { 29 | this.name = name; 30 | } 31 | 32 | public void setAge (int age) { 33 | this.age = age; 34 | } 35 | 36 | /* Getter methods */ 37 | public String getName () { 38 | return name; 39 | } 40 | 41 | public int getAge() { 42 | return age; 43 | } 44 | 45 | /* Other additional methods as per requirements can be added in a bean. */ 46 | } -------------------------------------------------------------------------------- /DatabaseConnect.java: -------------------------------------------------------------------------------- 1 | import java.sql.*; 2 | 3 | public class DatabaseConnect { 4 | public static void main(String[] args) throws SQLException{ 5 | String jdbcDriver = "com.mysql.jdbc.Driver"; 6 | String dbUrl = "jdbc:mysql://localhost:3306/student"; 7 | try { 8 | Class.forName(jdbcDriver); 9 | Connection conn = DriverManager.getConnection(dbUrl, "root", "password"); 10 | Statement stmt = conn.createStatement(); 11 | 12 | /* Student(name, course, marks, college)*/ 13 | String query = "Select * from student"; 14 | 15 | ResultSet rs = stmt.executeQuery(query); 16 | while (rs.next()) { 17 | System.out.println(rs.getString("Name")); 18 | } 19 | conn.close(); 20 | } catch (Exception e) { 21 | System.out.println(e); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Exception/Custom/MyException.java: -------------------------------------------------------------------------------- 1 | public class MyException extends Exception { 2 | MyException(String s) { 3 | super(s); 4 | } 5 | } -------------------------------------------------------------------------------- /Exception/Custom/TestMyException.java: -------------------------------------------------------------------------------- 1 | public class TestMyException { 2 | public static void main(String[] args) { 3 | String name; 4 | name = "Sagun"; 5 | 6 | try { 7 | if (name == "Sagun" || name == "sagun") 8 | throw new MyException("Name already reserved"); 9 | } catch (Exception e) { 10 | System.out.println(e); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Exception/ExceptionHandling.java: -------------------------------------------------------------------------------- 1 | public class ExceptionHandling { 2 | public static void main(String args[]) { 3 | try { 4 | int arr[] = {5,10, 6, 9, 11}; 5 | System.out.println("Sixth element :" + arr[6]); 6 | } catch(ArrayIndexOutOfBoundsException e) { 7 | System.out.println("Exception thrown :" + e); 8 | } finally { 9 | System.out.println("Program in finally block"); 10 | } 11 | 12 | System.out.println("Program Terminating"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /FileHandling.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class FileHandling { 4 | public static void main(String[] args) throws IOException{ 5 | /* Copy File using Character stream classes */ 6 | FileReader fin = new FileReader("test.txt"); 7 | FileWriter fout = new FileWriter("new.txt"); 8 | int ch; 9 | while ((ch=fin.read())!=-1) 10 | fout.write(ch); 11 | 12 | System.out.println("File copied successfully"); 13 | fin.close(); 14 | fout.close(); 15 | 16 | /* Copy File using Byte stream classes */ 17 | FileInputStream in = new FileInputStream("new.txt");; 18 | FileOutputStream out = new FileOutputStream("out.txt"); 19 | 20 | int c; 21 | while ((c = in.read()) != -1) { 22 | out.write(c); 23 | } 24 | in.close(); 25 | out.close(); 26 | } 27 | } -------------------------------------------------------------------------------- /HelloWorld.java: -------------------------------------------------------------------------------- 1 | public class HelloWorld { 2 | public static void main(String[] args) { 3 | System.out.println("Hello World!"); 4 | } 5 | } -------------------------------------------------------------------------------- /Inheritance/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String firstName; 3 | private String lastName; 4 | 5 | Person(String firstName, String lastName) { 6 | this.firstName = firstName; 7 | this.lastName = lastName; 8 | } 9 | 10 | public void sayHello() { 11 | System.out.println("Hello " + firstName + " " + lastName); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Inheritance/Student.java: -------------------------------------------------------------------------------- 1 | public class Student extends Person{ 2 | private double percent; 3 | private String course; 4 | 5 | public Student(String firstName, String lastName, double percent, String course) { 6 | super(firstName, lastName); 7 | this.percent = percent; 8 | this.course = course; 9 | } 10 | 11 | public void display() { 12 | System.out.println("Percent : " + percent); 13 | System.out.println("Course : " + course); 14 | } 15 | 16 | public static void main(String[] args) { 17 | Student student = new Student("Bat", "Man", 76.80, "Any"); 18 | student.sayHello(); 19 | student.display(); 20 | } 21 | } -------------------------------------------------------------------------------- /Interface/Circle.java: -------------------------------------------------------------------------------- 1 | public class Circle implements Shape{ 2 | private double radius; 3 | 4 | public Circle(double radius) { 5 | this.radius = radius; 6 | } 7 | 8 | public double area() { 9 | return 3.14*radius*radius; 10 | } 11 | 12 | public double perimeter() { 13 | return 2*3.14*radius; 14 | } 15 | 16 | public static void main(String[] args) { 17 | Circle circle = new Circle(5.5); 18 | System.out.println("Area = " + circle.area()); 19 | System.out.println("Perimeter = " + circle.perimeter()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Interface/Rectangle.java: -------------------------------------------------------------------------------- 1 | public class Rectangle implements Shape{ 2 | private double length; 3 | private double breadth; 4 | 5 | public Rectangle(double length, double breadth) { 6 | this.length = length; 7 | this.breadth = breadth; 8 | } 9 | 10 | public double area() { 11 | return length*breadth; 12 | } 13 | 14 | public double perimeter() { 15 | return 2*(length+breadth); 16 | } 17 | 18 | public static void main(String[] args) { 19 | Rectangle rectangle = new Rectangle(6.5, 7.9); 20 | System.out.println("Area = " + rectangle.area()); 21 | System.out.println("Perimeter = " + rectangle.perimeter()); 22 | } 23 | } -------------------------------------------------------------------------------- /Interface/Shape.java: -------------------------------------------------------------------------------- 1 | interface Shape { 2 | double area(); 3 | double perimeter(); 4 | } -------------------------------------------------------------------------------- /MultiThreading.java: -------------------------------------------------------------------------------- 1 | public class MultiThreading extends Thread{ 2 | private Thread t; 3 | private String name; 4 | 5 | MultiThreading(String name) { 6 | this.name = name; 7 | System.out.println("Creating thread " + name); 8 | } 9 | 10 | public void run() { 11 | System.out.println("Running thread " + name); 12 | try { 13 | for (int i = 5; i > 0; i--) { 14 | System.out.println("Thread " + name + ", Counting " + i); 15 | Thread.sleep(50); 16 | } 17 | } catch (InterruptedException e) { 18 | System.out.println("Thread " + name + " is interrupted"); 19 | } 20 | System.out.println("Exiting Thread " + name); 21 | } 22 | 23 | public static void main(String[] args) { 24 | MultiThreading first = new MultiThreading("First"); 25 | first.start(); 26 | MultiThreading second = new MultiThreading("Second"); 27 | second.start(); 28 | } 29 | } -------------------------------------------------------------------------------- /Networking/InetAddressClass.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | 3 | public class InetAddressClass { 4 | public static void main(String[] args) throws Exception{ 5 | InetAddress inetAddress = InetAddress.getByName("www.google.com"); 6 | System.out.println("Host name = " + inetAddress.getHostName()); 7 | System.out.println("IP address = " + inetAddress.getHostAddress()); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Networking/Tcp socket/TCPClient.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | 4 | public class TCPClient { 5 | 6 | public static void main(String [] args) throws Exception{ 7 | String serverName = "127.0.0.1"; 8 | int port = 6066; 9 | Socket client = new Socket(serverName, port); 10 | 11 | OutputStream outToServer = client.getOutputStream(); 12 | DataOutputStream out = new DataOutputStream(outToServer); 13 | 14 | out.writeUTF("Hello server"); 15 | InputStream inFromServer = client.getInputStream(); 16 | DataInputStream in = new DataInputStream(inFromServer); 17 | 18 | System.out.println("Server says " + in.readUTF()); 19 | client.close(); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /Networking/Tcp socket/TCPServer.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.io.*; 3 | 4 | public class TCPServer extends Thread { 5 | private ServerSocket serverSocket; 6 | public static int i; 7 | 8 | public TCPServer(int port) throws IOException { 9 | serverSocket = new ServerSocket(port); 10 | serverSocket.setSoTimeout(10000); 11 | i=1; 12 | } 13 | 14 | public void run() { 15 | while (true) { 16 | try { 17 | Socket server = serverSocket.accept(); 18 | 19 | DataInputStream in = new DataInputStream(server.getInputStream()); 20 | System.out.println("Client " + i +" says " + in.readUTF()); 21 | i++; 22 | 23 | DataOutputStream out = new DataOutputStream(server.getOutputStream()); 24 | out.writeUTF("Hi Client"); 25 | server.close(); 26 | } catch (Exception e) { 27 | System.out.println("Exception: " + e); 28 | } 29 | } 30 | } 31 | 32 | public static void main(String [] args) throws Exception{ 33 | int port = 6066; 34 | Thread t = new TCPServer(port); 35 | t.start(); 36 | } 37 | } -------------------------------------------------------------------------------- /Networking/Udp socket/UDPReceiver.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | 3 | public class UDPReceiver{ 4 | public static void main(String[] args) throws Exception { 5 | DatagramSocket socket = new DatagramSocket(3000); 6 | byte[] buffer = new byte[1024]; 7 | DatagramPacket packet = new DatagramPacket(buffer, 1024); 8 | socket.receive(packet); 9 | String str = new String(packet.getData(), 0, packet.getLength()); 10 | System.out.println("Received packet: " + str); 11 | socket.close(); 12 | } 13 | } -------------------------------------------------------------------------------- /Networking/Udp socket/UDPSender.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | 3 | public class UDPSender{ 4 | public static void main(String[] args) throws Exception { 5 | DatagramSocket socket = new DatagramSocket(); 6 | String packet = "UDP Packet"; 7 | InetAddress ip = InetAddress.getByName("localhost"); 8 | 9 | DatagramPacket datagramPacket = 10 | new DatagramPacket(packet.getBytes(), packet.length(), ip, 3000); 11 | 12 | socket.send(datagramPacket); 13 | socket.close(); 14 | 15 | System.out.println("Packet Sent"); 16 | } 17 | } -------------------------------------------------------------------------------- /Networking/UrlClass.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | 3 | public class UrlClass { 4 | public static void main(String[] args) throws Exception{ 5 | URL url = new URL( 6 | "https://docs.oracle.com/javase/tutorial/networking/overview/networking.html"); 7 | System.out.println("Protocol = " + url.getProtocol()); 8 | System.out.println("Host name = " + url.getHost()); 9 | System.out.println("File = " + url.getFile()); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Networking/UrlConnectionClass.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class UrlConnectionClass { 5 | public static void main(String[] args) throws Exception{ 6 | URL url=new URL("https://sagunsh.github.io/"); 7 | URLConnection urlconn = url.openConnection(); 8 | InputStream inputStream = urlconn.getInputStream(); 9 | int c; 10 | while((c=inputStream.read())!=-1){ 11 | System.out.print((char)c); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaPrograms 2 | Java Programs (7th semester CSIT) 3 | 4 | - Clone the repo 5 | ``` 6 | git clone https://github.com/sagunsh/JavaPrograms 7 | ``` 8 | -------------------------------------------------------------------------------- /RMI/AdderInterface.java: -------------------------------------------------------------------------------- 1 | import java.rmi.*; 2 | 3 | public interface AdderInterface extends Remote { 4 | public int add(int x, int y)throws RemoteException; 5 | } -------------------------------------------------------------------------------- /RMI/AdderRemote.java: -------------------------------------------------------------------------------- 1 | import java.rmi.*; 2 | import java.rmi.server.*; 3 | 4 | public class AdderRemote extends UnicastRemoteObject implements AdderInterface { 5 | AdderRemote()throws RemoteException { 6 | super(); 7 | } 8 | 9 | public int add(int x, int y) { 10 | return x+y; 11 | } 12 | } -------------------------------------------------------------------------------- /RMI/Client.java: -------------------------------------------------------------------------------- 1 | import java.rmi.*; 2 | 3 | public class Client{ 4 | public static void main(String args[]) { 5 | try { 6 | AdderInterface stub = (AdderInterface) Naming.lookup("rmi://localhost:1234/sagunsh"); 7 | System.out.println(stub.add(103,51)); 8 | } catch(Exception e) { 9 | System.out.println("Exception: " + e); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /RMI/Server.java: -------------------------------------------------------------------------------- 1 | import java.rmi.*; 2 | 3 | public class Server{ 4 | public static void main(String args[]) { 5 | try { 6 | AdderInterface stub = new AdderRemote(); 7 | Naming.rebind("rmi://localhost:1234/sagunsh", stub); 8 | } catch(Exception e) { 9 | System.out.println(e); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /RMI/how-to-run: -------------------------------------------------------------------------------- 1 | How to run the program? 2 | 3 | - Open terminal (or cmd) and move to the directory containing these files 4 | cd JavaPrograms/RMI 5 | 6 | - Compile all files 7 | javac *.java 8 | 9 | - Run the following commands 10 | rmic AdderRemote 11 | rmiregistry 1234 12 | 13 | - Open second terminal window and start server 14 | java Server 15 | 16 | - Open third terminal window and start client 17 | java Client 18 | 19 | -------------------------------------------------------------------------------- /Swing/AddNumber.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.*; 4 | 5 | public class AddNumber extends JFrame implements ActionListener{ 6 | private JButton addBtn; 7 | private JTextField num1; 8 | private JTextField num2; 9 | private JLabel plusSign; 10 | private JLabel equalSign; 11 | private JLabel result; 12 | 13 | AddNumber() 14 | { 15 | initUI(); 16 | } 17 | 18 | public void initUI() 19 | { 20 | GridBagLayout layout = new GridBagLayout(); 21 | GridBagConstraints gbc = new GridBagConstraints(); 22 | setLayout(layout); 23 | 24 | addBtn = new JButton("ADD"); 25 | num1 = new JTextField(5); 26 | num2 = new JTextField(5); 27 | result = new JLabel(); 28 | plusSign = new JLabel(" + "); 29 | 30 | gbc.gridx = 0;gbc.gridy = 0; 31 | add(num1, gbc); 32 | gbc.gridx = 1; 33 | add(plusSign, gbc); 34 | gbc.gridx = 2; 35 | add(num2, gbc); 36 | gbc.gridx = 0;gbc.gridy = 2; 37 | gbc.gridwidth = 4; 38 | add(addBtn, gbc); 39 | gbc.gridx = 0;gbc.gridy = 4; 40 | add(result, gbc); 41 | 42 | setSize(400,400); 43 | setVisible(true); 44 | setDefaultCloseOperation(EXIT_ON_CLOSE); 45 | addBtn.addActionListener(this); 46 | } 47 | 48 | public void actionPerformed(ActionEvent actionEvent) { 49 | int x = Integer.parseInt(num1.getText()); 50 | int y = Integer.parseInt(num2.getText()); 51 | result.setText("Sum = " + Integer.toString(x+y)); 52 | } 53 | 54 | public static void main(String [] args) { 55 | new AddNumber(); 56 | } 57 | } -------------------------------------------------------------------------------- /Swing/Form.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | public class Form extends JFrame{ 5 | JLabel firstNameLabel, lastNameLabel, passwordLabel, 6 | emailLabel, genderLabel, languageLabel; 7 | JTextField firstName, lastName, email; 8 | JPasswordField password; 9 | JRadioButton maleBtn, femaleBtn; 10 | JCheckBox cpp, java, python; 11 | 12 | Form() { 13 | GridBagConstraints gbc = new GridBagConstraints(); 14 | setLayout(new GridBagLayout()); 15 | 16 | firstName = new JTextField(20); 17 | lastName = new JTextField(20); 18 | email = new JTextField(20); 19 | password = new JPasswordField(20); 20 | maleBtn = new JRadioButton("Male"); 21 | femaleBtn = new JRadioButton("Female", true); 22 | cpp = new JCheckBox("C++"); 23 | java = new JCheckBox("Java"); 24 | python = new JCheckBox("Python", true); 25 | 26 | firstNameLabel = new JLabel("First Name"); 27 | lastNameLabel = new JLabel("Last Name"); 28 | emailLabel = new JLabel("Email Address"); 29 | passwordLabel = new JLabel("Password"); 30 | genderLabel = new JLabel("Gender"); 31 | languageLabel = new JLabel("Programming Language"); 32 | 33 | gbc.gridx = 0; gbc.gridy = 0; 34 | add(firstNameLabel, gbc); 35 | gbc.gridx = 2; gbc.gridy = 0; 36 | add(firstName, gbc); 37 | 38 | gbc.gridx = 0; gbc.gridy = 1; 39 | add(lastNameLabel, gbc); 40 | gbc.gridx = 2; gbc.gridy = 1; 41 | add(lastName, gbc); 42 | 43 | gbc.gridx = 0; gbc.gridy = 2; 44 | add(emailLabel, gbc); 45 | gbc.gridx = 2; gbc.gridy = 2; 46 | add(email, gbc); 47 | 48 | gbc.gridx = 0; gbc.gridy = 3; 49 | add(passwordLabel, gbc); 50 | gbc.gridx = 2; gbc.gridy = 3; 51 | add(password, gbc); 52 | 53 | gbc.gridx = 0; gbc.gridy = 4; 54 | add(genderLabel, gbc); 55 | gbc.gridx = 2; gbc.gridy = 4; 56 | add(maleBtn, gbc); 57 | gbc.gridx = 4; gbc.gridy = 4; 58 | add(femaleBtn, gbc); 59 | ButtonGroup buttonGroup = new ButtonGroup(); 60 | buttonGroup.add(maleBtn);buttonGroup.add(femaleBtn); 61 | 62 | gbc.gridwidth = 20; 63 | gbc.gridx = 0; gbc.gridy = 5; 64 | add(languageLabel, gbc); 65 | gbc.gridx = 0; gbc.gridy = 6; 66 | add(cpp, gbc); 67 | gbc.gridx = 0; gbc.gridy = 7; 68 | add(java, gbc); 69 | gbc.gridx = 0; gbc.gridy = 8; 70 | add(python, gbc); 71 | 72 | setTitle("Simple Form"); 73 | setVisible(true); 74 | setSize(500,500); 75 | 76 | /* 77 | To do list 78 | - Add button 79 | - Add listener to button 80 | */ 81 | } 82 | 83 | public static void main(String[] args) { 84 | new Form(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Swing/Layout/Border.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | public class Border { 5 | JFrame jFrame; 6 | Border() { 7 | jFrame = new JFrame(); 8 | JButton labelNorth = new JButton("North"); 9 | JButton labelSouth = new JButton("South"); 10 | JButton labelEast = new JButton("East"); 11 | JButton labelWest = new JButton("West"); 12 | JButton labelCenter = new JButton("Center"); 13 | 14 | jFrame.add(labelNorth, BorderLayout.NORTH); 15 | jFrame.add(labelSouth, BorderLayout.SOUTH); 16 | jFrame.add(labelEast, BorderLayout.EAST); 17 | jFrame.add(labelWest, BorderLayout.WEST); 18 | jFrame.add(labelCenter, BorderLayout.CENTER); 19 | 20 | jFrame.setSize(300,300); 21 | jFrame.setVisible(true); 22 | } 23 | 24 | public static void main(String[] args) { 25 | new Border(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Swing/Layout/Grid.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | public class Grid{ 5 | JFrame jFrame; 6 | 7 | Grid() { 8 | initUI(); 9 | } 10 | 11 | public void initUI() { 12 | jFrame = new JFrame(); 13 | 14 | JButton btn[] = new JButton[9]; 15 | for (int i=0; i<9; i++) { 16 | btn[i] = new JButton(Integer.toString(i+1)); 17 | jFrame.add(btn[i]); 18 | } 19 | jFrame.setLayout(new GridLayout(3,3, 10, 10)); 20 | jFrame.setSize(300,300); 21 | jFrame.setVisible(true); 22 | } 23 | 24 | public static void main(String[] args) { 25 | new Grid(); 26 | } 27 | } -------------------------------------------------------------------------------- /Swing/Layout/GridBag.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import javax.swing.*; 3 | 4 | public class GridBag extends JFrame{ 5 | GridBagConstraints gbc; 6 | GridBagLayout layout; 7 | 8 | public static void main(String[] args) { 9 | new GridBag(); 10 | } 11 | 12 | public GridBag() { 13 | gbc = new GridBagConstraints(); 14 | 15 | setTitle("GridBagLayout Example"); 16 | layout = new GridBagLayout(); 17 | setLayout(layout); 18 | 19 | gbc.fill = GridBagConstraints.HORIZONTAL; 20 | gbc.gridx = 0;gbc.gridy = 0; 21 | add(new Button("Button One"), gbc); 22 | gbc.gridx = 1;gbc.gridy = 0; 23 | add(new Button("Button two"), gbc); 24 | gbc.ipady = 20; 25 | gbc.gridx = 0;gbc.gridy = 1; 26 | gbc.gridwidth = 2; 27 | 28 | add(new Button("Button three"), gbc); 29 | setSize(300, 300); 30 | setPreferredSize(getSize()); 31 | setVisible(true); 32 | setDefaultCloseOperation(EXIT_ON_CLOSE); 33 | } 34 | } -------------------------------------------------------------------------------- /Swing/List.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class List { 4 | List(){ 5 | JFrame f= new JFrame(); 6 | DefaultListModel l1 = new DefaultListModel<>(); 7 | l1.addElement("ADBMS"); 8 | l1.addElement("IT"); 9 | l1.addElement("JAVA"); 10 | l1.addElement("DBA"); 11 | JList list = new JList<>(l1); 12 | list.setBounds(100,100, 75,75); 13 | f.add(list); 14 | f.setSize(400,400); 15 | f.setLayout(null); 16 | f.setVisible(true); 17 | } 18 | public static void main(String args[]) 19 | { 20 | new List(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Swing/ProgressBar.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class ProgressBar extends JFrame{ 4 | JProgressBar bar; 5 | JLabel label; 6 | 7 | ProgressBar() { 8 | bar = new JProgressBar(0,1000); 9 | bar.setBounds(40,40,160,30); 10 | bar.setValue(0); 11 | bar.setStringPainted(true); 12 | label = new JLabel("Running"); 13 | label.setBounds(80,60,100,50); 14 | add(bar); 15 | add(label); 16 | 17 | setSize(250,200); 18 | setLayout(null); 19 | setVisible(true); 20 | } 21 | 22 | public void iterate() { 23 | int i = 0; 24 | while (i<=1000) { 25 | bar.setValue(i); 26 | i += 1; 27 | try { 28 | Thread.sleep(20); 29 | } catch (Exception e) { 30 | e.printStackTrace(); 31 | } 32 | if (bar.getValue()==1000) 33 | label.setText("Completed"); 34 | } 35 | } 36 | 37 | public static void main(String[] args) { 38 | ProgressBar progress = new ProgressBar(); 39 | progress.iterate(); 40 | } 41 | } -------------------------------------------------------------------------------- /Swing/SimpleInterest.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.*; 4 | 5 | public class SimpleInterest extends JFrame implements ActionListener{ 6 | JTextField principal, rate, time; 7 | JButton calculate; 8 | JLabel interest; 9 | 10 | SimpleInterest() { 11 | GridBagConstraints gbc = new GridBagConstraints(); 12 | setLayout(new GridBagLayout()); 13 | 14 | principal = new JTextField(5); 15 | rate = new JTextField(5); 16 | time = new JTextField(5); 17 | calculate = new JButton("Calculate Interest"); 18 | interest = new JLabel(); 19 | 20 | gbc.gridx = 0;gbc.gridy = 0; 21 | add(new JLabel("Principal"), gbc); 22 | gbc.gridx = 1; 23 | add(principal, gbc); 24 | gbc.gridx = 0;gbc.gridy = 1; 25 | add(new JLabel("Rate"), gbc); 26 | gbc.gridx = 1; 27 | add(rate, gbc); 28 | gbc.gridx = 0;gbc.gridy = 2; 29 | add(new JLabel("Time"), gbc); 30 | gbc.gridx = 1; 31 | add(time, gbc); 32 | gbc.gridx = 0;gbc.gridy = 3;gbc.gridwidth = 2; 33 | add(calculate, gbc); 34 | gbc.gridy = 4; 35 | add(interest, gbc); 36 | 37 | calculate.addActionListener(this); 38 | setTitle("Simple Interest"); 39 | setVisible(true); 40 | setSize(500, 500); 41 | setDefaultCloseOperation(EXIT_ON_CLOSE); 42 | } 43 | 44 | 45 | public void actionPerformed(ActionEvent actionEvent) { 46 | double p = Double.parseDouble(principal.getText()); 47 | double t = Double.parseDouble(time.getText()); 48 | double r = Double.parseDouble(rate.getText()); 49 | double i = (p*t*r)/100; 50 | 51 | interest.setText(Double.toString(i)); 52 | } 53 | 54 | public static void main(String[] args) { 55 | new SimpleInterest(); 56 | } 57 | } -------------------------------------------------------------------------------- /Swing/Slider.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import javax.swing.event.*; 3 | import java.awt.*; 4 | 5 | public class Slider extends JFrame{ 6 | private JPanel panel; 7 | private JSlider slider; 8 | private JLabel value; 9 | 10 | public Slider() { 11 | initUI(); 12 | } 13 | 14 | public void initUI() { 15 | panel = new JPanel(); 16 | 17 | slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 10); 18 | slider.setMajorTickSpacing(20); 19 | slider.setPaintTicks(true); 20 | slider.setPaintLabels(true); 21 | 22 | value = new JLabel(); 23 | value.setText("Value = " + slider.getValue()); 24 | 25 | panel.add(slider, BorderLayout.CENTER); 26 | panel.add(value); 27 | 28 | slider.addChangeListener(new ChangeListener() { 29 | @Override 30 | public void stateChanged(ChangeEvent changeEvent) { 31 | value.setText("Value = " + slider.getValue()); 32 | } 33 | }); 34 | 35 | add(panel); 36 | } 37 | 38 | public static void main(String[] args) { 39 | Slider s = new Slider(); 40 | s.setSize(500,300); 41 | s.setVisible(true); 42 | s.setDefaultCloseOperation(EXIT_ON_CLOSE); 43 | } 44 | } -------------------------------------------------------------------------------- /Swing/Table.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class Table { 4 | JFrame frame; 5 | 6 | Table() { 7 | frame = new JFrame(); 8 | String data[][] = { 9 | {"Queen","Hard"}, 10 | {"Pearl Jam", "Grunge"}, 11 | {"Radiohead", "Alternative"} 12 | }; 13 | String column[] = {"Band", "Genre"}; 14 | 15 | JTable table = new JTable(data,column); 16 | table.setBounds(30,40,200,300); 17 | JScrollPane scrollPane=new JScrollPane(table); 18 | 19 | frame.add(scrollPane); 20 | frame.setSize(300,400); 21 | frame.setVisible(true); 22 | } 23 | 24 | public static void main(String[] args) { 25 | new Table(); 26 | } 27 | } -------------------------------------------------------------------------------- /loop.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello World 4 | 5 | 6 | 7 |

JSP program to print "Hello World" 10 times

8 | 9 | <% 10 | String str = "Hello World"; 11 | for (int i = 0; i < 10; i++) { 12 | out.print("

" + str + "

"); 13 | } 14 | %> 15 | 16 | 17 | --------------------------------------------------------------------------------