Files
HW-02-git-and-java-practice/src/main/java/BonusExercises.java
T
2026-04-23 04:57:11 +04:30

272 lines
7.1 KiB
Java

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BonusExercises {
/*
complete the method below, so it will validate an email address
1. Must have exactly one @ (no more, no less)
2. Split into local-part and domain (before @ and after @)
3. Local-part rules:
- Can't be empty
- Can't start or end with dot
- Can't have two dots in a row
4. Domain rules:
- Can't be empty
- Can't start or end with hyphen
- Can't have underscores
- Each segment (between dots) must follow same hyphen rules
*/
public boolean validateEmail(String email) {
String regex = ""; // todo
int indexAt = 0;
int Atnum = 0 ;
int datnum = 0 ;
int dat1 = 0;
int dat2 = 0 ;
int emsize = email.length() ;
for (int i = 0 ; i < email.length(); i ++)
{
if (email.charAt(i) == '@')
{
indexAt = i ;
Atnum ++ ;
}
if (email.charAt(i) == ' ')
{
return false ;
}
}
if (Atnum != 1)
{
return false ;
}
if(indexAt == 0)
{
return false ;
}
if (indexAt == 1 && email.charAt(0) == ' ')
{
return false ;
}
if (email.charAt(0) == '.' || email.charAt(Atnum-1) == '.')
{
return false ;
}
for (int i = 0 ; i <Atnum ; i++)
{
if (email.charAt(i) == '.')
{
datnum ++ ;
}
if (datnum == 1)
{
dat1 = i;
}
if (datnum == 2)
{
dat2 = i ;
}
}
if (datnum > 1)
{
return false ;
}
if(indexAt== (emsize-1))
{
return false ;
}
if (indexAt == (emsize-2) && email.charAt(emsize-1) == ' ' )
{
return false ;
}
if (email.charAt(indexAt+1) == '-' || email.charAt(emsize-1) == '-')
{
return false ;
}
for (int i = indexAt ; i <emsize ; i++)
{
if (email.charAt(i) == '_')
{
return false;
}
}
if(datnum == 2)
{
if (email.charAt(dat1+1) == '-' || email.charAt(dat2-1) == '-')
{
return false ;
}
else {
return true ;
}
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
return true;
}
/*
This method should find and return the first date in a string.
Supported formats:
- American: MM/DD/YYYY (e.g., 12/09/2023)
- British: DD/MM/YYYY (e.g., 12/09/2023 — same pattern, context matters)
- ISO: YYYY-MM-DD (e.g., 2024-07-15)
- Slash variant: YYYY/MM/DD (e.g., 2025/01/01)
If no match for a date is found in the string, return null.
*/
public String findDate(String string) {
// todo
int StartIndex = 0 ;
String Final = "" ;
boolean CheckNull = true ;
String number = "0123456789" ;
int StrSize = string.length() ;
for (int i = 0 ; i < StrSize ; i ++)
{
char x = string.charAt(i) ;
if(number.indexOf(x) != -1)
{
StartIndex = i ;
CheckNull = false ;
break;
}
}
if (CheckNull == true)
{
return null ;
}
for (int i = StartIndex ; i <StartIndex+10 ; i ++)
{
Final = Final + string.charAt(i) ;
}
return Final;
}
/*
given a string, implement the method to detect all valid passwords
then, it should return the count of them
a valid password has the following properties:
- at least 8 characters
- has to include at least one uppercase letter, and at least a lowercase
- at least one number and at least a special char "!@#$%^&*"
- has no white-space in it
*/
public int findValidPasswords(String string) {
// todo
String number = "0123456789" ;
String special = "!@#$%^&*" ;
int FinalNumber = 0 ;
int StrSize = string.length() ;
for(int i = 0 ; i < StrSize ; i ++)
{
if(string.charAt(i)==' ') continue;
boolean check_upper = false ;
boolean check_lower = false ;
boolean check_number = false ;
boolean check_special = false ;
boolean len = false ;
char x = string.charAt(i) ;
if (x != ' ' ) {
int j = i ;
while ( j < StrSize && string.charAt(j) != ' ') {
char y = string.charAt(j) ;
if (Character.isDigit(y)) {
check_number = true;
}
if (Character.isUpperCase(y)) {
check_upper = true;
}
if (Character.isLowerCase(y)) {
check_lower = true;
}
if (special.indexOf(y) != -1) {
check_special = true;
}
j ++ ;
}
if ((j-i)>=8)
{
len = true ;
}
i = j-1 ;
if (len && check_lower && check_number && check_special && check_upper)
{
FinalNumber ++ ;
}
}
}
return FinalNumber;
}
/*
you should return a list of *words* which are palindromic
by word we mean at least 3 letters with no whitespace in it
vv
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
*/
public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>();
// todo
int size = string.length() ;
for (int i = 0 ; i < size ; i ++)
{
if(string.charAt(i)==' ') continue;
String pal = "" ;
String pal2 = "" ;
String pal3 = "" ;
String spe = "!?.*," ;
int j = i ;
while (j < size && string.charAt(j) != ' ' ) {
char y = string.charAt(j) ;
if (Character.isLetter(y)) {
pal3 = pal3 + y;
pal = Character.toLowerCase(y) + pal;
pal2 = pal2 + Character.toLowerCase(y);
}
j++ ;
}
if (pal2.equals(pal) && pal3.length()>=3)
{
list.add(pal3) ;
}
i = j - 1 ;
}
return list;
}
public static void main(String[] args) {
// you can test your code here
}
}