-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabActivity.java
More file actions
129 lines (102 loc) · 3.99 KB
/
LabActivity.java
File metadata and controls
129 lines (102 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.*;
public class LabActivity {
public static void main(String[] args) {
Scanner con = new Scanner(System.in);
// 1
System.out.print("Enter your lastname: ");
String lastname = con.next().toLowerCase();
System.out.print("Enter your firstname: ");
String firstname = con.next().toLowerCase();
System.out.print("Enter your middle initial: ");
String middlename = con.next();
System.out.println("Your name is: " + formatName(lastname, firstname, middlename));
// 2
System.out.print("\nEnter a String With Special Characters: ");
String data = con.next();
System.out.println("The repeated symbol is: " + extractUniqueSpecialCharacters(data));
// 3
System.out.print("\nEnter a email: ");
String email = con.next();
System.out.println("The email is: " + isEmailValid(email));
// call and test your methods here
// provide inputs as arguments to your methods
}
/*
* Create your own method that returns the complete name in this format:
*
* Sample parameter or input: lastname: PaTiNo firstname: jOaQUIn mi : sArNo
*
* result or return value: Joaquin S. Patino NOTE: there must be a period after
* the middle initial Error TRAP: lastname and firstname and middle name MUST
* NOT CONTAIN any number, even at least 1 number. If the user does not follow
* the error trap rule, return an empty string.
*
*/
public static String formatName(String lastname, String firstname, String middlename) {
String formattedName = "";
int countfirstname = firstname.length();
int countlastname = lastname.length();
char firstnameLetterCapital = firstname.toUpperCase().charAt(0);
char lastnameLetterCapital = lastname.toUpperCase().charAt(0);
char finalmiddlename = middlename.toUpperCase().charAt(0);
String finalfirstname = firstnameLetterCapital + firstname.substring(1, countfirstname);
String finallastname = lastnameLetterCapital + lastname.substring(1, countlastname);
formattedName = finalfirstname + " " + finalmiddlename + ". " + finallastname;
return formattedName;
}
/*
* Create a method or function that accepts a string as paramter and extract or
* collect all special characters in that string WITHOUT DUPLICATES OR REPEATING
* VALUES.
*
* Example parameter: 123abc@#...#131#^^.. Sample return value: @#.^
*/
public static String extractUniqueSpecialCharacters(String data) {
String result = "";
for (int count = 0; count < data.length() - 1; count++) {
char temp = data.charAt(count);
if (!Character.isDigit(temp) && !Character.isLetter(temp)) {
if (result.indexOf(temp) == -1) { // checks if there is no repetition of a character
result = result + temp;
}
}
} // loop exit
return result;
}
/*
* Create a method or function that accepts a string as the email and check if
* the email is valid or not.
*
* A valid email must have ONLY 1 @ AND ONLY 1 .(PERIOD)
*
* Sample parameter: ken@gmail.com return value: true
*
* Sample parameter: ken@gmailcom return value: false (no period)
*
* Sample parameter: ken@@@gmail.com return value: false (too many @)
*
* Sample parameter: kengmail...com return value: false (too many .(period) and
* no @)
*/
public static boolean isEmailValid(String email) {
boolean emailValid = false;
int symbol1 = 0;
int symbol2 = 0;
for (int count = 0; count < email.length(); count++) {
char temp = email.charAt(count);
if (temp == '@') {
symbol1 = symbol1 + 1;
}
if (temp == '.') {
symbol2 = symbol2 + 1;
}
} // loop exit
if (symbol1 > 1 && symbol2 > 1) {
emailValid = false;
}
if (symbol1 == 1 && symbol2 == 1) {
emailValid = true;
}
return emailValid;
}
}