第五周作业及总结

实验三 String类的应用

实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

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

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

package zf;

public class zf {
	public static void main(String[] args) {
		String s = "this is a test of java";
		int n = s.indexOf("s",3);
		System.out.println(+n);
		int a = s.indexOf("is");
		System.out.println(+a);
		int b = (s.split(" is ")).length - 1;
	    System.out.println("单词is出现的次数" + b);
	    StringBuffer r = new StringBuffer ("this is a test of java");
	    System.out.println(r.reverse());
	}
}

第一题老师上课提过
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图

package zf;

import java.util.Scanner;
public class wj {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一串英文字符串或解密字符串");
		String password = sc.nextLine();
		char[] array=password.toCharArray(); 
		for (int i = 0; i < array.length; i++) { 
			array[i]=(char)(array[i]^20000);
		}
		System.out.println("加密或解密结果如下:");
		System.err.println(new String(array));
}


上面是错误截图,一直报错“Syntax error, insert "}" to complete ClassBody”查找说是因为括号前的空格没有删除尝试之后还是报错然后发现是少了一个“}”
以下是正确截图

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

package zf;

public class zm {
	public static void main(String[] args) {
		String s = "ddejidsEFALDFfnef2357 3ed";
		int min=0,max=0,z,none=0;
		for (z=0;z<s.length();z++) {
			char a=s.charAt(z);
			if (Character.isLowerCase(a)) {
				min++;
			}
			else if (Character.isUpperCase(a)){
				max++;
			}
		}
		none=s.length()-min-max;
		System.out.println("大写字母个数:"+max);
		System.out.println("小写字母个数:"+min);
		System.out.println("非英语字母个数:"+none);
	}
}


这题和第一题稍微有点相像的感觉差不多的套用一下得出

总结
(1)super()关键字的作用
1、super表示超(父)类的意思,this表示对象本身
2、super可用于访问父类被子类隐藏或着覆盖的方法和属性,使用形式为super.方法(属性)
3、在类的继承中,子类的构造方法中默认会有super()语句存在(默认隐藏),相当于执行父类的相应构造方法中的语句,若显式使用则必须位于类的第一行 
4、对于父类有参的构造方法,super不能省略,否则无法访问父类的有参构造方法,使用形式为super()
“super和this不能同时使用”
(2)mian方法中不能使用this和super

posted @ 2019-09-27 14:00  阿园吖  阅读(301)  评论(0编辑  收藏  举报