第五周课程总结:

一::String类

1:实例化String对象,String可以采取直接赋值的方式进行操作;

2:==可以用来进行内容的比较,但是单纯写==不会只进行内容上的比较,还有地址的比较;

3:equals()方法可以只进行内容上的比较;

4:String还可以使用new关键字来实例化;

5:String类常用操作方法 (p110);

6:字符串可以使用toCharrArray()方法变成一个字符数组;

7:String可以使用indexOf()方法返回指定的字符串位置。

二:继承的基本概念

1:使用extends关键字实现继承;

2:只能子承父类,并且只能继承一个父类;

3:使用super关键字调用父类中的指定构造方法;

4:Overloading是重载,Overridng用来覆写;

5:this用来访问本类中的属性,如果本类没有此属性则从父类中继续查找;

6:super访问父类中的属性;

三:final

1:使用final声明的类不能有子类;

2:使用final声明的方法不能被子类所覆写;

3:使用final声明的变量即成为常量,常量不可以改写。

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

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

实验代码:


public class s {
	public static void main(String[] args) {
		String r = "this is a test of java";
		int sum = 0;
		char[] c = r.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 's') {
				sum++;
			}}
		System.out.println("S的个数为"+sum);

	}
}

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

实验代码:


public class is {

	public static void main(String[] args) {
		String c = "this is a test of java";
		int sun = 0;
		char[] r = c.toCharArray();
		for (int i = 0; i < r.length; i++) {
			if (r[i] == 'i' && r[i + 1] == 's') {
				sun++;
			}
		}
		System.out.println("is的个数为"+sun);
	}

}

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

实验代码:


public class isis {
	public static void main(String[] args) {
		String r = "this is a test java";
		String t[] = r.split(" ");
		int sum = 0;
		for (int i = 0; i < t.length; i++) {

			if (t[i].equals("is")) {
				sum++;

			}
		}
		System.out.println("is的个数为"+sum);

	}
}

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

实验代码:


public class daoxu {
	public static void main(String[] args) {
		String c = "this is a test of java";
		int i;
		
		char[] r = c.toCharArray();
		 
		for (i = r.length-1; i >= 0; i--) {
			System.out.print(r[i]);
		}

	}

}
    	   
       

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

实验代码:

import java.util.Scanner;

public class 二 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String t = sc.nextLine();
		char[] r = t.toCharArray();
		if (t.length() == 1) {
			System.out.print(r[t.length()]);
		} else if (t.length() == 2) {
			System.out.print(r[1]);
			System.out.print(r[0]);
		} else {

			System.out.print(r[t.length() - 3]);
			System.out.print(r[t.length() - 2]);
			System.out.print(r[t.length() - 1]);
			for (int i = 0; i < t.length() - 3; i++) {
				System.out.print(r[i]);
			}
		}
	}

}
	

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

实验代码:


public class daxiaoxie {
      public static void main(String[] args) {
    	  String t="ddejidsEFALDFfnef2357 3ed";
    	  int daxie=0,xiaoxie=0,feizimu=0;
    	  char[] c=t.toCharArray();
    	  for(int i=0;i<t.length();i++) {
    		  if('a'<=c[i]&&c[i]<='z') {
    			  xiaoxie++;
    		  }
    		  else if('A'<=c[i]&&c[i]<'Z') {
    			  daxie++;
    		  }
    		  else if('1'<=c[i]&&c[i]<='9'){
    			  feizimu++;
    		  }
    		  
    	  }
    	  System.out.println("大写字母数为"+daxie);
    	  System.out.println("小写字母数为"+xiaoxie);
    	  System.out.println("非字母数为"+feizimu);
      }
}