zoukankan      html  css  js  c++  java
  • 第五周课程总结&试验报告

    实验三 String类的应用

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

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

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

    public class new2 {
        public static void main(String[] args) {
            String x="this is a test of java";
            int sum1=0;
            char[] a=x.toCharArray();
            for(int i=0;i<a.length;i++) {
                if(a[i]=='s')
                    sum1++;
            }
            System.out.println("字符串中字母s出现的次数:"+sum1);
            
            
            int sum2=0;
            for(int i=0;i<x.length();i++) {
                if('i'==x.charAt(i)&&'s'==x.charAt(i+1)) {
                  sum2++;
                }
            }
            System.out.println("字符串中子串“is”出现的次数:"+sum2);
            
            
            int sum3=0;
            String[] str=x.split(" ");
            for(String e:str){
                if(e.equals("is")) {
                  sum3++;
                }
            }
            System.out.println("字符串中单词“is”出现的次数:"+sum3);
    
          
            char a1[]=x.toCharArray();
            System.out.print("字符串的倒序输出:");
            for(int i=x.length()-1;i>=0;i--){
                System.out.print(a1[i]);
            }
      }
    }
    

    2)实验结果

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

    1)实验代码

    import java.util.*;
    public class new1 {
    
        public static void main(String[] args) {
             System.out.println("输入一个字符串:");
             
    	     Scanner z=new Scanner(System.in);   
             String str=z.next();
             char x[]= str.toCharArray();
             System.out.println("输出加密后结果:");
             
             for(char y:x) {
                  System.out.print((char)(y+3));
             }
         }
    }
    

    2)实验结果

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

    1)实验代码

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

    2)实验结果

    3.本周总结

    1、子类拥有父类非 private 的属性、方法
    2、类中有多个构造方法的话,可以用this关键字相互调用
    3、Java 的继承可以单继承也可以多继承
    4、子类对象在实例化之前会默认调用父类中的构造方法
    5、普通类的子类可以根据自己的需要选择性的进行某些父类方法的覆写,所以普通方法无法对子类的覆写进行限制,而抽象方法却可以强制要求子类覆写父类方法。

  • 相关阅读:
    js简单验证码的生成和验证
    基本够用的php.ini配置文件(CentOS7)
    转发:CentOS下tar压缩排除某个文件夹或文件及解压
    阿里云服务器CentOS7 vsftp安装、设置及后台端口的设置
    转发:entos7修改文件夹权限和用户名用户组
    转发:查看centos中的用户和用户组
    阿里云服务器CentOS7怎么分区格式化/挂载硬盘
    php调试用的几个小方法
    Jquery实现日期转换为 Unix时间戳及时间戳转换日期
    Jquery计算时间戳之间的差值,可返回年,月,日,小时等
  • 原文地址:https://www.cnblogs.com/Emotional/p/11598946.html
Copyright © 2011-2022 走看看