Java Basics Code and Output
Java Basics Code and Output
Code:
import [Link];
public class Operations {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter first number: ");
int n1 = [Link]();
[Link]("Enter second number: ");
int n2 = [Link]();
int sum = n1 + n2;
int difference = n1 - n2;
int product = n1 * n2;
int quotient = n1 / n2;
int modulo = n1 % n2;
Output:
2. Write program to perform all the arithmetic operations given in the table
Code:
import [Link];
public class TableOperations {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter first number= ");
int n1 = [Link]();
[Link]("Enter second number= ");
int n2 = [Link]();
int sum = n1 + n2;
int difference = n1 - n2;
int product = n1 * n2;
int quotient = n1 / n2;
int increment = ++n1;
int decrement = --n1;
Output:
If Condition
1. Write program to check if candidate is eligible for voting or not
Code:
import [Link];
public class Eligible {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter Age= ");
int n1 = [Link]();
if(n1>=18) {
[Link]("Candidate is eligible to vote");
}else {
[Link]("Candidate is not eligible to vote");
}
}
}
Output:
Code:
import [Link];
public class CheckNumber {
public static void main(String[] args) {
Scanner in =new Scanner([Link]);
[Link]("Enter number= ");
int n1 = [Link]();
if(n1 >= 0) {
[Link]("Number is Positive");
}else {
[Link]("Number is Negative");
}
}
}
Output:
3. Extend the previous program to check whether the given number is positive, zero
or negative
Code:
import [Link];
public class CheckZero {
public static void main(String[] args) {
Scanner in =new Scanner([Link]);
[Link]("Enter number= ");
int n1 = [Link]();
if(n1 > 0) {
[Link]("Number is Positive");
}else if(n1 == 0) {
[Link]("Number is Zero");
}else {
[Link]("Number is Negative");
}
}
}
Output:
Code:
import [Link];
public class Largest {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter first number= ");
int n1 = [Link]();
[Link]("Enter second number= ");
int n2 = [Link]();
if (n1 > n2) {
[Link](+n1 +" is largest number.");
}
else {
[Link](+n2+ " is largest number.");
}
}
}
Output:
5. Write a program to check given number is even or odd.
Code:
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner in =new Scanner([Link]);
[Link]("Enter number= ");
int n1 = [Link]();
if(n1 % 2== 0) {
[Link]("Number is Even");
}
else {
[Link]("Number is Odd");
}
}
}
Output:
For Loop
1. Write a program to print 10 even numbers and 10 odd numbers.
Code:
public class ForEvenOdd {
public static void main(String[] args) {
[Link]("Even Numbers: ");
for(int i = 0; i < 20; i++) {
if (i%2 == 0) {
[Link](i+", ");
}
}
[Link]("\n");
[Link]("Odd Numbers: ");
for(int i = 0; i < 20; i++) {
if (i%2 != 0) {
[Link](i+", ");
}
}
}
}
Output:
Code:
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number= ");
int input = [Link]();
int i, factorial = 1;
for(i=1; i<=input; i++) {
factorial = factorial * i;
}
[Link]("The factorial of " + input + " is " + factorial);
}
}
Output:
3. rite a program to generate tables of 10.
Code:
public class Table {
public static void main(String[] args) {
int num = 10;
int result;
for(int i=1;i<=10;i++) {
result = num * i;
[Link]("10 * " + i + " = " + result);
}
}
}
Output:
Code:
public class AddDigits {
public static void main(String[] args) {
int num = 1234;
int sum = 0;
for (int temp = num; temp > 0; temp = temp / 10) {
int digit = temp % 10;
sum += digit;
}
[Link]("Sum of digits of " + num + " is " + sum);
}
}
Output:
5. Write a program to reverse the digits of a number
Code:
public class Reverse {
public static void main(String[] args) {
int num = 2463;
int rev = 0;
for (int temp = num; temp > 0; temp = temp / 10) {
int digit = temp % 10;
rev = rev * 10 + digit;
}
[Link]("Original number: " + num);
[Link]("Reversed number: " + rev);
}
}
Output:
Code:
public class Fibonacci {
public static void main(String[] args) {
int x=0, y=1;
int z;
[Link](x);
[Link](" "+y+" ");
for(int i=2; i<10;i++) {
z = x + y;
[Link](z+" ");
x = y;
y = z;
}
}
}
Output:
Whille Loop
1. Write a program to print 10 even numbers and 10 odd numbers.
Code:
public class WhileEvenOdd {
public static void main(String[] args) {
int num = 0;
[Link]("even");
while (num < 20) {
num ++;
if(num%2 == 0) {
[Link](num+" ");
}
}
[Link]("\n");
[Link]("odd");
num = 0;
while (num < 20) {
num ++;
if(num%2 != 0) {
[Link](num+" ");
}
}
}
}
Output:
Code:
import [Link];
public class WhileFactorial {
public static void main(String[] args) {
int fact = 1;
Scanner sc = new Scanner([Link]);
[Link]("Enter the Number= ");
int num = [Link]();
int i = 1;
while(i<=num) {
fact = fact * i;
i++;
}
[Link]("The factorial of " + num + " is " + fact);
}
}
Output:
Code:
public class WhileAddDigits {
public static void main(String[] args) {
int num = 2321;
int sum = 0;
int temp = num;
while(temp>0) {
int digit = temp % 10;
sum = sum + digit;
temp = temp / 10;
}
[Link]("The sum of "+num+" is: "+sum);
}
}
Output:
Code:
public static void main(String[] args) {
int x = 0, y = 1;
int z, count = 10;
int i = 0;
while (i<count) {
[Link](x + " ");
z = x + y;
x = y;
y = z;
i++;
}
}
}
Output:
DO Whille Loop
1. Write a program to print 10 even numbers and 10 odd numbers.
Code:
public class DoOddEven {
public static void main(String[] args) {
int num = 0;
[Link]("Even: ");
do {
num ++;
if(num%2 == 0) {
[Link](num+" ");
}
}while (num < 20);
num = 0;
[Link]("\n");
[Link]("Odd: ");
do {
num ++;
if(num%2 != 0) {
[Link](num+" ");
}
}while (num < 20);
}
}
Output:
Code:
import [Link];
public class DoAddDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number: ");
int number = [Link]();
int sum = 0, i = number;
do {
int temp = i % 10;
sum += temp;
i /= 10;
}
while (i!=0);
[Link]("The sum of the digits " + number + " is " + sum);
}
}
Output:
Code:
public class DoFibonacci {
public static void main(String[] args) {
int x = 0, y = 1, z, count = 10, i= 0;
do {
[Link](x+" ");
z = x + y;
x = y;
y = z;
i++;
}
while(i<count);
}
}
Output:
Case Study 1
An Amusement park company wants one application for their billing counter to
enable ticket sale. Assume the Amusement park authorities approached Max to get
this application developed. This application should have ticket prize as Rs 400 per
person and if a person buys more than 10 tickets then person is eligible for 10 percent
discount. Calculate the total bill or amount according to the number of tickets that
are sold.
Code:
import [Link];
public class AmusementPark {
public static void main(String[] args) {
int ticketPrice = 400;
Scanner sc = new Scanner([Link]);
[Link]("How many Tickets would you like to buy? ");
int numberOfTicketBought = [Link]();
double totalPrice = 0;
if(numberOfTicketBought >= 10 ) {
double totalPricewithoutDiscount = numberOfTicketBought * ticketPrice;
double discountPercent = (1 - 0.1);
totalPrice = totalPricewithoutDiscount * discountPercent;
double discountAmount = totalPricewithoutDiscount - totalPrice;
[Link]("You saved Rs " + discountAmount + ". ");
}else {
totalPrice = numberOfTicketBought * ticketPrice;
}
[Link]("Your total amount is Rs " + totalPrice );
}
}
Output:
Case Study 2
John and Paul went to watch a movie in theatre where they need to buy two tickets.
There are two types of tickets, one Golden category and other as silver category. If
they buy tickets for silver category, then per person a ticket should cost Rs.150 and
for golden category ticket should cost them Rs.200 each. Considering this scenario,
write a program for theatre ticket booking application scenario.
Code:
import [Link];
public class MovieCase {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]
("Enter 1 to buy Gold Category Ticket pricing Rs 200"
+ "\nEnter 2 to buy Silver Category Ticket pricing Rs 150! ");
[Link]("Enter your choice");
int choice = [Link]();
int price = 0;
switch(choice) {
case 1:
price = 200;
break;
case 2:
price = 150;
break;
default:
[Link]("Invalid CHOICE");
}
[Link]("The total price is " + price*2+" for 2 people");
}
}
Output:
1. Write a program to accept 5 employee IDs and the corresponding names and their
salaries from the user and store them in three arrays. Pass these arrays to a function
display() as arguments. This display() will display the content of the arrays in the
following format.
Code:
import [Link].*;
public class Employees {
static void display(String[] id,String[] name,double[] salary) {
[Link]("Displaying Employee Details:\n");
[Link]("ID | Name | Salary");
for(int i=0;i<[Link];i++) {
[Link](id[i]+" | "+name[i]+" | "+salary[i]);
[Link]();
}}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String empId[] = new String[5];
String empName[]= new String[5];
double empSalary[]= new double[5];
for(int i=0;i<5;i++) {
[Link]("Enter the ID of Employee "+(i+1)+": ");
empId[i] = [Link]();
[Link]();
[Link]("Enter the Name of Employee "+(i+1)+": ");
empName[i] = [Link]();
[Link]("Enter the Salary of Employee "+(i+1)+": ");
empSalary[i] = [Link]();
[Link]();
}
display(empId,empName,empSalary);
}}
Output:
2. Write another function display() with Employee ID array and Employee name
array as arguments. (Note: here we are using concept of function overloading). This
function will display the content of the 2 arrays in the following format.
Code:
import [Link].*;
public class Employees2 {
static void display(String[] id,String[] name,double[] salary) {
[Link]("Displaying Employee Details:\n");
[Link]("ID | Name | Salary");
for(int i=0;i<[Link];i++) {
[Link](id[i]+" | "+name[i]+" | "+salary[i]);
[Link]();
}
}
static void display(String[] id,String[] name) {
[Link]("\nDisplaying Employee Details:\n");
[Link]("ID | Name ");
for(int i=0;i<[Link];i++) {
[Link](id[i]+" | "+name[i]);
[Link]();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String empId[] = new String[5];
String empName[]= new String[5];
double empSalary[]= new double[5];
for(int i=0;i<5;i++) {
[Link]("Enter the ID of Employee "+(i+1)+": ");
empId[i] = [Link]();
[Link]();
[Link]("Enter the Name of Employee "+(i+1)+": ");
empName[i] = [Link]();
[Link]("Enter the Salary of Employee "+(i+1)+": ");
empSalary[i] = [Link]();
[Link]();
}
display(empId,empName,empSalary);
display(empId,empName);
}
}
Output:
3. Write another function named display() which takes 4 arguments. The arguments
are named as String and 3 arrays (Employee id, name and salary). Function
prototype looks like: display (String name, int regno[], String Empname[], double
salary[]). This function will search for the name in the Empname array and will
display its corresponding id and salary in the below given format. For example, if
Divya is given as the name to search then display () function will display the following
record.
Code:
import [Link].*;
public class Employees3 {
static void display(String[] id,String[] name,double[] salary) {
[Link]("Displaying Employee Details:\n");
[Link]("ID | Name | Salary");
for(int i=0;i<[Link];i++) {
[Link](id[i]+" | "+name[i]+" | "+salary[i]);
[Link]();
}
}
static void display(String[] id,String[] name) {
[Link]("\nDisplaying Employee Details:\n");
[Link]("ID | Name ");
for(int i=0;i<[Link];i++) {
[Link](id[i]+" | "+name[i]);
[Link]();
}
}
static void display(String[] id, String[] name, double[] salary, String searchName) {
boolean found = false;
for(int i=0;i<[Link];i++) {
if([Link](name[i])) {
[Link]("\nDisplaying Employee Details: "+searchName+" ");
[Link]("ID | Name | Salary");
[Link](id[i]+" | "+name[i]+" | "+salary[i]);
found = true;
}
}
if(!found){
[Link]("\n\n*!!!Name Not Found: "+searchName+" is not available.!!!");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String empId[] = new String[5];
String empName[]= new String[5];
double empSalary[]= new double[5];
for(int i=0;i<5;i++) {
[Link]("Enter the ID of Employee "+(i+1)+": ");
empId[i] = [Link]();
[Link]();
[Link]("Enter the Name of Employee "+(i+1)+": ");
empName[i] = [Link]();
[Link]("Enter the Salary of Employee "+(i+1)+": ");
empSalary[i] = [Link]();
[Link]();
}
display(empId,empName,empSalary);
display(empId,empName);
display(empId,empName,empSalary,"Seeya");
}
}
Output:
Case Study
Consider a class named phone which have functionalities like make a call, receive a
call and messaging. Based on this scenario John wants to develop an application
which will have class named Mobile and methods like dial, receive and message which
will demonstrate the functioning of these methods. Use a reference object to call these
methods(dial, receive and message and display)
Code:
public class Mobile{
void dial(String num) {
[Link]("Calling..."+num);
}
void recieve(String num) {
[Link]("Receiving Call from "+num);
}
void recieveMessage(String msg, String num) {
[Link]("Received Message: '"+msg+"' from "+num);
}
void sendMessage(String msg, String num) {
[Link]("Sending Message: '"+msg+"' to "+num);
}
public static void main(String[] args) {
Mobile dipamMobile = new Mobile();
[Link]("9819210391");
[Link]("9813046401");
[Link]("Hello Zeeya", "9810233685");
[Link]("Message", "9810233685");
}
}
Output:
1. Write classes to hold Account, SB-Account and Current-Account details. The
common properties of the account are Account number, name and amount. Specifics
of SB account is 4% interest to be paid per month.
a) Implement the run-time polymorphism by creating base class variable and derived
class object.
b) Ask the user for which type of account to be created then create the corresponding
account.
c) Implement function overriding by having deposit and withdraw functions and
perform the required action accordingly.
Ensure base class can’t be instantiated.
2. Define the minimum balance for the both the type of accounts. Use final keyword to
create constants.
Code:
import [Link];
public class AccountMod {
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
Account a;
[Link]("Enter Account Number:");
int accNo = [Link]();
[Link]();
[Link]("Enter Account Holder Name:");
String name = [Link]();
[Link]("Choose Account Type:\n1. SB\n2. Current");
int accType = [Link]();
if(accType == 1) {
a = new SBAccount(accNo, name);
} else {
a = new CurrentAccount(accNo, name);
}
[Link]("Enter initial deposit amount:");
double initAmount = [Link]();
[Link](initAmount);
[Link]("\nChoose Operation:\n1. Deposit\n2. Withdraw");
[Link]("Enter");
int choice = [Link]();
[Link]("Enter amount:");
double amount = [Link]();
if(choice == 1) {
[Link](amount);
} else if(choice == 2) {
[Link](amount);
} else {
[Link]("Invalid choice!");
}
[Link]();
}
}
abstract class Account {
int accountNo;
String name;
double bankBalance;
final double MIN_BALANCE = 5000.0;
Account(int accNo, String name) {
[Link] = accNo;
[Link] = name;
}
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
final class SBAccount extends Account {
final double INTEREST = 0.04;
SBAccount(int accNo, String name) {
super(accNo, name);
}
@Override
public void deposit(double amount) {
bankBalance += amount;
addInterest();
[Link]("\nDeposit successful");
[Link]("Balance: " + bankBalance);
}
@Override
public void withdraw(double amount) {
if(bankBalance >= amount + MIN_BALANCE) {
bankBalance -= amount;
[Link]("\nWithdraw successful");
[Link]("Balance: " + bankBalance);
} else {
[Link]("\nMinimum balance must be maintained!");
}
}
public void addInterest() {
bankBalance += bankBalance * INTEREST;
[Link]("Interest added");
}
}
final class CurrentAccount extends Account {
CurrentAccount(int accNo, String name) {
super(accNo, name);
}
@Override
public void deposit(double amount) {
bankBalance += amount;
[Link]("\nDeposit successful");
[Link]("Balance: " + bankBalance);
}
@Override
public void withdraw(double amount) {
if(bankBalance >= amount + MIN_BALANCE) {
bankBalance -= amount;
[Link]("\nWithdraw successful");
[Link]("Balance: " + bankBalance);
} else {
[Link]("\nMinimum balance must be maintained!");
}
}
}
Output:
Interfaces
Write a program to define a queue interface and have insert and delete methods in
the interface. Implement these methods in a class.
Code:
import [Link];
class QueueNode {
int data;
QueueNode next;
public QueueNode(int data) {
[Link] = data;
[Link] = null;
}
}
interface QueueFirst {
void insert(QueueNode qi);
void delete();
}
class Queue implements QueueFirst {
QueueNode front, rear;
public Queue() {
front = null;
rear = null;
}
@Override
public void insert(QueueNode qi) {
if (front == null) {
front = qi;
rear = qi;
} else {
[Link] = qi;
rear = qi;
}
[Link]("Inserted successfully");
}
@Override
public void delete() {
if (front == null) {
[Link]("Queue is empty");
return;
}
[Link]("Deleted: " + [Link]);
front = [Link];
if (front == null) {
rear = null;
}
}
public void display() {
if (front == null) {
[Link]("Queue is empty");
return;
}
QueueNode temp = front;
[Link]("Queue elements:");
while (temp != null) {
[Link]([Link] + " ");
temp = [Link];
}
[Link]();
}
}
public class QueueInterface {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Queue queue = new Queue();
boolean flag = true;
while (flag) {
[Link]("\n1. Insert");
[Link]("2. Delete");
[Link]("3. Display");
[Link]("0. Exit");
[Link]("Enter choice: ");
try {
int choice = [Link]([Link]());
switch (choice) {
case 1:
[Link]("Enter integer to insert: ");
int data = [Link]([Link]());
[Link](new QueueNode(data));
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 0:
flag = false;
break;
default:
[Link]("Invalid choice");
}
} catch (Exception e) {
[Link]("Invalid input");
}
}
[Link]();
}
}
Output:
Packages
Write a program to define functions for subtract, multiply, divide, factorial and
reversing the digits of a number in a package, import this class in another package
and use all the methods defined in the primary package.
Code:
package LabFourArithmetic;
public class ArithmeticProblems {
public static double subtract(double a, double b) {
return a - b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double divide(double a, double b) {
return a / b;
}
public static int factorial(int n) {
int fact = 1;
for(int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
public static int reverse(int n) {
int rev = 0;
while(n > 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
return rev;
}
}
package LabFour;
import [Link];
import [Link];
public class MathematicalTask {
public static void main(String[] args){
boolean flag = true;
Scanner scanner = new Scanner([Link]);
while (flag){
[Link]("\n1. Subtract");
[Link]("2. Multiply");
[Link]("3. Divide");
[Link]("4. Factorial");
[Link]("5. Reverse digits");
[Link]("0. Exit");
[Link]("Enter your choice: ");
try {
int choice = [Link]([Link]());
switch (choice) {
case 0 -> flag = false;
case 1 -> {
double a = [Link]([Link]());
double b = [Link]([Link]());
[Link](a + " - " + b + " = " +
[Link](a, b));
}
case 2 -> {
double a = [Link]([Link]());
double b = [Link]([Link]());
[Link](a + " * " + b + " = " +
[Link](a, b));
}
case 3 -> {
double a = [Link]([Link]());
double b = [Link]([Link]());
if (b == 0)
[Link]("Divide by zero error");
else
[Link](a + " / " + b + " = " +
[Link](a, b));
}
case 4 -> {
int a = [Link]([Link]());
if (a < 0)
[Link]("Factorial not allowed");
else
[Link](a + "! = " +
[Link](a));
}
case 5 -> {
int a = [Link]([Link]());
if (a < 0)
[Link]("Reverse not allowed");
else
[Link]("Reverse = " +
[Link](a));
}
}
} catch (Exception e) {
[Link]("Invalid input");
}
}
}
}
Output:
Exception
Write a program to demonstrate ArrayIndexOutOfBoundsException.
Code:
public class ArrayOutBound {
public static void main(String[] args) {
int[] arr = {10, 20, 30};
try {
for(int i=0;i<5;i++) {
[Link](arr[i]+" ");
}
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Exception caught: Array index is out of range!");
}
[Link]("Program continues...");
}
}
Output:
Thread
Write a program to print tables of 5 by creating a new thread and display 20 even
numbers as a task of main thread.
Code:
class TableThread extends Thread {
public void run() {
[Link]("Table of 5 (Child Thread):");
for (int i = 1; i <= 10; i++) {
[Link]("5 x " + i + " = " + (5 * i));
try {
[Link](500);
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
public class ThreadLab {
public static void main(String[] args) {
TableThread t = new TableThread();
[Link]();
[Link]("Even Numbers (Main Thread):");
int count = 0;
int num = 2;
while (count < 20) {
[Link](num);
num += 2;
count++;
try {
[Link](300);
} catch (InterruptedException e) {
[Link](e);
}
}
}
}
Output:
Layout Management
Write a program to demonstrate Layout Management: No Layout, Flow layout,
Border Layout, Grid Layout, Grid bag Layout and Group Layout.
No Layout Code:
package LayoutManagement;
import [Link].*;
public class NoLayout {
public static void main(String[] args) {
JFrame frame = new JFrame("No Layout");
[Link](400, 200);
[Link](null);
JLabel label = new JLabel("Hello, I am Zeeya Shrestha");
[Link](80, 70, 350, 30);
[Link](label);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
Output:
Output:
Border Layout Code:
package LayoutManagement;
import [Link].*;
import [Link].*;
public class BorderLay {
public static void main(String[] args) {
JFrame f = new JFrame("Zeeya");
[Link](new JButton("NavBar"), [Link]);
[Link](new JButton("Footer"), [Link]);
[Link](new JButton("Advertisement"), [Link]);
[Link](new JButton("SideBar"), [Link]);
[Link](new JButton("Content"), [Link]);
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE); //optional
[Link](true);
}
}
Output:
package LayoutManagement;
import [Link].*;
import [Link].*;
public class GridLay{
public static void main(String[] args) {
JFrame f = new JFrame("Zeeya");
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 3");
JButton b4 = new JButton("Button 4");
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](300, 300);
[Link](new GridLayout(2, 2, 10, 10));
[Link](true);
}
}
Output:
package LayoutManagement;
import [Link].*;
import [Link].*;
public class GridBagLay{
public static void main(String[] args) {
JFrame f = new JFrame("Zeeya");
GridBagConstraints gbc = new GridBagConstraints();
[Link](new GridBagLayout());
[Link] = [Link];
[Link] = 0;
[Link] = 0;
[Link](new Button("Button One"), gbc);
[Link] = 1;
[Link] = 0;
[Link](new Button("Button two"), gbc);
[Link] = [Link];
[Link] = 20;
[Link] = 0;
[Link] = 1;
[Link](new Button("Button Three"), gbc);
[Link] = 1;
[Link] = 1;
[Link](new Button("Button Four"), gbc);
[Link] = 0;
[Link] = 2;
[Link] = [Link];
[Link] = 2;
[Link](new Button("Button Five"), gbc);
[Link](300, 300);
[Link](true);
}
}
Output:
package LayoutManagement;
import [Link].*;
public class GroupLay{
public static void main(String[] args) {
JFrame frame = new JFrame("Zeeya");
JPanel panel = new JPanel();
JButton button = new JButton("Click here");
GroupLayout layout = new GroupLayout(panel);
[Link](layout);
[Link](true);
[Link]([Link]().addComponent(button));
[Link]([Link]().addComponent(button));
[Link](panel);
[Link](300, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
Output:
GUI Controls
Write a program to demonstrate GUI Controls: Text Fields, Password Fields, Text
Areas, Scroll Pane, Labels, Check Boxes, Radio Buttons, Borders, Combo Boxes,
Sliders.
Code:
package LabGUIControls;
import [Link].*;
import [Link].*;
import [Link].*;
public class GUIControls extends JFrame {
public GUIControls() {
setTitle("GUI Controls Demo");
setSize(500, 550);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
[Link](null);
[Link](10, 10, 460, 490);
[Link]([Link](
[Link]([Link]),
"Student Registration Form"
));
JLabel lblName = new JLabel("Full Name:");
JLabel lblPassword = new JLabel("Password:");
JLabel lblAddress = new JLabel("Address:");
JLabel lblFaculty = new JLabel("Faculty:");
JLabel lblGender = new JLabel("Gender:");
JLabel lblHobby = new JLabel("Hobbies:");
JLabel lblSlider = new JLabel("Satisfaction:");
JTextField txtName = new JTextField();
JPasswordField txtPassword = new JPasswordField();
JTextArea txtAddress = new JTextArea();
JScrollPane scroll = new JScrollPane(txtAddress);
String faculties[] = {"Select", "Science", "Management", "Humanities"};
JComboBox<String> comboFaculty = new JComboBox<>(faculties);
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
[Link](male);
[Link](female);
JCheckBox chkMusic = new JCheckBox("Music");
JCheckBox chkSports = new JCheckBox("Sports");
JSlider slider = new JSlider(0, 100, 50);
[Link](20);
[Link](5);
[Link](true);
[Link](true);
JButton btnSubmit = new JButton("Submit");
JButton btnReset = new JButton("Reset");
[Link](20, 30, 120, 25);
[Link](150, 30, 200, 25);
[Link](20, 70, 120, 25);
[Link](150, 70, 200, 25);
[Link](20, 110, 120, 25);
[Link](150, 110, 200, 60);
[Link](20, 190, 120, 25);
[Link](150, 190, 200, 25);
[Link](20, 230, 120, 25);
[Link](150, 230, 70, 25);
[Link](230, 230, 80, 25);
[Link](20, 270, 120, 25);
[Link](150, 270, 80, 25);
[Link](230, 270, 100, 25);
[Link](20, 310, 120, 25);
[Link](150, 310, 200, 50);
[Link](150, 380, 90, 30);
[Link](260, 380, 90, 30);
[Link](lblName); [Link](txtName);
[Link](lblPassword); [Link](txtPassword);
[Link](lblAddress); [Link](scroll);
[Link](lblFaculty); [Link](comboFaculty);
[Link](lblGender); [Link](male); [Link](female);
[Link](lblHobby); [Link](chkMusic); [Link](chkSports);
[Link](lblSlider); [Link](slider);
[Link](btnSubmit); [Link](btnReset);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new GUIControls();
}
}
Output:
Menu
Write a program to demonstrate Menu using Swing.
Code:
package LabGUIControls;
import [Link].*;
import [Link].*;
public class SwingMenu extends JFrame {
JMenuBar menuBar;
JMenu menuRecordOp, menuReport, menuHelp;
JMenuItem mItemAddRecord, mItemEditRecord, mItemDeleteRecord, mItemExitRecord;
JMenuItem mItemAll, mItemIndv;
JMenuItem mItemAbout;
public SwingMenu() {
setTitle("Simple Menu Demo");
menuBar = new JMenuBar();
menuRecordOp = new JMenu("Record Operation");
menuReport = new JMenu("Report");
menuHelp = new JMenu("Help");
mItemAddRecord = new JMenuItem("Add Record");
mItemEditRecord = new JMenuItem("Edit Record");
mItemDeleteRecord = new JMenuItem("Delete Record");
mItemExitRecord = new JMenuItem("Exit");
mItemAll = new JMenuItem("All Record");
mItemIndv = new JMenuItem("Search Record");
mItemAbout = new JMenuItem("About Application");
[Link](menuRecordOp);
[Link](menuReport);
[Link](menuHelp);
[Link](mItemAddRecord);
[Link](mItemEditRecord);
[Link](mItemDeleteRecord);
[Link](mItemExitRecord);
[Link](mItemAll);
[Link](mItemIndv);
[Link](mItemAbout);
setJMenuBar(menuBar);
setSize(600, 400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // center screen
setVisible(true);
}
public static void main(String[] args) {
new SwingMenu();
}
}
Output:
Advance GUI
Write a program to demonstrate: Option Dialogs, Creating Dialogs, File Choosers,
Color Choosers, Internal Frames, Frames, Tables, Trees, and Tables.
Code:
package LabGUIControls;
import [Link].*;
import [Link];
import [Link];
import [Link].*;
public class AdvanceGUI extends JFrame {
JDesktopPane desktop;
public AdvanceGUI() {
setTitle("Advanced GUI Components Demo");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
desktop = new JDesktopPane();
add(desktop);
JMenuBar bar = new JMenuBar();
JMenu menuDialog = new JMenu("Dialogs");
JMenu menuComponents = new JMenu("Components");
JMenuItem optDialog = new JMenuItem("Option Dialog");
JMenuItem customDialog = new JMenuItem("Custom Dialog");
JMenuItem fileChooser = new JMenuItem("File Chooser");
JMenuItem colorChooser = new JMenuItem("Color Chooser");
JMenuItem tableItem = new JMenuItem("Table");
JMenuItem treeItem = new JMenuItem("Tree");
[Link](optDialog);
[Link](customDialog);
[Link](fileChooser);
[Link](colorChooser);
[Link](tableItem);
[Link](treeItem);
[Link](menuDialog);
[Link](menuComponents);
setJMenuBar(bar);
[Link](e -> {
[Link](this, "Hello from Option Dialog!");
});
[Link](e -> {
JDialog dialog = new JDialog(this, "Custom Dialog", true);
[Link](300, 200);
[Link](new FlowLayout());
[Link](new JLabel("This is a custom dialog"));
[Link](new JButton("OK"));
[Link](this);
[Link](true);
});
[Link](e -> {
JFileChooser fc = new JFileChooser();
[Link](this);
});
[Link](e -> {
Color c = [Link](this, "Pick Color", [Link]);
getContentPane().setBackground(c);
});
[Link](e -> {
JInternalFrame frame = new JInternalFrame("Table", true, true, true, true);
String col[] = {"ID", "Name", "Faculty"};
String data[][] = {
{"1", "Zeeya", "Management"
+ ""},
{"2", "Dipam", "Science"},
{"3", "Aarshi", "Humanities"}
};
JTable table = new JTable(new DefaultTableModel(data, col));
[Link](new JScrollPane(table));
[Link](300, 200);
[Link](true);
[Link](frame);
});
[Link](e -> {
JInternalFrame frame = new JInternalFrame("Tree", true, true, true, true);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("College");
DefaultMutableTreeNode science = new DefaultMutableTreeNode("Science");
[Link](new DefaultMutableTreeNode("BScCSIT"));
[Link](new DefaultMutableTreeNode("MBBS"));
DefaultMutableTreeNode management = new DefaultMutableTreeNode("Management");
[Link](new DefaultMutableTreeNode("BCA"));
[Link](new DefaultMutableTreeNode("BBA"
+ ""));
[Link](science);
[Link](management);
JTree tree = new JTree(root);
[Link](new JScrollPane(tree));
[Link](300, 300);
[Link](true);
[Link](frame);
});
setVisible(true);
}
public static void main(String[] args) {
new AdvanceGUI();
}
}
Output:
Event Handling:
Write a program to demonstrate Key Events.
Code:
package EventLab;
import [Link].*;
import [Link].*;
import [Link].*;
public class KeyboardEvent implements KeyListener {
JFrame f;
JTextField input;
JLabel lblPressed, lblReleased, lblTyped;
int count = 0;
KeyboardEvent() {
f = new JFrame("KeyListener - All Methods");
input = new JTextField();
[Link](50, 30, 200, 30);
lblPressed = new JLabel("Pressed: ");
[Link](50, 80, 300, 25);
lblReleased = new JLabel("Released: ");
[Link](50, 110, 300, 25);
lblTyped = new JLabel("Typed Count: 0");
[Link](50, 140, 300, 25);
[Link](this);
[Link](input);
[Link](lblPressed);
[Link](lblReleased);
[Link](lblTyped);
[Link](350, 250);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
public void keyPressed(KeyEvent e) {
char key = [Link]();
[Link]("Pressed: " + key);
if (key == 'r') {
[Link]().setBackground([Link]);
}
}
public void keyReleased(KeyEvent e) {
char key = [Link]();
[Link]("Released: " + key);
}
public void keyTyped(KeyEvent e) {
count++;
[Link]("Typed Count: " + count);
}
public static void main(String[] args) {
new KeyboardEvent();
}
}
Output:
Code:
package EventLab;
import [Link].*;
import [Link];
import [Link].*;
public class MouseEvent implements MouseListener, MouseMotionListener {
JFrame f;
JLabel status;
MouseEvent() {
f = new JFrame("Mouse Events Demo");
status = new JLabel("Perform mouse actions here");
[Link](50, 80, 300, 30);
[Link](status);
[Link](this);
[Link](this);
[Link](400, 250);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
@Override
public void mouseDragged([Link] e) {
[Link]("Dragging at (" + [Link]() + ", " + [Link]() + ")");
}
@Override
public void mouseMoved([Link] e) {
[Link]("Moving at (" + [Link]() + ", " + [Link]() + ")");
}
@Override
public void mouseClicked([Link] e) {
[Link]("Clicked at (" + [Link]() + ", " + [Link]() + ")");
}
@Override
public void mousePressed([Link] e) {
[Link]().setBackground([Link]);
[Link]("Mouse Pressed");
}
@Override
public void mouseReleased([Link] e) {
[Link]().setBackground([Link]);
[Link]("Mouse Released");
}
@Override
public void mouseEntered([Link] e) {
[Link]("Mouse Entered");
}
@Override
public void mouseExited([Link] e) {
[Link]("Mouse Exited");
}
public static void main(String[] args) {
new MouseEvent();
}
}
Output:
Write a Program to demonstrate Simple Action Listener
Code:
package EventLab;
import [Link].*;
import [Link].*;
public class ActionListenerExamp implements ActionListener {
JFrame f;
JButton btn;
JLabel label;
ActionListenerExamp() {
f = new JFrame("ActionListener Demo");
btn = new JButton("Click Here");
[Link](100, 50, 120, 30);
label = new JLabel("Result will appear here");
[Link](80, 100, 200, 30);
[Link](this);
[Link](btn);
[Link](label);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked!");
}
public static void main(String[] args) {
new ActionListenerExamp();
}
}
Output:
JDBC
Write programs to create/insert/update/delete/select student table in the db. Student
table will
have the following fields:
Student ID
Name
Class
Marks
Creation of Table in MySQL WorkBench
CRUD Operation:
package Javadatabase;
import [Link].*;
import [Link];
public class CRUDop {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
Output:Insert
Update
Select
Delete
Write a program to perform Batch processing and Transaction management for
Student table
created in Problem 1.
Code:
package Javadatabase;
import [Link].*;
import [Link];
public class BatchQuest {
public static void main(String[] args) {
try {
DBConnection db = new DBConnection();
Connection con = [Link];
[Link](false);
Statement st = [Link]();
[Link]("INSERT INTO student VALUES (701, 'Manshi', 'ECA', 76)");
[Link]("INSERT INTO student VALUES (702, 'Arshi', 'CSIT', 98)");
[Link]("INSERT INTO student VALUES (703, 'Zeeya', 'BIT', 75)");
int[] result = [Link]();
[Link]();
[Link]("Batch executed successfully!");
ResultSet rs = [Link]("SELECT * FROM student");
[Link]("\n--- Student Records ---");
[Link]("ID\tName\tClass\tMarks");
while ([Link]()) {
[Link](
[Link]("id") + "\t" +
[Link]("name") + "\t" +
[Link]("class") + "\t" +
[Link]("marks"));
}
} catch (Exception e) {
[Link]();
}
}
}
Output
Transaction Management
Code:
package Javadatabase;
import [Link].*;
public class TransactionManag{
public static void main(String[] args) {
Connection con = null;
try {
DBConnection db = new DBConnection();
con = [Link];
[Link](false);
PreparedStatement ps = [Link](
"INSERT INTO student VALUES (?, ?, ?, ?)");
[Link](1, 501);
[Link](2, "Dipam");
[Link](3, "BCA");
[Link](4, 95);
[Link]();
[Link](1, 502);
[Link](2, "Yuyu");
[Link](3, "BIM");
[Link](4, 45);
[Link]();
[Link]();
[Link]("Transaction Successful!");
} catch (Exception e) {
try {
[Link]("Error! Error! Error! Rolling back...");
if (con != null)
[Link]();
} catch (Exception ex) {
[Link]();
}
}
}
}
Output
Network Programming
Write a Java program to implement client-server communication using TCP sockets where
the client sends messages to the server until the message ‘Over’ is entered.
ServerProgram:
import [Link].*;
import [Link].*;
public class ServerProgram {
private Socket socket =null;
private ServerSocket server = null;
private DataInputStream in =null;
public ServerProgram(int port) {
try {
server=new ServerSocket(port);
[Link]("Server started");
[Link]("Waiting for a client...");
socket=[Link]();
[Link]("Client accepted");
in=new DataInputStream(new
BufferedInputStream([Link]()));
String line ="";
while () {
try {
line=[Link]();
[Link](line);
}catch(IOException i) {
[Link](i);
}
}
[Link]("Closing connection");
[Link]();
[Link]();
}
catch (IOException i) {
[Link](i);
// TODO: handle exception
}
}
public static void main(String[] args) {
ServerProgram server = new ServerProgram(7000);
}
}
ClientProgram
import [Link].*;
import [Link].*;
public class ClientProgram {
private Socket socket=null;
private DataInputStream input = null;
private DataOutputStream out= null;
public ClientProgram (String address,int port) {
try {
socket= new Socket(address,port);
[Link]("Connected");
input = new DataInputStream ([Link]);
out=new DataOutputStream([Link]());
}
catch(UnknownHostException u) {
[Link](u);
}
catch(IOException i) {
[Link](i);
}
String line ="";
while(){
try {
line = [Link]();
[Link](line);
}
catch(IOException i){
[Link](i);
}
}
try {
[Link]();
[Link]();
[Link]();
}
catch(IOException i) {
[Link](i);
}
}
public static void main(String[] args) {
ClientProgram client = new ClientProgram("[Link]",7000);
}
}
Output
GUI with JavaFX
Write a program to demonstrate BorderPane, Controls, FlowPane, GridPane, Label
TextField.
Code:
BorderPane:
package JavaFX;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class BorderPaneFX extends Application {
@Override
public void start(Stage stage) {
BorderPane pane = new BorderPane();
[Link](new Button("Top"));
[Link](new Button("Bottom"));
[Link](new Button("Left"));
[Link](new Button("Right"));
[Link](new Button("Center"));
Scene scene = new Scene(pane, 300, 200);
[Link](scene);
[Link]("BorderPane Example");
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
Output
Controls
Code:
package JavaFX;
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class ControlFX extends Application {
public void start(Stage stage) {
Button btn = new Button("Click Me");
RadioButton rb = new RadioButton("Dipam the Great");
CheckBox cb = new CheckBox("Like ME");
VBox vbox = new VBox(10, btn, rb, cb);
[Link](new Scene(vbox, 200, 150));
[Link]("Button, RadioButton, CheckBox");
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
Output
FlowPane
Code:
package JavaFX;
import [Link];
import [Link]; // This is important
import [Link];
import [Link];
import [Link];
public class FlowPaneFX extends Application {
@Override
public void start(Stage primaryStage) {
FlowPane flowPane = new FlowPane(10, 10);
for (int i = 1; i <= 3; i++) {
[Link]().add(new Button("Dipam Button " + i));
}
Scene scene = new Scene(flowPane, 300, 200);
[Link]("FlowPane Example");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
Output
Grid Pane
Code:
package JavaFX;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class GridPaneFX extends Application {
public void start(Stage stage) {
GridPane grid = new GridPane();
[Link](new Button("Dip1"), 0, 0);
[Link](new Button("Dip2"), 1, 0);
[Link](new Button("Dip3"), 0, 1);
[Link](new Button("Dip4"), 1, 1);
[Link](new Scene(grid, 200, 150));
[Link]("GridPane");
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
Output
LabelTextField
Code:
package JavaFX;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class LabelFX extends Application {
public void start(Stage stage) {
Label label = new Label("DipamName:");
TextField tf = new TextField();
VBox vbox = new VBox(10, label, tf);
[Link](new Scene(vbox, 200, 100));
[Link]("Label and TextField");
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
Output
Servlets and Java Server Pages
Develop a Java web application using Servlets and JSP to perform user registration
and login with database connectivity, handling form data, sessions, and HTTP
requests (GET/POST) and display information.
Code:
Login Servlet:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
[Link]().append("Served at: ").append([Link]());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
try {
PreparedStatement pstmt;
ResultSet rs = [Link]();
if([Link]()) {
HttpSession session = [Link]();
[Link]("reg_no", reg_no);
[Link]("DisplayServlet");
}else {
RequestDispatcher rd=[Link]("[Link]");
[Link](request,response);
}
}catch(Exception ex) {
[Link]();
}
}
}
Register Servlet:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RegisterServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
[Link]().append("Served at: ").append([Link]());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String registration = [Link]("reg_no");
String fullName = [Link]("full_name");
String faculty = [Link]("faculty");
String semester = [Link]("semester");
String address = [Link]("address");
String number = [Link]("phone");
String gender = [Link]("gender");
String email = [Link]("email");
String semClear = [Link]("semestatus");
if(semClear==null) {
semClear = "No";
}
try {
PreparedStatement pstmt;
DBConnection con = new DBConnection();
pstmt = [Link]("insert into STUDENT_INFO values
(?,?,?,?,?,?,?,?,?)");
[Link](1,registration);
[Link](2,fullName);
[Link](3,faculty);
[Link](4,semester);
[Link](5,address);
[Link](6, number);
[Link](7, gender);
[Link](8, email);
[Link](9, semClear);
int result = [Link]();
if(result>0) {
[Link]("[Link]");
}else {
RequestDispatcher rd=[Link]("[Link]");
[Link](request,response);
}
}catch(Exception ex) {
[Link]();
}
}
}
Display Servlet
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];
@WebServlet("/DisplayServlet")
public class DisplayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DisplayServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
[Link]().append("Served at: ").append([Link]());
try {
HttpSession session = [Link]();
String reg_no = (String)[Link]("reg_no");
DBConnection con = new DBConnection();
PreparedStatement ps = [Link](
"SELECT * FROM student_info WHERE reg_no=?");
[Link](1, reg_no);
ResultSet rs = [Link]();
if ([Link]()) {
[Link]("name", [Link]("full_name"));
[Link]("faculty", [Link]("faculty"));
[Link]("email", [Link]("email"));
[Link]("semester", [Link]("semester"));
[Link]("semestatus", [Link]("semestatus"));
[Link]("gender", [Link]("gender"));
[Link]("address", [Link]("address"));
[Link]("phone", [Link]("phone"));
}
RequestDispatcher rd = [Link]("[Link]");
[Link](request, response);
}catch(Exception ex) {
[Link]();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
[Link]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String user=(String)[Link]("reg_no");
if(user == null){
[Link]("[Link]");
}
%>
<h1>welcome home</h1>
<h2>Your Details</h2>
<div>
<span>Registration Number: <%=user %></span>
<br>
<span>Name: ${name}</span>
<br>
<span>Faculty: ${faculty}</span>
<br>
<span>Semester: ${semester}</span>
<br>
<span>Address: ${address}</span>
<br>
<span>Phone: ${phone}</span>
<br>
<span>Gender: ${gender}</span>
<br>
<span>Email: ${email}</span>
<br>
<span>Semester Cleared: ${semestatus}</span>
</div>
<form method="post" action="[Link]">
<input type="submit" value="LogOut">
</form>
</body>
</html>
[Link]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="LoginServlet" >
<div class="container">
<label for="email"><b>Email</b></label>
<input type="email" placeholder="youremail@[Link]" name="email" required>
<br><br>
<label for="reg_no"><b>Registration Number</b></label>
<input type="text" placeholder="X-X-XXX-XX-XXXX" name="reg_no" required>
<br><br>
<button type="submit">Login</button>
<label>
<br><br>
<input type="checkbox" checked="checked" name="remember"> Remember me
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<br><br>
<span class="psw">Forgot <a href="#">password?</a></span>
<br><br>
<span><a href="[Link]">Register New Student</a></span>
</div>
</form>
</body>
</html>
[Link]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add new User</title>
</head>
<body>
<form action="RegisterServlet" method="post">
Reg No:
<input type="text" name="reg_no" placeholder="X-X-XXX-XX-XXXX" required ><br><br>
Full Name:
<input type="text" name="full_name" placeholder = "Your Name" required><br><br>
Faculty:
<select name="faculty" required>
<option value="">--Select Faculty--</option>
<option value="Science">Science</option>
<option value="Humanities">Humanities</option>
<option value="Management">Management</option>
</select><br><br>
Semester:
<select name="semester" required>
<option value="">--Select Semester--</option>
<option value="I">I</option>
<option value="II">II</option>
<option value="III">III</option>
<option value="IV">IV</option>
<option value="V">V</option>
<option value="VI">VI</option>
<option value="VII">VII</option>
<option value="VIII">VIII</option>
</select><br><br>
Address:
<input type="text" name="address" placeholder="Maitidevi, Kathmandu" required><br><br>
Phone:
<input type="text" name="phone" placeholder="98XXXXXXXX"><br><br>
Gender:
<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<input type="radio" name="gender" value="Other"> Other
<br><br>
Email:
<input type="email" name="email" placeholder="youremail@[Link]" required><br><br>
Semester Status:
<input type="checkbox" name="semestatus" value="Yes"> Sem Cleared
<br><br>
<input type="submit" value="Register">
<br><br>
<span>Already Registered? <a href="[Link]">Login</a></span>
</form>
</body>
</html>
Output