import [Link].
*;
import [Link].*;
import [Link].*;
public class QuizForm {
private JFrame frame;
private JPanel panel;
private JLabel question1Label;
private JRadioButton option1Radio;
private JRadioButton option2Radio;
private JRadioButton option3Radio;
private ButtonGroup optionGroup;
private JLabel correctAnswerLabel;
private JCheckBox checkBox1;
private JCheckBox checkBox2;
private JCheckBox checkBox3;
private JTextField textField;
private JTextArea textArea;
private JButton submitButton;
private int totalScore;
public QuizForm() {
frame = new JFrame("Quiz Form");
panel = new JPanel();
[Link](new BoxLayout(panel, BoxLayout.Y_AXIS));
// Question 1: Radio buttons
question1Label = new JLabel("What is the capital of France?");
[Link](question1Label);
option1Radio = new JRadioButton("Paris");
option2Radio = new JRadioButton("London");
option3Radio = new JRadioButton("Berlin");
optionGroup = new ButtonGroup();
[Link](option1Radio);
[Link](option2Radio);
[Link](option3Radio);
[Link](option1Radio);
[Link](option2Radio);
[Link](option3Radio);
// Question 2: Check boxes
JLabel question2Label = new JLabel("What are the colors of the French
flag?");
[Link](question2Label);
checkBox1 = new JCheckBox("Blue");
checkBox2 = new JCheckBox("White");
checkBox3 = new JCheckBox("Red");
[Link](checkBox1);
[Link](checkBox2);
[Link](checkBox3);
// Question 3: Text field
JLabel question3Label = new JLabel("What is the name of the famous
French painter?");
[Link](question3Label);
textField = new JTextField(20);
[Link](textField);
// Question 4: Text area
JLabel question4Label = new JLabel("Write a short essay about the
Eiffel Tower:");
[Link](question4Label);
textArea = new JTextArea(5, 20);
[Link](textArea);
// Submit button
submitButton = new JButton("Submit");
[Link](new SubmitButtonListener());
[Link](submitButton);
correctAnswerLabel = new JLabel("Correct answers will be displayed
here.");
[Link](correctAnswerLabel);
[Link](panel);
[Link]();
[Link](true);
}
private class SubmitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
findTotalScore();
}
}
public void findTotalScore() {
totalScore = 0;
// Question 1: Radio buttons
if ([Link]()) {
totalScore++;
}
// Question 2: Check boxes
if ([Link]() && [Link]() &&
[Link]()) {
totalScore++;
}
// Question 3: Text field
if ([Link]().trim().equalsIgnoreCase("Claude Monet")) {
totalScore++;
}
// Question 4: Text area
if ([Link]().trim().contains("Eiffel Tower")) {
totalScore++;
}
[Link]("Your total score is " + totalScore + " out
of 4.");
}
public static void main(String[] args) {
[Link](new Runnable() {
public void run() {
new QuizForm();
}
});
}
}
output:
2.
import [Link].*;
import [Link].*;
import [Link].*;
public class Calculator extends JFrame implements ActionListener {
// Components of the calculator
private JTextField display;
private JButton[] numberButtons;
private JButton[] functionButtons;
private JButton addButton, subButton, mulButton, divButton;
private JButton equalButton, clearButton, dotButton;
// Variables for calculations
private double num1 = 0, num2 = 0, result = 0;
private char operator;
// Constructor for Calculator
public Calculator() {
// Set window title
setTitle("Calculator");
// Set default size of window
setSize(300, 400);
// Set default location of window
setLocationRelativeTo(null);
// Set window to be not resizable
setResizable(false);
// Set default close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create display field
display = new JTextField();
// Set display field to be non-editable
[Link](false);
// Set display field to be centered
[Link]([Link]);
// Add display field to the calculator
add(display, [Link]);
// Create number buttons
numberButtons = new JButton[10];
JPanel numberPanel = new JPanel(new GridLayout(4, 3, 5, 5));
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton([Link](i));
numberButtons[i].addActionListener(this);
[Link](numberButtons[i]);
}
// Create dot button
dotButton = new JButton(".");
[Link](this);
[Link](dotButton);
// Add number panel to the calculator
add(numberPanel, [Link]);
// Create function buttons
functionButtons = new JButton[4];
JPanel functionPanel = new JPanel(new GridLayout(4, 1, 5, 5));
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
for (JButton button : functionButtons) {
[Link](this);
[Link](button);
}
// Add function panel to the calculator
add(functionPanel, [Link]);
// Create equal and clear buttons
equalButton = new JButton("=");
clearButton = new JButton("C");
[Link](this);
[Link](this);
JPanel equalClearPanel = new JPanel(new GridLayout(1, 2, 5, 5));
[Link](equalButton);
[Link](clearButton);
// Add equal and clear panel to the calculator
add(equalClearPanel, [Link]);
// Make the calculator visible
setVisible(true);
}
// Method to handle button clicks
public void actionPerformed(ActionEvent e) {
// Get the source of the event
JButton button = (JButton) [Link]();
// Check if the button is a number button
for (int i = 0; i < 10; i++) {
if (button == numberButtons[i]) {
[Link]([Link]() + i);
return;
}
}
// Check if the button is the dot button
if (button == dotButton) {
if (![Link]().contains(".")) {
[Link]([Link]() + ".");
}
return;
}
// Check if the button is an operator button
if (button == addButton) {
operator = '+';
} else if (button == subButton) {
operator = '-';
} else if (button == mulButton) {
operator = '*';
} else if (button == divButton) {
operator = '/';
}
// Check if the button is the equal button
if (button == equalButton) {
num2 = [Link]([Link]());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
[Link]([Link](result));
num1 = 0;
num2 = 0;
return;
}
// Check if the button is the clear button
if (button == clearButton) {
[Link]("");
num1 = 0;
num2 = 0;
return;
}
// Set num1 to the current display value
num1 = [Link]([Link]());
// Clear the display
[Link]("");
}
// Main method to start the calculator
public static void main(String[] args) {
new Calculator();
}
}
output:
3.
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
public class GoogleAccountRegistration extends JFrame implements
ActionListener {
private JTextField firstNameField, lastNameField, usernameField,
phoneField, recoveryEmailField, dayField, yearField, genderField;
private JPasswordField passwordField, confirmPasswordField;
private JComboBox monthBox;
private JButton createAccountButton, resetButton, cancelButton;
public GoogleAccountRegistration() {
super("Create Google Account");
setSize(400, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(10, 2));
setLocationRelativeTo(null);
// Labels and Text Fields
JLabel firstNameLabel = new JLabel("First Name:");
firstNameField = new JTextField();
JLabel lastNameLabel = new JLabel("Last Name:");
lastNameField = new JTextField();
JLabel usernameLabel = new JLabel("Username:");
usernameField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
JLabel confirmPasswordLabel = new JLabel("Confirm Password:");
confirmPasswordField = new JPasswordField();
JLabel phoneLabel = new JLabel("Phone Number:");
phoneField = new JTextField();
JLabel recoveryEmailLabel = new JLabel("Recovery Email:");
recoveryEmailField = new JTextField();
JLabel monthLabel = new JLabel("Month:");
String[] months = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
monthBox = new JComboBox(months);
JLabel dayLabel = new JLabel("Day:");
dayField = new JTextField();
JLabel yearLabel = new JLabel("Year:");
yearField = new JTextField();
JLabel genderLabel = new JLabel("Gender:");
genderField = new JTextField();
// Buttons
createAccountButton = new JButton("Create Account");
[Link](this);
resetButton = new JButton("Reset");
[Link](this);
cancelButton = new JButton(" Cancel");
[Link](this);
// Add components to the frame
add(firstNameLabel);
add(firstNameField);
add(lastNameLabel);
add(lastNameField);
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(confirmPasswordLabel);
add(confirmPasswordField);
add(phoneLabel);
add(phoneField);
add(recoveryEmailLabel);
add(recoveryEmailField);
add(monthLabel);
add(monthBox);
add(dayLabel);
add(dayField);
add(yearLabel);
add(yearField);
add(genderLabel);
add(genderField);
add(createAccountButton);
add(resetButton);
add(cancelButton);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if ([Link]() == createAccountButton) {
// Validate input fields
if (validateFields()) {
// Create account logic here
[Link](this, "Account created
successfully!");
}
} else if ([Link]() == resetButton) {
// Reset fields
resetFields();
} else if ([Link]() == cancelButton) {
// Close the window
dispose();
}
}
private boolean validateFields() {
// Validate first name
if ([Link]().trim().isEmpty()) {
[Link](this, "First name is required!");
return false;
}
// Validate last name
if ([Link]().trim().isEmpty()) {
[Link](this, "Last name is required!");
return false;
}
// Validate username
if ([Link]().trim().isEmpty()) {
[Link](this, "Username is required!");
return false;
}
// Validate password
if ([Link]().length == 0) {
[Link](this, "Password is required!");
return false;
}
// Validate confirm password
if (!
[Link]([Link]()).equals([Link](confirmPassw
[Link]()))) {
[Link](this, "Passwords do not match!");
return false;
}
// Validate phone number
if ([Link]().trim().isEmpty()) {
[Link](this, "Phone number is required!");
return false;
}
// Validate recovery email
if ([Link]().trim().isEmpty()) {
[Link](this, "Recovery email is
required!");
return false;
}
// Validate birthdate
if ([Link]().trim().isEmpty() ||
[Link]().trim().isEmpty()) {
[Link](this, "Birthdate is required!");
return false;
}
// Validate gender
if ([Link]().trim().isEmpty()) {
[Link](this, "Gender is required!");
return false;
}
// Validate phone number format
String phoneRegex = "^\\d{10}$";
Pattern phonePattern = [Link](phoneRegex);
Matcher phoneMatcher = [Link]([Link]());
if (![Link]()) {
[Link](this, "Invalid phone number
format!");
return false;
}
return true;
}
private void resetFields() {
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
}
public static void main(String[] args) {
new GoogleAccountRegistration();
}
}
output:
import [Link].*;
import [Link].*;
import [Link].*;
public class QuizForm {
private JFrame frame;
private JPanel panel;
private JLabel question1Label;
private JRadioButton option1Radio;
private JRadioButton option2Radio;
private JRadioButton option3Radio;
private JRadioButton option4Radio;
private ButtonGroup optionGroup;
private JLabel correctAnswerLabel;
private JCheckBox checkBox1;
private JCheckBox checkBox2;
private JCheckBox checkBox3;
private JCheckBox checkBox4;
private JCheckBox checkBox5;
private JTextField textField;
private JTextArea textArea;
private JButton submitButton;
private int totalScore;
public QuizForm() {
frame = new JFrame("Quiz Form");
panel = new JPanel();
[Link](new BoxLayout(panel, BoxLayout.Y_AXIS));
// Question 1: Radio buttons
question1Label = new JLabel("Which pacakge is used for swing
components?");
[Link](question1Label);
option1Radio = new JRadioButton("[Link]");
option2Radio = new JRadioButton("[Link]");
option3Radio = new JRadioButton("javafx");
option4Radio = new JRadioButton("[Link]");
optionGroup = new ButtonGroup();
[Link](option1Radio);
[Link](option2Radio);
[Link](option3Radio);
[Link](option4Radio);
[Link](option1Radio);
[Link](option2Radio);
[Link](option3Radio);
[Link](option4Radio);
// Question 2: Check boxes
JLabel question2Label = new JLabel("Which of the following are layout
manners?");
[Link](question2Label);
checkBox1 = new JCheckBox("border layout");
checkBox2 = new JCheckBox("flow layout");
checkBox3 = new JCheckBox("grid layout");
checkBox4 = new JCheckBox("flow layout");
checkBox5 = new JCheckBox("grid layout");
[Link](checkBox1);
[Link](checkBox2);
[Link](checkBox3);
[Link](checkBox4);
[Link](checkBox5);
// Question 3: Text field
JLabel question3Label = new JLabel("What is the name of the famous
French painter?");
[Link](question3Label);
textField = new JTextField(20);
[Link](textField);
// Question 4: Text area
JLabel question4Label = new JLabel("Write a short essay about the
Eiffel Tower:");
[Link](question4Label);
textArea = new JTextArea(5, 20);
[Link](textArea);
// Submit button
submitButton = new JButton("Submit");
[Link](new SubmitButtonListener());
[Link](submitButton);
correctAnswerLabel = new JLabel("Correct answers will be displayed
here.");
[Link](correctAnswerLabel);
[Link](panel);
[Link]();
[Link](true);
}
private class SubmitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
findTotalScore();
}
}
public void findTotalScore() {
totalScore = 0;
// Question 1: Radio buttons
if ([Link]()) {
totalScore++;
}
// Question 2: Check boxes
if ([Link]() && [Link]() &&
[Link]()) {
totalScore++;
}
// Question 3: Text field
if ([Link]().trim().equalsIgnoreCase("Claude Monet")) {
totalScore++;
}
// Question 4: Text area
if ([Link]().trim().contains("Eiffel Tower")) {
totalScore++;
}
[Link]("Your total score is " + totalScore + " out
of 4.");
}
public static void main(String[] args) {
[Link](new Runnable() {
public void run() {
new QuizForm();
}
});
}
}