第五周学习总结&实验报告三

实验三 String类的应用

实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)

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

package shiyan3;

public class Main {
	public static void main(String[] args){
		String str= "this is a test of java";
		int count1=0;
		int count2=0;
		int count3=0;
		char[] c=str.toCharArray();
		for(int i=0;i<c.length;i++){
			if(c[i]=='s'){
				count1++;
			}
			if(c[i]=='i'&&c[i+1]=='s'){
				count2++;
			}
			if(c[i]==' '&&c[i+1]=='i'&&c[i+2]=='s'&&c[i+3]==' '){
				count3++;
			}
		}
		System.out.println("s个数="+count1);
		System.out.println("子串'is'个数="+count2);
		System.out.println("单词'is'个数="+count3);
		
		StringBuffer str1=new StringBuffer("this is a test of java");
		System.out.println(str1.reverse().toString());
	}
}

运行截图:

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

package shiyan3;

import java.util.Scanner;

public class A {
	public static void main(String[] args){
		System.out.print("请输入一个字符串:");
		Scanner str=new Scanner(System.in);
		String str1=str.nextLine();
		char c[]=str1.toCharArray();
		char b[]=new char[50];
		int j=0;
		for(int i=c.length-3;i<c.length;i++){
			b[j]=c[i];
			j++;
		}
		for(int i=0;i<c.length;i++){
			b[j]=c[i];
			j++;
		}
		System.out.print(b);
	}
	
}

运行截图:

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

package shiyan3;

public class D {
	public static void main(String[] args){
		String str="ddejidsEFALDFfnef2357 3ed";
		int num1=0;
		int num2=0;
		int num3=0;
		for(int i=0;i<str.length();i++){
			char c=str.charAt(i);
			if(c>='A'&&c<='Z'){
				num1++;
			}
			if(c>='a'&&c<='z'){
				num2++;
			}
			else{
				num3++;
			}
		}
		System.out.println("大写字母数:"+num1);
		System.out.println("小写字母数:"+num2);
		System.out.println("非英文字母数:"+num3);
	}

}

运行截图:

学习总结:
这周我们又学习java三大特性的继承跟内部类。我感觉有点难懂,就想前面的另一个特性一样(封装)。

   继承就是继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。它有四个特点:1、继承关系是传递的;2、提供多重继承机制;3、提高代码的复用性;4、Java只支持单继承。在java中使用extends关键字完成类的继承关系。

   Super可以从子类中调用父类的构造方法、普通方法、属性。但是super类不可以跟this同时使用,this可以访问本类中的属性(方法),如果本类中没有此属性(方法),则从父类中继续查找;而super是直接从父类中直接访问属性及方法。this调用本类构造必须放在构造方法的首行,而super则是调用父类构造,必须在子类构造方法的首行,this表示当前对象,而super无此概念。

   然后就是final关键字,表示最终的意思,也可以称作完结器。使用final声明类不能有子类,声明的方法不能被子类所覆写,声明的变量是常量不能修改。

  抽象类:使用了关键词abstract声明的类叫叫做“抽象类”。如果一个类里包含了一个或多个抽象方法,类就必须指定成abstract(抽象)。“抽象方法”,属于一种不完整的方法,只含有一个声明,没有方法主体
原文地址:https://www.cnblogs.com/jzq93/p/11595337.html