第五周课程总结&实验报告

第五周课程总结&实验报告

实验三 String类的应用

实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容

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

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

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

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

1.实验源码

public class new2{

    public static void main(String[] args) {
       String str = "this is a test of java";
       int i = 0;
       int q = 0;
       for(i = 0;i < str.length();i++) {     //利用for循环统计字符串总长度
           char a = str.charAt(i);
           if(a == 's') {
           q++;
       }
    }
       System.out.println("s出现的次数为:" +q);
       int b = str.indexOf("is");
       System.out.println("子串is出现的次数为:"+b);    
       int e = (str.split(" is ")).length-1;
       System.out.println("单词is出现的次数为:"+e);     //split函数
       StringBuffer r = new StringBuffer ("this is a test of java");   
       System.out.println("字符串的倒序输出:"+r.reverse());   
}
}

实验结果

2.实验源码

import java.util.*;
public class new2{

    public static void main(String[] args) {
         System.out.println("输入英文字符串:");
            Scanner ad=new Scanner(System.in); 
            String str=ad.next();//读取字符串
            char t[]= str.toCharArray();
            System.out.println("加密后结果:");
            for(char r:t) {
                System.out.print((char)(r+3));
         }
     }
}

实验结果

3.实验源码

public class new2 {

    public static void main(String[] args) {
         String str="ddejidsEFALDFfnef2357 3ed";
            
            char a[]=str.toCharArray();
            int x=0,y=0,z=0;
            for(int i=0;i<a.length;i++){
                if(a[i]>='A'&&a[i]<='Z'){
                    x++;//计算大写字母数量
                }
                else if(a[i]>='a'&&a[i]<='z'){
                    y++;//计算小写字母数量
                }
                else {
                    z++;//计算除开英文字母其他内容数量
                }
            }
            System.out.println("大写字母数:"+x);
            System.out.println("小写字母数:"+y);
            System.out.println("非英文字母数:"+z);

    }

}

实验结果

第五周总结

1. 既然this和super都可以调用构造方法,那么两者是不可以同时出现的,因为两者调用构造的时候都必须放在构造方法的首行,另外,需要注意的是,无论子类怎样操作,最终都是要先调用父类中的构造方法。

2.抽象类的定义及使用规则如下:

(1)包含一个抽象方法的类必须是抽象类:
(2)抽象类和抽象方法都要使用abstract关键字声明:(3)抽象方法只需声明而不需要实现:
(4)抽象类必须被子类继承,子类(如果不是抽象类)必须覆写抽象类中的全部抽象方法

3.实际上在一个抽象 类中是允许存在构造方法的,因为抽象类依然使用的是类的继承关系,而且抽象类中也存在各个属性,所以子类在实例化之前肯定是先要对父类进行实例化的。

4.

猜你喜欢

转载自www.cnblogs.com/9557yxl/p/11599898.html