-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package GGs; | ||
|
||
public class MyCharacter{ | ||
public final char MIN_VALUE = '\u0000'; | ||
public final char MAX_VALUE = '\uFFFF'; | ||
private final char value; | ||
|
||
static final MyCharacter[] cache = new MyCharacter[128]; | ||
|
||
static { | ||
for (int i = 0; i < cache.length; i++) | ||
MyCharacter.cache[i] = new MyCharacter((char) i); | ||
} | ||
|
||
|
||
public MyCharacter() { | ||
value = MIN_VALUE; | ||
} | ||
|
||
public MyCharacter(char value) { | ||
this.value = value; | ||
} | ||
|
||
public static MyCharacter valueOf(char c) { | ||
if (c <= 127) { | ||
return MyCharacter.cache[(int) c]; | ||
} | ||
return new MyCharacter(c); | ||
} | ||
|
||
public static MyCharacter valueOf(int i) { | ||
return new MyCharacter((char) i); | ||
} | ||
|
||
public char charValue() { | ||
return value; | ||
} | ||
|
||
public static boolean isLowerCase(char ch) { | ||
return ((int) ch) >= 97 && ((int) ch) <= 122; | ||
} | ||
|
||
public static boolean isDigit(char ch) { | ||
return ((int) ch) >= 48 && ((int) ch) <= 57; | ||
} | ||
|
||
public static boolean isUpperCase(char ch) { | ||
return ((int) ch) >= 65 && ((int) ch) <= 90; | ||
} | ||
|
||
public static boolean isLetter(char ch) { | ||
return isUpperCase(ch) || isLowerCase(ch); | ||
} | ||
|
||
public static boolean isLetterOrDigit(char ch) { | ||
return isLetter(ch) || isDigit(ch); | ||
} | ||
|
||
public static char toLowerCase(char ch) { | ||
return Character.toLowerCase(ch); | ||
} | ||
|
||
public static char toUpperCase(char ch) { | ||
return Character.toUpperCase(ch); | ||
} | ||
|
||
public static int compare(char x, char y) { | ||
return x - y; | ||
} | ||
|
||
|
||
public int compareTo(MyCharacter anotherMyCharacter) { | ||
return compare(this.value, anotherMyCharacter.value); | ||
} | ||
|
||
|
||
public int hashCode() { | ||
return MyCharacter.hashCode(value); | ||
} | ||
|
||
|
||
public static int hashCode(char value) { | ||
return (int) value; | ||
|
||
} | ||
|
||
|
||
public boolean equals(MyCharacter obj) { | ||
return (int) this.value == (int) obj.value ? true : false; | ||
} | ||
|
||
|
||
public String toString() { | ||
char buf[] = {value}; | ||
return String.valueOf(buf); | ||
} | ||
} | ||
|
||
class Mycmain { | ||
public static void main(String[] args) { | ||
var myc = new MyCharacter('Z'); | ||
var myc2 = new MyCharacter('Z'); | ||
System.out.println(myc.charValue()); | ||
System.out.println(myc.equals(myc2)); | ||
System.out.println(myc.compareTo(myc2)); | ||
System.out.println(myc.hashCode()); | ||
System.out.println(myc.toString()); | ||
System.out.println(MyCharacter.compare('b','a')); | ||
System.out.println(MyCharacter.hashCode('A')); | ||
System.out.println(MyCharacter.isDigit('p')); | ||
System.out.println(MyCharacter.isLetter('A')); | ||
System.out.println(MyCharacter.isLetterOrDigit('2')); | ||
System.out.println(MyCharacter.isLowerCase('A')); | ||
System.out.println(MyCharacter.isUpperCase('A')); | ||
System.out.println(MyCharacter.toLowerCase('A')); | ||
System.out.println(MyCharacter.toUpperCase('a')); | ||
System.out.println(MyCharacter.valueOf(65)); | ||
System.out.println(MyCharacter.valueOf('b')); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package GGs; | ||
public final class MyString2 { | ||
char[] chars; | ||
String s; | ||
|
||
public MyString2(String s) { | ||
this.chars = s.toCharArray(); | ||
this.s = s; | ||
} | ||
|
||
public int compare(String s) { | ||
int currObjTotal = 0; | ||
int sTotal = 0; | ||
|
||
if (this.chars.length != s.length()) { | ||
int commonLength = 0; | ||
for (int i = 0; i < chars.length && i < s.length(); i++) { | ||
commonLength = i; | ||
} | ||
return (Character.toLowerCase(this.chars[commonLength]) - Character.toLowerCase(s.charAt(commonLength)) < 0) ? -1 : 1; | ||
} | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
currObjTotal += Character.toLowerCase(chars[i]); | ||
sTotal += Character.toLowerCase(s.charAt(i)); | ||
} | ||
return currObjTotal - sTotal; | ||
} | ||
|
||
public MyString2 substring(int begin) { | ||
String s = ""; | ||
for (int i = begin; i < chars.length; i++) { | ||
s += chars[i]; | ||
} | ||
return new MyString2(s); | ||
|
||
} | ||
|
||
public MyString2 toUpperCase() { | ||
char[] chs = new char[chars.length]; | ||
for (int i = 0; i < chars.length; i++) { | ||
chs[i] = Character.toUpperCase(chars[i]); | ||
} | ||
String b = ""; | ||
for(int i =0;i < chars.length;i++) | ||
b += chs[i]; | ||
|
||
return new MyString2(b); | ||
|
||
} | ||
|
||
public char[] toChars() { | ||
return chars; | ||
} | ||
|
||
public static MyString2 valueOf(boolean b) { | ||
return b ? new MyString2("true") : new MyString2("false"); | ||
} | ||
|
||
} | ||
|
||
|
||
class mymain{ | ||
public static void main(String[] args) { | ||
var myst = new MyString2("deterministic"); | ||
MyString2 b = myst.substring(5); | ||
MyString2 c = myst.toUpperCase(); | ||
System.out.println("sub: " + b.s + "\n"+ | ||
"up: " + c.s + "\nDeterministic VS Undeterministic: " + myst.compare("retErmiNistic")); | ||
} | ||
} |