Fifth week Course summary:
Third experiment String application class
- Purpose
- Master the use of class String class;
- Learn to use JDK help documentation;
- Content Experiments
1. Known string: "this is a test of java" required to do the following requirements :( source code, the results screenshot).
- 1. count the number of letters in the string s appears.
- Experiments Source:
-
public class text1 {
public static void main(String[] args) {
String str="this is a test of java";
int count=0;
char c[]=str.toCharArray();
for(int i=0;i<c.length;i++){
if(c[i]=='s'){
count++;
}
}
System.out.println("s出现了"+count+"次");
}} - Screenshot:
- 2. statistics the number of occurrences of the string neutron string "is".
- Experiments Source:
-
public class texe2 {
public static void main(String[] args) {
String str = "this is a test of java";
int count = 0, index = 0;
while (true) {
int n = str.indexOf("is", index);
if (n != -1) {
index = n + 1;
count++;
} else {
break;
}}
Of System.out.print ( "IS appeared" + count + "times");
}
} - Screenshot:
- 3. count the number of words in the string "is" appears
- Experiments Source:
-
public class text3 {
public static void main(String[] args) {
int count=0;
String str="this is a test of java";
String s[]=str.split(" ");
for(int i=0;i<s.length;i++){
if(s[i].equals("is")){
count++;
}}
System.out.println(count);
}
} - Screenshot:
。
- 4. The realization of the reverse output string.
- Experiments Source:
-
public class text4 {
public static void main(String[] args) {
String str = "this is a test of java";
char s[] = str.toCharArray();
for (int i=s.length-1;i>=0;i--) {
System.out.print(s[i]);}}
} - Screenshot:
- Summary: class when the teacher basically for us to demonstrate through eclipse again, that is the basic operation of such a situation;
2. Write a program, using the following algorithm to encrypt or decrypt the English string input by the user. It requires source code, the results screenshot.
Experiments Source:
public class text1 {
public static void main(String[] args) {
System.out.print("输入一个字符串:");
var scanner = new Scanner(System.in);
String str1 = scanner.nextLine();
System.out.println(str1);
char c[] = str1.toCharArray();
int ASCII;
char c1;
for (int i = 0; i < c.length; i++) {
ASCII = c[i] + 3;
// System.out.print(ASCII+" ");
c1 = (char) ASCII;
// System.out.print(c1+" ");
String s = String.valueOf(c1);
System.out.print(s);
}
3. Given the string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.
Experiments Source:
public static void main(String[] args) {
String str = "adhadhahdhahhH 1231";
char c[] = str.toCharArray();
int count1 = 0, count2 = 0, count3 = 0;
for (int i = 0; i < c.length; i++) {
int n = (int) c[i];
if (65 <= n && n <= 90) {
count1++;
} else if (97 <= n && n <= 122) {
count2++;
} else {
count3++;
}
System.out.println ( "number of uppercase letters:" + count1 is);
System.out.println ( "Number lowercase letters:" count2 +);
System.out.println ( "Number of non-English letters:" + count3);
}
Lessons Learned:
form:
Inheritance (extends);
Overloaded and overridden methods difference:
Point of difference |
Overload |
Overwrite |
---|---|---|
word |
|
|
definition |
The same method name, the number of parameters of different types, or |
Method name, parameter types, the return values are all the same type |
definition |
I did not ask for permission |
The method can not be overridden in a more restrictive permission |
range |
It occurs in a class |
It occurred in succession |
Use super keyword;
The final keyword: class & Attribute & Method;
..... ..... benefit
Plain and boring