[go: up one dir, main page]

0% found this document useful (0 votes)
28 views11 pages

A JP Prac Answers

Uploaded by

deepalimore9822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views11 pages

A JP Prac Answers

Uploaded by

deepalimore9822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Develop a Program to illustrate the use of Choice List in an applet:

import java.applet.Applet;import java.awt.*;


public class ChoiceListApplet extends Applet {
public void init() {
Choice choice = new Choice();
choice.add("Option 1");
choice.add("Option 2");
choice.add("Option 3");
add(choice);
}
}

2.Develop a Program to illustrate the use of BorderLayout in an applet.:

import java.applet.Applet;import java.awt.*;


public class BorderLayoutApplet extends Applet {
public void init() {
setLayout(new BorderLayout());
add(new Button("North"), BorderLayout.NORTH);
add(new Button("South"), BorderLayout.SOUTH);
add(new Button("East"), BorderLayout.EAST);
add(new Button("West"), BorderLayout.WEST);
add(new Button("Center"), BorderLayout.CENTER);
}
}

3.Develop a Program to illustrate the use of GridLayout in an applet.:

import java.applet.Applet;import java.awt.*;


public class GridLayoutApplet extends Applet {
public void init() {
setLayout(new GridLayout(2, 2));
add(new Button("Button 1"));
add(new Button("Button 2"));
add(new Button("Button 3"));
add(new Button("Button 4"));
}
}
4.Develop a Program to create three Menus- „Format‟,‟Tool‟, and
„Help‟ and add suitable Menu items under these menus:

import java.awt.*;import java.applet.Applet;


public class MenuApplet extends Applet {
public void init() {
MenuBar menuBar = new MenuBar();
Menu formatMenu = new Menu("Format");
formatMenu.add(new MenuItem("Bold"));
formatMenu.add(new MenuItem("Italic"));
menuBar.add(formatMenu);

Menu toolMenu = new Menu("Tool");


toolMenu.add(new MenuItem("Cut"));
toolMenu.add(new MenuItem("Copy"));
menuBar.add(toolMenu);

Menu helpMenu = new Menu("Help");


helpMenu.add(new MenuItem("About"));
menuBar.add(helpMenu);

setMenuBar(menuBar);
}
}

5.Develop a Program to create an applet to accept a number in a text


field and display the square of the number when button with caption
Square is pressed:

import java.applet.Applet;import java.awt.*;import java.awt.event.*;


public class SquareApplet extends Applet {
TextField inputField;
Button squareButton;

public void init() {


inputField = new TextField(10);
squareButton = new Button("Square");
squareButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num = Integer.parseInt(inputField.getText());
int square = num * num;
showStatus("Square: " + square);
}
});
add(inputField);
add(squareButton);
}
}
6.Develop a Program to create an applet to find factorial of the
number entered in a text field and when button with caption „Find
factorial‟ is pressed:

import java.applet.Applet;import java.awt.*;import java.awt.event.*;


public class FactorialApplet extends Applet {
TextField inputField;
Button factorialButton;

public void init() {


inputField = new TextField(10);
factorialButton = new Button("Find Factorial");
factorialButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num = Integer.parseInt(inputField.getText());
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
showStatus("Factorial: " + factorial);
}
});
add(inputField);
add(factorialButton);
}
}

7.Develop a Program to Print Protocol, port, file of


http://www.msbte.com:

import java.net.*;
public class ProtocolPortFile {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.msbte.com");
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Port: " + url.getPort());
System.out.println("File: " + url.getFile());
}
}
8. Develop a Program to send a password through client to server and
server will send the message to the client that the password is valid or
invalid using UDP.:

Server Code:

import java.net.*;
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
String password = new String(receivePacket.getData(), 0, receivePacket.getLength());
if (password.equals("password123")) {
System.out.println("Valid password");
} else {
System.out.println("Invalid password");
}
socket.close();
}
}

Client Code:

import java.net.*;
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
String password = "password123";
DatagramPacket sendPacket = new DatagramPacket(password.getBytes(), password.length(),
serverAddress, 9876);
socket.send(sendPacket);
socket.close();
}
}

9.Develop a Program to insert a record into the given database table


„StudTable‟.:

import java.sql.*;
public class InsertRecord {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection(“jdbc:ucanaccess://emp.accdb");
Statement stmt = conn.createStatement();
String sql = "INSERT INTO StudTable (rollno, name) VALUES (1, 'John')";
stmt.executeUpdate(sql);
conn.close();
}
}

10.Develop a Program to Change the name of the student as „John‟


whose rollno is 2 in the table „StudTable‟:

import java.sql.*;
public class UpdateRecord {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection(“jdbc:ucanaccess://emp.accdb");
Statement stmt = conn.createStatement();
String sql = "UPDATE StudTable SET name = 'John' WHERE rollno = 2";
stmt.executeUpdate(sql);
conn.close();
}
}

11.Develop a Program to delete the current record.:


import java.sql.*;
public class DeleteRecord {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection(“jdbc:ucanaccess://emp.accdb");
Statement stmt = conn.createStatement();
String sql = "DELETE FROM StudTable WHERE rollno = 2";
stmt.executeUpdate(sql);
conn.close();
}
}

12.Develop a Program to display the whole contents of a database.:


import java.sql.*;
public class DisplayRecords {
public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection(“jdbc:ucanaccess://emp.accdb");
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM StudTable";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("Rollno: " + rs.getInt("rollno") + ", Name: " + rs.getString("name"));
}
conn.close();
}
}
13.Develop a Program having a tabbed panes with button component
added to it. :

import javax.swing.*;
public class TabbedPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Tabbed Pane Example");
JTabbedPane tabbedPane = new JTabbedPane();

JPanel panel1 = new JPanel();


