0% found this document useful (0 votes)
5 views

Java functions and loops

The document outlines commonly used Java functions such as toString(), length(), and substring(), providing examples of their usage. It also presents a real-life application problem, specifically a Password Strength Checker program that verifies if a password meets certain criteria. The program checks for length, character types, and special characters to determine password strength.

Uploaded by

fahimferdoush26
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java functions and loops

The document outlines commonly used Java functions such as toString(), length(), and substring(), providing examples of their usage. It also presents a real-life application problem, specifically a Password Strength Checker program that verifies if a password meets certain criteria. The program checks for length, character types, and special characters to determine password strength.

Uploaded by

fahimferdoush26
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Commonly Used Java Functions & Real-Life Application Problems

1. Commonly Used Java Functions

toString()
Converts an object to a string representation.

Integer num = 10;


System.out.println(num.toString()); // Output: "10"

length()
Returns the length of a string or array.

String text = "Hello";


System.out.println(text.length()); // Output: 5

charAt(index)
Returns the character at a given index.

String word = "Java";


System.out.println(word.charAt(2)); // Output: "v"

substring(start, end)
Extracts part of a string.

String text = "Programming";


System.out.println(text.substring(3, 7)); // Output: "gram"

equals()
Compares two strings for equality.
String str1 = "Hello";
String str2 = "hello";
System.out.println(str1.equals(str2)); // Output: false
equalsIgnoreCase()
Compares two strings, ignoring case differences.

System.out.println(str1.equalsIgnoreCase(str2)); // Output: true

toUpperCase() / toLowerCase()
Converts a string to uppercase or lowercase.

System.out.println(str1.toUpperCase()); // Output: "HELLO"

replace(oldChar, newChar)
Replaces characters in a string.

String sentence = "I like Java";


System.out.println(sentence.replace("Java", "Python")); // Output: "I like Python"

trim()
Removes leading and trailing spaces.

String spacedText = " Hello World ";


System.out.println(spacedText.trim()); // Output: "Hello World"

split(delimiter)
Splits a string into an array based on a delimiter.

String csv = "apple,banana,grape";


String[] fruits = csv.split(",");
System.out.println(fruits[1]); // Output: "banana"

parseInt() / parseDouble()
Converts a string into an integer or double.

String number = "123";


int num = Integer.parseInt(number);
System.out.println(num * 2); // Output: 246
Math.max(a, b) / Math.min(a, b)
Finds the maximum or minimum of two numbers.

System.out.println(Math.max(10, 20)); // Output: 20

Math.pow(base, exponent)
Raises a number to a power.

System.out.println(Math.pow(2, 3)); // Output: 8.0

Math.random()
Generates a random number between 0.0 and 1.0.

System.out.println(Math.random());
2. Real-Life Application Problems

Password Strength Checker


A program that checks if a password meets these criteria:
- At least 8 characters long
- Contains an uppercase letter, a lowercase letter, a digit, and a special character (@, #, $,
%, &).
import java.util.Scanner;

public class PasswordStrengthChecker {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a password: ");
String password = scanner.nextLine().trim();

if (isStrongPassword(password)) {
System.out.println("Your password is strong.");
} else {
System.out.println("Weak password! Follow the rules.");
}
}

public static boolean isStrongPassword(String password) {


if (password.length() < 8) return false;

boolean hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;


String specialCharacters = "@#$%&";

for (int i = 0; i < password.length(); i++) {


char ch = password.charAt(i);
if (Character.isUpperCase(ch)) hasUpper = true;
if (Character.isLowerCase(ch)) hasLower = true;
if (Character.isDigit(ch)) hasDigit = true;
if (specialCharacters.contains(String.valueOf(ch))) hasSpecial = true;
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
}

You might also like