Third & Fifth weekly summary test report

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).
Count the number of letters in the string s appears.
The string neutron count the number of times the string "is" appears.
The number of words in the string "is" appears in the statistics.
Achieve reverse the string is output.

Experiment Code

public class Example1 {
    public static void main(String args[]) {
        String str = new String("this is test of java");
        System.out.println("含有S的个数:"+Fangfa1(str));
        System.out.println("is的字串:"+Fangfa2(str));
        System.out.println("单词is的个数"+Fangfa3(str));
        Fangfa4("倒序输出字符串"+str);

    }
    public static int Fangfa1(String str) {
        int count1 = 0;
        char c[] = str.toCharArray();
        for(int i = 0; i < c.length;i++) {
            if(c[i] == 's') {
                count1++;
            }
        }
        return count1;
    }
    public static  int Fangfa2(String str) {
        int count2 = 0;
        char c[] = str.toCharArray();
        for(int i = 0; i < c.length;i++) {
            if(c[i] == 's' && c[i - 1] == 'i') {
                count2++;
            }
        }
        return count2;
    }
    public static int Fangfa3(String str) {
        int count3 = 0;
        String c[] = str.split(" ");
        for(int i = 0;i < c.length ; i++) {
            if(c[i].equals("is")) {
                count3++;
            }
        }
        return count3;
    }
    public static void Fangfa4(String str) {
        String c[] = str.split(" ");
        for(int i = c.length - 1; i > 0;i--) {
             System.out.print(c[i] + " ");
            
        }
    }
}

Experimental results

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.

#### experiment code

import java.util.*;
public class Jiami {
    public static void main(String args[]) {
    Scanner in= new Scanner(System.in);
    String str = in.nextLine();
    char c[] = str.toCharArray();
    for(int i = 0; i< c.length;i ++) {
         c[i] = (char) (c[i] + 3);
    }
    String str2 = new String(c);
    System.out.println(str2);
    in.close();
    }
}

Experimental results

3. Given the string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.

Experiment Code

public class Panduanzifu {
    public static void main(String args[]) {
        String str = "ddejidsEFALDEfnef2357 3ed";
        char c[] = str.toCharArray();
        int count1 = 0, count2 = 0, count3 = 0;
        for(int i = 0;i < c.length;i ++) {
            if((int)c[i] >= 65 & (int)c[i] <= 90) 
            count1++;
            if((int)c[i] >= 97 & (int)c[i] <= 122) {
                count2++;
            }
            
            if((int)c[i] < 65 || (int)c[i] > 90 & (int)c[i] < 97 || (int)c[i] > 122) {
                count3++;
            }
        }
        System.out.println("大写字母数:"+count1);
        System.out.println("小写字母数:"+count2);
        System.out.println("非英文字数:"+count3);
    }

}

Experimental results

Experimental summary

The experiment is string handling, general process is nothing more difficult, is to count the word is, when the word count is zero, then I thought, is more direct comparison address, no problem after using the equals of. Although the experiment is simple, but the more I deal with a string of more handy.

Fifth weekly summary

Major topics of this week's second Java properties, inheritance, final keyword to further explain the polymorphism, polymorphic objects,

attention: Java allows only single inheritance, multiple inheritance is not allowed, that is a subclass only inheriting a parent class may inherit multiple layers, i.e., a sub-class may have a parent, a parent may have a parent.
Inheritance subclasses sometimes called a derived class, extends itself meaning for the expansion, is to extend the content of the parent class's
attention: a subclass can not access the parent class private members direct, non-private method can be called, you can be called indirect private members, such as setter, getter.
Subclass object is instantiated before the default constructor will first call the parent class, so before instantiating subclasses of objects, you need to initialize the first parent class attribute, it implies a super () method in the subclass.
super () represents a sub-class constructor with no arguments can directly call the parent class.
Overwrite:
there is subclass inherits the parent class.
Idea: subclass override method can not have more stringent than the parent class privileges.

super keyword:
As mentioned earlier super parent class can be called directly.
super constructor call with this as the first line of super constructor method to subclass
super and this is very similar, you can call the constructor, general methods, properties, but both still have a little difference.

The final keyword:
final represents the ultimate meaning in Java, you can use the final declaration classes, properties, methods
Note:

  • Using the final declaration of the class can not have subclasses.
  • Use final declaration methods can not quilt class override.
  • Variables declared become final, const can not be modified.
    It requires all caps when the final variable name.
    Abstract class:
    abstract class is dedicated to provide a Java class as the parent class
    definitions and naming abstract classes:
  • The method comprises an abstract class must be abstract.
  • Abstract classes and abstract methods should use the abstract keyword to declare;
  • Only need to implement an abstract method declaration.
  • An abstract class must be inherited by subclasses, subclasses an abstract class if not all abstract methods must be abstract class to be overwritten.

    An abstract class can not use the keyword final statement, because an abstract class must be inherited, it must be overwritten.
    Constructor abstract class defines
    an abstract class is actually more than an abstract method than regular classes, in addition there is no big difference.

Polymorphism:
overloading and one is a method of overwriting
polymorphism is another object
polymorphic objects are mainly of two types

Upward transition, the program will automatically, downcast, subclasses types must be clear transformation
must take place before the transition downcast up.
main function

Guess you like

Origin www.cnblogs.com/xiao--liang/p/11598008.html