panel1.add(new JButton("Button 1"));
tabbedPane.addTab("Tab 1", panel1);

JPanel panel2 = new JPanel();


panel2.add(new JButton("Button 2"));
tabbedPane.addTab("Tab 2", panel2);

frame.add(tabbedPane);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

14... Develop a Program demonstrating the use of tree component in


swing.:

import javax.swing.*;import javax.swing.tree.*;


public class TreeExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Tree Example");
JTree tree = new JTree(createTreeModel());
frame.add(new JScrollPane(tree));
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static TreeModel createTreeModel() {


DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
root.add(child1);
root.add(child2);
return new DefaultTreeModel(root);
}
}

15.Develop a Program demonstrating the use of table component in


swing. :
import javax.swing.*;import javax.swing.table.DefaultTableModel;
public class TableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Table Example");
String[] columnNames = {"Rollno", "Name"};
Object[][] data = {
{1, "John"},
{2, "Jane"},
{3, "Tom"}
};
JTable table = new JTable(data, columnNames);
frame.add(new JScrollPane(table));
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

16.Develop a program to authenticate the user name and password


using GenericServlet:

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;


public class LoginServlet extends GenericServlet {
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
PrintWriter out = res.getWriter();
if (username.equals("admin") && password.equals("password123")) {
out.println("Login successful");
} else {
out.println("Invalid credentials");
}
}
}

17.Develop a program to verify authenticated user extending Http


Servlet.:

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;


public class AuthServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
PrintWriter out = res.getWriter();
if (username.equals("admin") && password.equals("password123")) {
out.println("Authenticated");
} else {
out.println("Not authenticated");
}
}
}
18.. Demonstrate ActionEvent of Button in AWT.:

import java.awt.*;import java.awt.event.*;


public class ActionEventExample {
public static void main(String[] args) {
Frame frame = new Frame("ActionEvent Example");
Button button = new Button("Click Me");

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});

frame.add(button);
frame.setSize(200, 100);
frame.setVisible(true);
}
}

19.Demonstrate ItemEvent of Choice in AWT:

import java.awt.*;import java.awt.event.*;


public class ItemEventExample {
public static void main(String[] args) {
Frame frame = new Frame("ItemEvent Example");
Choice choice = new Choice();

choice.add("Option 1");
choice.add("Option 2");
choice.add("Option 3");

choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println("Item selected: " + choice.getSelectedItem());
}
});

frame.add(choice);
frame.setSize(200, 100);
frame.setVisible(true);
}
}

20.Develop a Program to create an applet to accept a number in a text


field and display the cube of the number when button with caption
Cube is pressed.:

import java.applet.Applet;import java.awt.*;import java.awt.event.*;


public class CubeApplet extends Applet {
TextField inputField;
Button cubeButton;
public void init() {
inputField = new TextField(10);
cubeButton = new Button("Cube");
cubeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num = Integer.parseInt(inputField.getText());
int cube = num * num * num;
showStatus("Cube: " + cube);
}
});
add(inputField);
add(cubeButton);
}
}

21.Develop a Program to create an applet to accept a radius in a text


field and display the area of circle when button with caption Area is
pressed:

import java.applet.Applet;import java.awt.*;import java.awt.event.*;


public class AreaApplet extends Applet {
TextField radiusField;
Button areaButton;

public void init() {


radiusField = new TextField(10);
areaButton = new Button("Area");
areaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double radius = Double.parseDouble(radiusField.getText());
double area = Math.PI * radius * radius;
showStatus("Area: " + area);
}
});
add(radiusField);
add(areaButton);
}
}

23.Design applet with Breakfast, Lunch, and Dinner choices and total
bill calculation:

import java.applet.Applet;import java.awt.*;import java.awt.event.*;


public class BillApplet extends Applet {
CheckboxGroup meals;
Checkbox breakfast, lunch, dinner;
TextField totalBillField;
Button calculateButton;

public void init() {


meals = new CheckboxGroup();
breakfast = new Checkbox("Breakfast", meals, false);
lunch = new Checkbox("Lunch", meals, false);
dinner = new Checkbox("Dinner", meals, false);
add(breakfast);
add(lunch);
add(dinner);

totalBillField = new TextField(10);


calculateButton = new Button("Calculate Bill");
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int total = 0;
if (breakfast.getState()) total += 50;
if (lunch.getState()) total += 100;
if (dinner.getState()) total += 150;
totalBillField.setText(String.valueOf(total));
}
});
add(totalBillField);
add(calculateButton);
}
}

24.Design an applet with radio buttons using AWT:

import java.applet.Applet;import java.awt.*;


public class RadioButtonApplet extends Applet {
CheckboxGroup choiceGroup;
Checkbox option1, option2, option3;

public void init() {


choiceGroup = new CheckboxGroup();
option1 = new Checkbox("Option 1", choiceGroup, false);
option2 = new Checkbox("Option 2", choiceGroup, false);
option3 = new Checkbox("Option 3", choiceGroup, false);

add(option1);
add(option2);
add(option3);
}
}

25.Design an applet with radio buttons using Swing:


java
Copy code
import javax.swing.*;import java.awt.*;
public class RadioButtonSwingApplet extends JFrame {
public RadioButtonSwingApplet() {
setTitle("Radio Buttons Example in Swing");
setLayout(new FlowLayout());

ButtonGroup group = new ButtonGroup();


JRadioButton rb1 = new JRadioButton("Option 1");
JRadioButton rb2 = new JRadioButton("Option 2");
JRadioButton rb3 = new JRadioButton("Option 3");

group.add(rb1);
group.add(rb2);
group.add(rb3);

add(rb1);
add(rb2);
add(rb3);

setSize(200, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {


new RadioButtonSwingApplet();
}
}

You might also like