第五周总结&实验报告三

第五周总结&实验报告三

实验报告

1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)

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

① 统计该字符串中字母s出现的次数。

package test3;

public class Zifu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "this is a test of java";
		char c[]=str.toCharArray();
		char s='s';
		int count=0;
		for(int j=0;j<=c.length-1;j++)
			if(s==c[j])
				count++;
			 
		     
	System.out.print("字符s出现的次数:"+count);
      }

}

② 统计该字符串中子串“is”出现的次数。

package test3;

public class Zifu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "this is a test of java";
		char c[]=str.toCharArray();
		char s='s',i='i';
		int count=0;
		for(int j=0;j<=c.length-1;j++)
			if(i==c[j]&&s==c[j+1])
				count++;
			 
		     
	System.out.print(“字串is出现的次数:"+count);
      }

}

③ 统计该字符串中单词“is”出现的次数。

package test3;

public class Zifu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "this is a test of java";
		char c[]=str.toCharArray();
		char s='s',i='i';
		int count=0;
		for(int j=0;j<=c.length-1;j++)
			if(c[j]==' '&&i==c[j+1]&&s==c[j+2]&&c[j+3]==' ')
				count++;
			 
		     
	System.out.print("单词is出现的次数:"+count);
      }

}

④ 实现该字符串的倒序输出。

package test3;

public class Zifu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "this is a test of java";
		String c[]=str.split(" ");
		
		for(int j=c.length-1;j>=0;j--)
			
			 System.out.print(c[j]+" ");
      }

}

这个题目其实比较简单,只要掌握了String方法就能做出来。

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

package test4;

import java.util.Scanner;
public class Jiemi {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		char a,b,d;
		Scanner scanner = new Scanner(System.in);
		String str=scanner.next();
		char e[]=str.toCharArray();
		char c[]=str.toCharArray();
		String result[]=str.split("");
		for(int x=0;x<result.length;x++) {
			System.out.print(result[x]+" ");
			
		}
		System.out.println(" ");
		a=c[c.length-1];
		b=c[c.length-2];
		d=c[c.length-3];
		for(int i=0;i<c.length-3;i++) {
			e[i+3]=c[i];
			
		}
		e[0]=d;
		e[1]=b;
		e[2]=a;
		for(int i=0;i<c.length;i++)
			System.out.print(e[i]);
		

	}

}

这个题目的话我觉得主要考察对字符的运用,以及String类方法的运用,也是比较简单的。

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

package test5;

public class Zifutongji {

	public static void main(String[] args) {
		String str="ddejidsEFALDFfnef2357 3ed";
		int m=0,n=0,k=0;
		char[] c=str.toCharArray();
		for(int i=0;i<str.length();i++)
		{
			if(c[i]>='a'&&c[i]<='z')
			{
				m++;
			}
			else if(c[i]>='A'&&c[i]<='Z')
			{
				n++;
			}
			
			else {
				k++;
			}
		}
		System.out.println("小写字母出现的次数: "+m);
		System.out.println("大写字母出现的次数: "+n);
		System.out.println("其他字符出现的字数: "+k);
        
	}

}


这个题目也是考察的字符串类问题,这是一个统计字符的题目,我们以前做过类似的,所以相对来说也没有很大的问题。

课程总结

这周我们学习了子类用extends继承父类的应用,并用super可以从子类中调用父类中的构造方法,
并且学习了方法的覆写,在子类写一个与父类相同的构造方法把父类中的方法覆盖掉,并输出子类方法中的内容
还有抽象类,抽象类必须用abstract声明,抽象类必须被子类继承,,含有抽象方法的类必须是抽象类等等。

posted @ 2019-09-26 21:45  渔家傲。  阅读(168)  评论(1编辑  收藏  举报