Java Cat 2
Java Cat 2
class PasswordStrength {
String password;
void checkStrength() {
boolean hasUpper = false, hasLower = false, hasDigit = false,
hasSpecial = false;
if (password.length() >= 8) {
for (char ch : password.toCharArray()) {
if (Character.isUpperCase(ch)) hasUpper = true;
else if (Character.isLowerCase(ch)) hasLower = true;
else if (Character.isDigit(ch)) hasDigit = true;
else hasSpecial = true;
}
obj.setPassword(input);
obj.checkStrength();
sc.close();
}
}
📘 Explanation:
✅ 2. Username Validator
📌 Java Code:
import java.util.Scanner;
class Username {
String name;
void validate() {
boolean isValid = true;
if (name.length() < 6) {
System.out.println("Username must be at least 6 characters
long.");
isValid = false;
} else if (!Character.isLetter(name.charAt(0))) {
System.out.println("Username must start with a letter.");
isValid = false;
} else {
for (int i = 0; i < name.length(); i++) {
char ch = name.charAt(i);
if (!Character.isLetterOrDigit(ch)) {
System.out.println("Username can only contain letters
and digits.");
isValid = false;
break;
}
}
}
if (isValid) {
System.out.println("Username is valid.");
}
}
}
user.setUsername(input);
user.validate();
sc.close();
}
}
📘 Explanation:
📌 Java Code:
import java.util.Scanner;
import java.sql.*;
class FoodOrderJDBC {
int total = 0;
void displayMenu() {
System.out.println("Menu:");
System.out.println("1. Pizza - $10");
System.out.println("2. Burger - $5");
System.out.println("3. Coke - $2");
}
void saveToDatabase() {
try {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/fooddb", "root",
"yourpassword"
);
conn.close();
System.out.println("Order saved to database.");
} catch (Exception e) {
e.printStackTrace();
}
}
void showBill() {
System.out.println("Total bill: $" + total);
}
}
order.displayMenu();
order.takeOrder(sc);
order.showBill();
order.saveToDatabase();
sc.close();
}
}
📘 Explanation:
📌 Java Code:
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class Student {
String name, email, course;
int age;
conn.close();
System.out.println("Student registered in database.");
} catch (Exception e) {
e.printStackTrace();
}
}
void printDetails() {
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Email : " + email);
System.out.println("Course : " + course);
}
}
frame.setSize(350, 250);
frame.setLayout(null);
frame.setVisible(true);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String email = emailField.getText();
String course = courseField.getText();
JOptionPane.showMessageDialog(frame, "Submitted
Successfully!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Error: " +
ex.getMessage());
}
}
});
}
}
📘 Explanation:
GUI Setup:
o JTextField and JButton capture input.
o Layout is absolute (setBounds()).
Student class:
o setDetails() stores form data.
o printDetails() prints to console.
o saveToDatabase() connects to studentdb and inserts the form data.
submitButton.addActionListener():
o Triggered when "Submit" is clicked.
o Retrieves form values, sets data, saves to DB, and shows a popup.