Application third experiment String 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).
- The number of letters in the string s occurs statistics.
- Code:
-
public class Wrold{ public static void main(String[] args) { String str="this is a test of java"; int count=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='s') count++; } System.out.println("字符串里面“s”出现的次数:"+count); } }
Screenshot:
-
- The string neutron count the number of times the string "is" appears.
- Code:
public class Wrold{ public static void main(String[] args) { String str="this is a test of java"; int count=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='i'&&str.charAt(i+1)=='s') count++; } System.out.println("字符串中子串“is”出现的次数:"+count); } }
Screenshot:
-
- The number of words in the string "is" appears in the statistics.
- Code:
public class Wrold{ public static void main(String[] args) { String str="this is a test of java"; String s[]=str.split(" "); int count=0; for(int i=0;i<s.length;i++) { if(s[i].equals("is")) count++; } System.out.println("该字符串中单词“is”出现的次数:"+count); } }
Screenshot:
-
- Achieve reverse the string is output.
- Code:
public class Wrold{ public static void main(String[] args) { StringBuffer n=new StringBuffer(); n.append("this is a test of java"); String str=n.reverse().toString(); System.out.println(str); } }
Screenshot:
-
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.
Code:
public class Wrold{ public static void main(String[] args){ String str="123456"; Scanner sc=new Scanner(System.in); String str=sc.nextLine(); char m[]=str.toCharArray(); char n[]=new char[50];int j=0; for(int i = m.length-3;i<m.length;i++) { n[j]=m[i]; j++; } for(int i=0;i<m.length-3;i++){ n[j]=m[i]; j++; } System.out.print(n); } }
Screenshot:
3. Given the string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.
Code:
public class Wrold{ public static void main(String[] args) { String str="ddejidsEFALDFfnef2357 3ed"; int a=0, b=0, c=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)>=70&&str.charAt(i)<=95) { a++; } else if(str.charAt(i)>=70&&str.charAt(i)<=100) { b++; } else c++; } System.out.println ( "number of uppercase letters:" + A); System.out.println ( "Number lowercase letters:" + B); System.out.println ( "Number of non-English letters:" + C) ; } }
Screenshot:
Learning Summary: The first part of this experiment is to use the string class, the teacher in class most of all speak, and I took a map. The second question, do not run out, I think the program should be no problem
the difference between this and super keywords:
this: current object
super: direct parent object
this (): constructor with no arguments current class, there may specify parameters, such as: this (a)
super (): constructor with no arguments direct parent class may be assigned a reference such as: super (a)
Note: 1, when calling a variable b in the method, the compiler will traverse upwards until you find the nearest one reference variable: b-> this.b-> super.b, if not found, the compiler will prompt an explicit error message;
2, when the variable b is defined only occurs when the parent class, then b = this.b = super.b;
3, when the cover member variable local variable b b, using this.b call a member variable, in this case include member variables and new subclass inherits variables, does not contain hidden variables;
4, when the sub-class member variables b b covering the parent class member variables, use super.b call this the hidden member variables;
5, when the child class overrides a superclass method Method (), you may be used super.method () to call the parent class's method is hidden;
6, super () and this () with hard conditions, or the compiler can not must be the first statement in a constructor by --Constructor call. That
Both appear to be the position of the first line constructor.
Inheritance: Java class allows only single inheritance.
Overloading and covering:
169 books.