-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterWrapperClass.java
135 lines (99 loc) · 5 KB
/
CharacterWrapperClass.java
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
130
131
132
133
134
135
public class CharacterWrapperClass {
public static void main(String[] args) {
// Demonstrating Static Methods
demonstrateStaticMethods();
// Demonstrating Instance Methods
demonstrateInstanceMethods();
// Demonstrating Relational Operators
demonstrateRelationalOperations();
// Real-World Use Cases
demonstrateRealWorldUseCases();
}
// Method to demonstrate static methods of Character class
private static void demonstrateStaticMethods() {
System.out.println("Static Methods:\n" + //
"------------------------------");
char letter = 'A';
char digit = '5';
// isDigit()
System.out.println("Character.isDigit('5'): " + Character.isDigit(digit));
// isLetter()
System.out.println("Character.isLetter('A'): " + Character.isLetter(letter));
// isLetterOrDigit()
System.out.println("Character.isLetterOrDigit('5'): " + Character.isLetterOrDigit(digit));
// isLowerCase()
System.out.println("Character.isLowerCase('a'): " + Character.isLowerCase('a'));
// isUpperCase()
System.out.println("Character.isUpperCase('A'): " + Character.isUpperCase(letter));
// isWhitespace()
System.out.println("Character.isWhitespace(' '): " + Character.isWhitespace(' '));
// toLowerCase()
System.out.println("Character.toLowerCase('A'): " + Character.toLowerCase(letter));
// toUpperCase()
System.out.println("Character.toUpperCase('b'): " + Character.toUpperCase('b'));
// toString()
System.out.println("Character.toString('C'): " + Character.toString('C'));
// getNumericValue()
System.out.println("Character.getNumericValue('7'): " + Character.getNumericValue('7'));
// getType()
System.out.println("Character.getType('A'): " + Character.getType(letter));
// forDigit()
System.out.println("Character.forDigit(9, 10): " + Character.forDigit(9, 10));
System.out.println(); // Empty line for separation
}
// Method to demonstrate instance methods of Character class
private static void demonstrateInstanceMethods() {
System.out.println("Instance Methods:\n" + //
"------------------------------");
Character charObj = 'A';
// charValue()
System.out.println("charObj.charValue(): " + charObj.charValue());
// toString()
System.out.println("charObj.toString(): " + charObj.toString());
// equals()
System.out.println("charObj.equals('A'): " + charObj.equals('A'));
// hashCode()
System.out.println("charObj.hashCode(): " + charObj.hashCode());
System.out.println(); // Empty line for separation
}
// Method to demonstrate relational operations
private static void demonstrateRelationalOperations() {
System.out.println("Relational Operations:\n" + //
"------------------------------");
Character value1 = 'A';
Character value2 = 'B';
// Using relational operators
System.out.println("== (Equal to): " + (value1 == value2)); // false
System.out.println("!= (Not equal to): " + (value1 != value2)); // true
System.out.println("> (Greater than): " + (value1 > value2)); // false
System.out.println("< (Less than): " + (value1 < value2)); // true
System.out.println(">= (Greater than or equal to): " + (value1 >= value2)); // false
System.out.println("<= (Less than or equal to): " + (value1 <= value2)); // true
// Using equivalent methods
System.out.println("== (Equal to) using equals(): " + value1.equals(value2)); // false
System.out.println("!= (Not equal to) using !equals(): " + !value1.equals(value2)); // true
System.out.println("< (Less than) using compare(): " + (Character.compare(value1, value2) < 0)); // true
System.out.println("> (Greater than) using compare(): " + (Character.compare(value1, value2) > 0)); // false
System.out.println("<= (Less than or equal to) using compare(): " + (Character.compare(value1, value2) <= 0)); // true
System.out.println(); // Empty line for separation
}
// Method to demonstrate real-world use cases of Character class
private static void demonstrateRealWorldUseCases() {
System.out.println("Real-World Use Cases:\n" + //
"------------------------------");
String input = "Hello 123!";
int digitCount = 0;
int letterCount = 0;
for (char ch : input.toCharArray()) {
if (Character.isDigit(ch)) {
digitCount++;
} else if (Character.isLetter(ch)) {
letterCount++;
}
}
System.out.println("Input String: " + input);
System.out.println("Number of digits: " + digitCount);
System.out.println("Number of letters: " + letterCount);
System.out.println(); // Empty line for separation
}
}