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

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

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

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

1.代码

package text;

public class text1 {
      public static void main(String[] args){
          String str = "this is a test of java";
          int count=0,n=0,a=0;
          
          int i=0;
          while(str.indexOf("is",i)!=-1){
              count++;
              i=str.indexOf("is",i)+1;
          }
          
          int j=0;
          while(str.indexOf("s",j)!=-1){
              n++;
              j=str.indexOf("s",j)+1;
          }  
          
          int b=0;
          while(str.indexOf(" is ",b)!=-1){
              a++;
              b=str.indexOf(" is ",b)+1;
          } 
          StringBuffer st = new StringBuffer(str);
          System.out.println("s的个数:"+n);
          System.out.println("字符is的个数:"+count); 
          System.out.println("单词is的个数:"+a);
          System.out.println("倒序:"+st.reverse().toString());
}
    }

2.运行

二.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
第五周课程总结&试验报告(三)_第1张图片

1.代码

package text;

import java.util.*;
public class test2 {

      public static void main(String[] args) {
          System.out.println("请输入一个字符串:");
          Scanner sc = new Scanner(System.in);
          String str = sc.nextLine();
          char s[] = str.toCharArray();
          char asc=0;
          for(int i=0;i

2.运行

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

1.代码

 package text;

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

2.运行
第五周课程总结&试验报告(三)_第2张图片

总结:
继承: 使用extends关键字可以实现继承的关系
子类不能直接访问父类中的私有操作,要通过其他操作间接访问。
访问权限 private < default < public
重载发生在一个类中,覆写发生在继承中。

你可能感兴趣的:(第五周课程总结&试验报告(三))