代码改变世界

第五周课程总结&试验报告(三)

2019-09-27 16:05  小小乌龟君  阅读(272)  评论(2编辑  收藏  举报

学习总结

1.本周我们学习了继承的基本概念:子类与父类。子类通过extends来继承父类。在子类中可以用super()来调用父类的构造方法。在继承的父类的方法中可以对方法进行覆写。
2.super与this时一对冤家他们不能同时出现。在内容上也有很大不同。如super只能从父类中调用方法,而this实在子类没有时才调用父类。而且都要放在构造方法的首行。
3.final关键字是父类挡住子类继承的一道屏障,它可以让定义的类不被继承,而且使变量变成常量。
4.子类可以自动转化为父类,而父类要强制转化为子类。

已知字符串:"this is a test of java".按要求执行以下操作

统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。

public class daoxu {
	public static void main(String[] args) {
		String s = "this is a test of java";
        int x = (s.split("s")).length - 1;
        System.out.println("字符串中字母s出现的次数" + x);
        
        int y = (s.split("is")).length - 1;
        System.out.println("字符串中子串“is”出现的次数" + y);
        
        int z = (s.split(" is")).length - 1;
        System.out.println("字符串中单词“is”出现的次数" + z);
        
        StringBuffer str = new StringBuffer("this is a test of java");
        System.out.println(str.reverse());      //使用了StringBuffer类中的reverse()方法
    }
}

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串

import java. util.*;
public class jiami{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入要加密的英文字串");
		String str = sc.nextLine();
		
		char n;
		String str1=new String();
		for(int i=0;i<str.length();i++) {
			n = str.charAt(i);
			n = (char)(n+3);       //字符后移3位
			
			str1+=n;
		}
		System.out.println("加密后的子串是:\n"+str1);
	}
}

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

public class daxiao {
	public static void main(String[] args) {
		int x,y,z;
		x=y=z=0;
		String s = "ddejidsEFALDFfnef2357 3ed";
		char c[] = s.toCharArray();
		for(int i=0;i< c.length;i++) {
			if(c[i]>= 'a'&&c[i] <= 'z') {
				x++;
			}
			
			else if(c[i] >= 'A'&&c[i]<='Z') {
				y++;
			}
			
			else {
				z++;
			}
		}
		System.out.println("字符串里的大写英文字母数" +y);
		System.out.println("字符串里的小写英文字母数" +x);
		System.out.println("字符串里的非英文字母数" +z);
	}
	
}

关于对第二题的修改

import java. util.*;
public class jiami2{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入要加密的英文字串");
		String str = sc.nextLine();
		
		int x,y;
		String str1,str2;
		x = str.length();
		if(x>3) {
			y = x-3;
			str1 = str.substring(y);
			str2 = str.substring(0,y);
			System.out.println("加密后的子串是:\n"+str1+str2);
		}

		else {
			char n;
			String str3=new String();
			for(int i=0;i<str.length();i++) {
				n = str.charAt(i);
				n = (char)(n+3);       //字符后移3位
				
				str3+=n;
			}
			System.out.println("加密后的子串是:\n"+str3);
		}
	}
}

在字符串长度大于3的,我将后3位移动至最前面。在长度不足的,我将他们的ASCII往后移动了3位。