Viva Answers
Viva Answers
Edit, View and create a submenu under the File menu: New and Open
import java.awt.*;
import java.awt.event.*;
// Create menus
Menu fileMenu = new Menu("File");
Menu editMenu = new Menu("Edit");
Menu viewMenu = new Menu("View");
2-Write a Itree program to show root directory and its subFolders of your System.
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
TreeExample() {
// Initialize tree nodes
root = new DefaultMutableTreeNode("Root");
folder1 = new DefaultMutableTreeNode("Folder 1");
folder2 = new DefaultMutableTreeNode("Folder 2");
file1 = new DefaultMutableTreeNode("File 1.txt");
file2 = new DefaultMutableTreeNode("File 2.txt");
file3 = new DefaultMutableTreeNode("File 3.txt");
// Frame settings
setTitle("Tree Example");
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
3-Write a Java program to create a table of Name of Student, Percentage and Grade of 10 students using
JTable.
import java.awt.*;
import javax.swing.*;
StudentTableExample() {
// Column names for the table
String[] columnNames = {"Name of Student", "Percentage", "Grade"};
// Frame settings
setTitle("Student Table Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
4-Write a Program using JProgressBar to show the progress of Progressbar when user clicks on JButton.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JProgressBar progressBar;
JButton startButton;
ProgressBarExample() {
// Frame properties
setTitle("JProgressBar Example");
setSize(400, 200);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initialize button
startButton = new JButton("Start Progress");
setVisible(true);
}
5-Write a program to generate KeyEvent when a key is pressed and display "Key Pressed" message.
import java.awt.*;
import java.awt.event.*;
Label label;
SimpleKeyEventExample() {
// Frame properties
setTitle("KeyEvent Example");
setSize(300, 200);
setLayout(new BorderLayout());
setDefaultCloseOperation();
// Initialize Label
label = new Label("Type something...");
label.setAlignment(Label.CENTER);
@Override
public void keyPressed(KeyEvent e) {
// Display "Key Pressed" message
label.setText("Key Pressed");
}
@Override
public void keyReleased(KeyEvent e) {
// No action needed
}
@Override
public void keyTyped(KeyEvent e) {
// No action needed
}
6-Write a program using JPasswordField and JTextField to demonstrate the use of user authentication.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
UserAuthentication() {
// Frame properties
setTitle("User Authentication");
setSize(350, 200);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initialize components
userLabel = new JLabel("Username:");
passLabel = new JLabel("Password:");
userField = new JTextField();
passField = new JPasswordField();
loginButton = new JButton("Login");
messageLabel = new JLabel("");
if (username.equals("admin") &&
password.equals("password123")) {
messageLabel.setText("Login Successful!");
messageLabel.setForeground(Color.GREEN);
} else {
messageLabel.setText("Invalid username or
password.");
messageLabel.setForeground(Color.RED);
}
}
});
import java.awt.*;
import java.awt.event.*;
class WindowAdapterExample extends Frame {
WindowAdapterExample() {
// Frame properties
setTitle("WindowAdapter Example");
setSize(400, 300);
setLayout(new FlowLayout());
setVisible(true);
}
8-Develop a program using InetAddress class to retrieve IP address of computer when hostname is
entered by the user.
import java.net.*;
import java.util.Scanner;
try {
// Get the InetAddress object for the hostname
InetAddress inetAddress =
InetAddress.getByName(hostname);
9-Write a program using URL class to retrieve the host, protocol, port and file of URL
http://www.msbte.org.in
import java.net.*;
// Retrieve and display the protocol, host, port, and file from the
URL
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("File: " + url.getFile());
} catch (MalformedURLException e) {
// Handle the case where the URL is invalid
System.out.println("Invalid URL format");
}
}
}
import java.io.*;
import java.net.*;
socket.close();
System.out.println("Client closed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
clientSocket.close();
System.out.println("Server closed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
11-Write a program using DatagramPacket and DatagramSocket to create chat application.
12-Write a Program to create a Student Table in database and insert a record in a Student table.