Ques)Create a login window for already registered user
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Login extends Frame implements ActionListener {
String message = "";
Label Name, Address, Gender, Email, Uname,Uname1, Pass,Pass1, Hobby;
TextField name, email, uname,uname1, pass,pass1;
TextArea address;
Button b,b1;
Checkbox male, female, other;
Checkbox hobby1, hobby2, hobby3, hobby4, hobby5;
ArrayList<String> un=new ArrayList<String>();
ArrayList<String> p=new ArrayList<String>();
public Login() {
// Set title and frame properties
setTitle("Registration and login Form");
setSize(600, 500);
setLayout(null);
setVisible(true);
// Close window properly
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
// Name
Name = new Label("Name:");
Name.setBounds(20, 50, 100, 30);
name = new TextField();
name.setBounds(160, 50, 150, 30);
// Address
Address = new Label("Address:");
Address.setBounds(20, 90, 100, 30);
address = new TextArea();
address.setBounds(160, 90, 150, 50);
// Gender
Gender = new Label("Gender:");
Gender.setBounds(20, 150, 100, 30);
CheckboxGroup obj = new CheckboxGroup();
male = new Checkbox("Male", obj, false);
male.setBounds(160, 150, 50, 30);
female = new Checkbox("Female", obj, false);
female.setBounds(220, 150, 60, 30);
other = new Checkbox("Other", obj, false);
other.setBounds(290, 150, 60, 30);
// Email
Email = new Label("Email:");
Email.setBounds(20, 190, 100, 30);
email = new TextField();
email.setBounds(160, 190, 150, 30);
// Username
Uname = new Label("Username:");
Uname.setBounds(20, 230, 100, 30);
uname = new TextField();
uname.setBounds(160, 230, 150, 30);
// Password
Pass = new Label("Password:");
Pass.setBounds(20, 270, 100, 30);
pass = new TextField();
pass.setBounds(160, 270, 150, 30);
// Hobbies
Hobby = new Label("Hobbies:");
Hobby.setBounds(20, 310, 100, 30);
hobby1 = new Checkbox("Reading");
hobby1.setBounds(160, 310, 70, 30);
hobby2 = new Checkbox("Coding");
hobby2.setBounds(240, 310, 70, 30);
hobby3 = new Checkbox("Writing");
hobby3.setBounds(320, 310, 70, 30);
hobby4 = new Checkbox("Sports");
hobby4.setBounds(400, 310, 70, 30);
hobby5 = new Checkbox("Music");
hobby5.setBounds(480, 310, 70, 30);
// Username for login
Uname1= new Label("Username:");
Uname1.setBounds(700, 230, 100, 30);
uname1= new TextField();
uname1.setBounds(810, 230, 150, 30);
// Password for login
Pass1= new Label("Password:");
Pass1.setBounds(700, 270, 100, 30);
pass1= new TextField();
pass1.setBounds(810, 270, 150, 30);
// Submit Button
b = new Button("Submit");
b.setBounds(160, 360, 80, 30);
b.addActionListener(this);
// Login Button
b1= new Button("Login");
b1.setBounds(700, 360, 80, 30);
b1.addActionListener(this);
// Adding components to Frame
add(Name);
add(name);
add(Address);
add(address);
add(Gender);
add(male);
add(female);
add(other);
add(Email);
add(email);
add(Uname);
add(uname);
add(Pass);
add(pass);
add(Hobby);
add(hobby1);
add(hobby2);
add(hobby3);
add(hobby4);
add(hobby5);
add(b);
add(Uname1);
add(uname1);
add(Pass1);
add(pass1);
add(b1);
}
// Event handler for button click
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b)
{
String enteredName = name.getText().trim();
String enteredEmail = email.getText().trim();
String enteredUsername = uname.getText().trim();
String enteredPassword = pass.getText().trim();
String enteredAddress = address.getText().trim();
// Validation Checks
if (enteredName.isEmpty() || enteredEmail.isEmpty() || enteredUsername.isEmpty() ||
enteredPassword.isEmpty() || enteredAddress.isEmpty())
{
message = "All fields are required!";
} else if (enteredPassword.length() < 6) {
message = "Password must be at least 6 characters!";
} else {
message = "Registration Successful!";
}
un.add(enteredUsername);
p.add(enteredPassword);
repaint(); // Refresh screen
}
if (e.getSource() == b1)
{
if(((un.size()<0)&&(p.size()<0)) && ((un.size()>4)&&(p.size()>4)))
message="Either no data is present or no of data have exceed";
else
{
for(int i=0;i<un.size();i++)
{
String u1=uname1.getText().trim();
String p1=pass1.getText().trim();
if ((u1.equals(un.get(i))) && (p1.equals(p.get(i))))
{message="all credentials are correct";break;}
else
message="Wrong credentail";
}//for closing
}
repaint();
}
}
// Paint method to display validation message
public void paint(Graphics g) {
super.paint(g); // Clears previous drawings
g.setColor(Color.RED);
g.drawString(message, 20, 450); // Display message below form
}
public static void main(String[] args) {
new Login();
}
}
Output:
Ques) Mail validation program
Code:
import java.util.*;
class Mail
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Email");
String x=sc.nextLine();
int l=x.length();
boolean y=x.endsWith("@gmail.com");
boolean z=x.endsWith("@gmail.co.in");
int i,d=0;
for(i=0;i<l;i++)
{
if(x.charAt(i)=='@')
{
d=i;
break;
}
}
int c=0;
String sub=x.substring(0,d);
for(i=0;i<d;i++)
{
String str="@#$.*";
char ch=sub.charAt(i);
if(str.indexOf(ch)==-1)
{
c=0;
//continue;
}
else
{
c=1;
break;
}
}
char m=x.charAt(0);
if((m>='A'&&m<='Z')||(m>='a'&&m<='z')&&(c==0 && (y || z)))
{
System.out.println("Email valid");
}
else
{
System.out.println("Not valid");
}
}
}
Output:
Ques)Multithreading using thread class
Code:
import java.util.*;
import java.io.*;
class M extends Thread
{
int i;
public void run()
{
try
{
for(i=1;i<=10;i++)
{
System.out.println("Thread 1" +i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}//M class closing
class N extends Thread
{
int i;
public void run()
{
try
{
for(i=1;i<=10;i++)
{
System.out.println("Thread 2" +i);
Thread.sleep(2000);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}//N class closing
class Ps
{
public static void main(String args[])
{
M obj=new M();
obj.start();
N obj1=new N();
obj1.start();
}
}
Output:
Ques)Passward checking using Multithreading.
Code: import java.util.*;
import java.io.*;
class M extends Thread
{
int i;String pass="Xyz@123";
public void run()
{
Scanner sc=new Scanner(System.in);
try
{
System.out.println("Enter username");
String u=sc.next();
for(i=1;i<=3;i++)
{
System.out.println("Attempt "+i);
System.out.println("Enter passward");
Thread.sleep(5000);
String p=sc.next();
if(p.compareTo(pass)==0)
{
System.out.println("Successful attempt");
break;
}
System.out.println("Session out");
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}//M class closing
class Ps1
{
public static void main(String args[])
{
M obj=new M();
obj.start();
}
}
Output: