实验三 String类的应用

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

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

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

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

 

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

 

1.

(1).源代码:

package test;
public class Test1 {
    public static void main(String args[]) {
        String str="this is a test of java";
        int count = 0;
        char c[]=str.toCharArray();
        for(int i=0;i<c.length;i++){
            if(c[i]=='s'){
                count++;
            }          
         }
        System.out.println("字母s出现了"+count+"次");
           
    }
}
截图:

 

 (2).源代码:

package test;
public class Test2 {
    public static void main(String args[]) {
        String str="this is a test of java";
        int count = 0;
        char c[]=str.toCharArray();
        for(int i=0;i<c.length;i++){
            if(c[i]=='i'&&c[i+1]=='s'){
                count++;
            }          
         }
        System.out.println("子串is出现了"+count+"次");
           
    }
}
 
截图:

 

 (3).源代码:

package test;
public class Test3 {
    public static void main(String args[]) {
        String str="this is a test of java";
        int count = 0;
        char c[]=str.toCharArray();
        for(int i=0;i<c.length;i++){
            if(c[i]==' '&&c[i+1]=='i'&&c[i+2]=='s'&&c[i+3]==' '){
                count++;
            }          
         }
        System.out.println("单词is出现了"+count+"次");
           
    }

}
截图:

 

 (4).源代码:

package test;
public class Test4 {
    public static void main(String args[]) {
        String str="this is a test of java";
        char c[]=str.toCharArray();
        for(int i=c.length-1;i>=0;i--){                          
        System.out.println(c[i]);
        }
           
    }
 
}
截图:

 

 2.源代码:

package 实验三;
import java.util.Scanner;
public class key {
    public static void main(String[] args){
        System.out.print("请输入一个字符串:");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        char c[]=str.toCharArray();
        char d[]=new char[50];int j=0;
        for(int i = c.length-3;i<c.length;i++) {
                d[j]=c[i];
                j++;
        }
        for(int i=0;i<c.length-3;i++){
            d[j]=c[i];
            j++;
        }
            System.out.print(d);
   
     }
}
截图:

 

 3.源代码:

package test; import java.util.Scanner;

public class test3 {
   
public static void main(String args[]) {
    int count1=0,count2=0,count3=0;
    String str1 ="ddejidsEFALDFfnef2357 3ed";
    char c[]= str1.toCharArray();
    int len=str1.length();
    for(int i=0;i<str1.length();i++) {
        if(c[i]>='A'&&c[i]<'Z') {
            count1++;
        }
        else if(c[i]>='a'&&c[i]<'z') {
            count2++;
        }
        else {
            count3++;
        }
    }
   
   
    System.out.println("大写字母数:"+count1);
    System.out.println("小写字母数:"+count2);
    System.out.println("非英文字母数:"+count3);
    System.out.println("字符串长度 :"+len);
}
   

}

截图:

 

学习总结:

1.继承具有以下特点:
       1.只支持单继承,即一个子类只允许有一个父类。
       2. 子类可以拥有父类的属性和方法
       3. 子类可以拥有自己的属性和方法
       4. 子类可以重写覆盖父类的方法
             提示:为防止子类在想要重写父类方法时写错,造成无法覆盖,我们采用注解@Override,注解是写给编译器的注释,告诉编译器,如果下面的方法没有覆盖父类方法,则给出报错。
2.this和super的区别:
this和super的区别
          (1.)代表的事物不同
              super代表的是父类空间的引用
              this代表的是所属函数的调用者对象
          (2.)使用前提不同
              super必须要有继承关系才能使用
              this不需要继承关系也能使用
          (3.)调用的构造函数不同
               super:调用父类的构造函数
               this:调用所属类的构造函数
posted on 2019-09-27 18:32  吃饭要吃饱  阅读(163)  评论(1编辑  收藏  举报