第五周课程总结&实验报告

一、已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。

package project;

public class 第一题 {
	public static void main(String[] args) {
        String str = "this is test of java";
        int count = 0;
        int sum = 0;
        int num = 0;
        String[] v = str.split(" ");
        for (int a = 0; a < str.length(); a++) {
            char c = str.charAt(a);
            if (c == 's') {
                count++;
            }

        }
        for (int n = 0; n < str.length() - 2; n++) {
            String z = str.substring(n, n + 2);
            if (z.equals("is")) {
                sum++;
            }
        }
        for (int m = 0; m < str.length() - 4; m++) {
            String z = str.su![](https://img2018.cnblogs.com/blog/1580635/201909/1580635-20190927202350305-864314516.jpg)

bstring(m, m + 4);
            if (z.equals(" is ")) {
                num++;
            }
        }

        System.out.println("字符串中字母“s”出现的次数:" + count);
        System.out.println("字符串中子串“is”出现的次数:" + sum);
        System.out.println("字符串中单词“is”出现的次数:" + num);
        System.out.print("倒序输出1:");
        for (int j = str.length() - 1; j > 0; j--) {
            char zf = str.charAt(j);
            System.out.print(zf);
        }
        
	}
}

二、请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

package project;
import java.util.Scanner;

public class 第二题 {
	public static void main(String[] args) {
        System.out.println("请输入字符串:");
        Scanner sc=new Scanner(System.in);
        String str1=sc.next();
        char[] c= str1.toCharArray();
        System.out.println("加密后的结果");
        for(char x:c){
            System.out.print((char) (x+3));
        }
    }
}

三、已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。

package project;

public class 第三题 {
	 public static void main(String args[]) {
         
         String str = "ddejidsEFALDFfnef2357 3ed";
         
         char a[] = str.toCharArray();
         
         int count1 = 0, count2 = 0, count3 = 0;
         
         for(int i = 0; i <= str.length() - 1;i++){
             if(a[i] >= 'A' && a[i] <= 'Z'){
                 count1++;
             }
             
             else if(a[i] >= 'a' && a[i] <= 'z') {
                 count2++;
             }
             
             else {
                 count3++;
             }
         }
         
         System.out.println("大写字母数为:"+count1);
         System.out.println("小写字母数为:"+count2);
         System.out.println("非英文字母数为:"+count3);

    }
}

本周课程总结:
1、学习使用了很多String类常用操作方法
例:public char[] toCharArray() 将字符串变为字符数组
public charAt(int index) 从一个字符串中取出指定位置的字符
public int length() 取得字符串长度
*public boolean equals(String str) 判断两个字符内容是否相等
2、了解学习了final
final声明的类不能有子类
final声明的方法不能被子类所覆写
final声明的变量即成为常量,常量不能修改
3、super()关键字的作用
super表示超(父)类的意思,this表示对象本身
super可用于访问父类被子类隐藏或着覆盖的方法和属性,使用形式为super.方法(属性)
在类的继承中,子类的构造方法中默认会有super()语句存在(默认隐藏),相当于执行父类的相应构造方法中的语句,若显式使用则必须位于类的第一行
对于父类有参的构造方法,super不能省略,否则无法访问父类的有参构造方法,使用形式为super()

原文地址:https://www.cnblogs.com/zl010206/p/11600184.html