AOT Practical File
AOT Practical File
Step 1: First, create the file as ShapeApplet.java and enter the code into it.
Step 2: Once the code is ready we need to create the ShapeApplet.html file, through which we will
display the output.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Shape Applet</title>
</head>
<body>
</applet>
</body>
</html>
Step 3: Then we need to compile the Java code using the below command:
javac ShapeApplet.java
Step 4: Now finally we need to run the application using the below command:
appletviewer ShapeApplet.html
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
setLayout(null);
add(shapeInput);
// creating button
drawButton.setForeground(Color.WHITE);
drawButton.addActionListener(this);
add(drawButton);
if (e.getSource() == drawButton) {
shapeName = shapeInput.getText().toLowerCase();
drawShape = true;
repaint();
centerX = getWidth() / 2;
centerY = getHeight() / 2;
g.setColor(Color.BLACK);
if (drawShape) {
switch (shapeName) {
// creating circle
case "circle":
// creating rectangle
case "rectangle":
break;
// creating triangle
case "triangle":
break;
// creating oval
case "oval":
break;
// creating square
case "square":
break;
// creating pentagon
case "pentagon":
int[] pentagonX = {centerX, centerX + 75, centerX + 47, centerX - 47, centerX - 75};
int[] pentagonY = {centerY - 75, centerY - 25, centerY + 58, centerY + 58, centerY - 25};
break;
default:
Output
Step 1: First, create the file as SApplet.java and enter the code into it.
Step 2: Once the code is been inserted then we need to create the SApplet.html
file, through which we will display the output.
<!DOCTYPE html>
<html>
<head>
<body>
No Support
</applet>
</body>
</html>
Step 3: Then we need to compile the Java code using the below command:
javac SApplet.java
Step 4: Now finally we need to run the application using the below power:
appletviewer SApplet.html
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JButton resBtn;
res.setEditable(false);
resBtn.addActionListener(this);
l1.setForeground(Color.black);
l2.setForeground(Color.black);
l3.setForeground(Color.black);
pan.add(l1);
pan.add(num1);
pan.add(l2);
pan.add(num2);
// Set button background color
resBtn.setBackground(Color.orange);
pan.add(resBtn);
pan.add(new JLabel());
pan.add(l3);
pan.add(res);
// components
pan.setForeground(Color.black);
l1.setFont(inFont);
l2.setFont(inFont);
l3.setFont(inFont);
num1.setFont(inFont);
num2.setFont(inFont);
res.setFont(inFont);
resBtn.setFont(inFont);
setLayout(new BorderLayout());
add(pan, BorderLayout.CENTER);
if (e.getSource() == resBtn) {
try {
res.setText(Integer.toString(result));
res.setText("Wrong Input");
}
Output
1. import javax.swing.*;
3. JFrame f;
4. TableExample(){
5. f=new JFrame();
7. {"102","Jai","780000"},
8. {"101","Sachin","700000"}};
9. String column[]={"ID","NAME","SALARY"};
11. jt.setBounds(30,40,200,300);
13. f.add(sp);
14. f.setSize(300,400);
15. f.setVisible(true);
16. }
17. public static void main(String[] args) {
19. }
20. }
Output:
Java JTree
1. import javax.swing.*;
2. import javax.swing.tree.DefaultMutableTreeNode;
3. public class TreeExample {
4. JFrame f;
5. TreeExample(){
6. f=new JFrame();
7. DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");
8. DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");
9. DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");
10. style.add(color);
11. style.add(font);
12. DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");
13. DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");
14. DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");
15. DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");
16. color.add(red); color.add(blue); color.add(black); color.add(green);
17. JTree jt=new JTree(style);
18. f.add(jt);
19. f.setSize(200,200);
20. f.setVisible(true);
21. }
22. public static void main(String[] args) {
23. new TreeExample();
24. }}
Output:
5. Write a Java program to demonstrate an application involving GUI with controls menus and
event handling using Swings
Program to add a menubar and add menuitems, submenu items and also add ActionListener to
menu items
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
// menubar
static JMenuBar mb;
// JMenu
// Menu items
// create a frame
static JFrame f;
// a label
static JLabel l;
// main class
// create a frame
// create a label
// create a menubar
mb = new JMenuBar();
// create a menu
x = new JMenu("Menu");
x1 = new JMenu("submenu");
// create menuitems
m1 = new JMenuItem("MenuItem1");
m2 = new JMenuItem("MenuItem2");
m3 = new JMenuItem("MenuItem3");
s1 = new JMenuItem("SubMenuItem1");
s2 = new JMenuItem("SubMenuItem2");
m1.addActionListener(m);
m2.addActionListener(m);
m3.addActionListener(m);
s1.addActionListener(m);
s2.addActionListener(m);
x.add(m1);
x.add(m2);
x.add(m3);
x1.add(s1);
x1.add(s2);
// add submenu
x.add(x1);
mb.add(x);
f.setJMenuBar(mb);
// add label
f.add(l);
f.setSize(500, 500);
f.setVisible(true);
String s = e.getActionCommand();
Output:
6. Create a simple calculator using javascript.
Build.html
1. <!-- Create a simple Program to build the Calculator in JavaScript using with HTML and CSS w
eb languages. -->
2. <!DOCTYPE html>
4. <head>
6.
7. <style>
8. h1 {
9. text-align: center;
13. }
14.
15. #clear{
21. }
22.
23. .formstyle
24. {
31. }
32.
33.
34.
35. input
36. {
45. }
46.
47.
48. #calc{
54. }
55.
56. </style>
57.
58. </head>
59. <body>
60. <h1> Calculator Program in JavaScript </h1>
63.
64. <!-- This input box shows the button pressed by the user in calculator. -->
67. <!-- onclick() function display the number prsses by the user. -->
68. <input type = "button" value = "1" onclick = "form1.answer.value += '1' ">
69. <input type = "button" value = "2" onclick = "form1.answer.value += '2' ">
70. <input type = "button" value = "3" onclick = "form1.answer.value += '3' ">
71. <input type = "button" value = "+" onclick = "form1.answer.value += '+' ">
73.
74. <input type = "button" value = "4" onclick = "form1.answer.value += '4' ">
75. <input type = "button" value = "5" onclick = "form1.answer.value += '5' ">
76. <input type = "button" value = "6" onclick = "form1.answer.value += '6' ">
77. <input type = "button" value = "-" onclick = "form1.answer.value += '-' ">
79.
80. <input type = "button" value = "7" onclick = "form1.answer.value += '7' ">
81. <input type = "button" value = "8" onclick = "form1.answer.value += '8' ">
82. <input type = "button" value = "9" onclick = "form1.answer.value += '9' ">
83. <input type = "button" value = "*" onclick = "form1.answer.value += '*' ">
85.
86.
87. <input type = "button" value = "/" onclick = "form1.answer.value += '/' ">
88. <input type = "button" value = "0" onclick = "form1.answer.value += '0' ">
89. <input type = "button" value = "." onclick = "form1.answer.value += '.' ">
90. <!-- When we click on the '=' button, the onclick() shows the sum results on the calculator
screen. -->
92. <br>
93. <!-- Display the Cancel button and erase all data entered by the user. -->
94. <input type = "button" value = "Clear All" onclick = "form1.answer.value = ' ' " id= "clear" >
95. <br>
96.
97. </form>
98. </div>
99. </body>
100. </html>
Output
7. Write a program for a generic servelet
import java.io.*;
import javax.servlet.*;
ServletResponse resp)
resp.setContentType("text/html");
PrintWriter pw = resp.getWriter();
pw.println("<html>");
pw.println("<body>");
pw.println("</body>");
pw.println("</html>");
pw.close();
Under the root folder, a subfolder is required with the name WEB-INF.
Under WEB-INF two subfolders are required called classes and lib.
All .class files including servlet’s .class file are placed inside classes folder.
All image, html, .js, jsp, etc files are placed inside the application root folder.
Also under the WEB-INF folder, we need to place the web.xml file
<web-app>
<servlet>
<servlet-class>MyFirstServlet</servlet-class>
<servlet-name>MyFirstServlet</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/MyFirstServlet</url-pattern>
</servlet-mapping>
</web-app>
Output:
8. Write a program to create a HttpServlet to read the parameters and run application with
the help of Tomcat server.