第三次实验报告&&学习总结

实验三 String类的应用

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

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

  • 统计该字符串中字母s出现的次数。
  • 代码:
  • public class Wrold{
        public static void main(String[] args) {
            String str="this is a test of java";
            int count=0;
            for(int i=0;i<str.length();i++) {
                if(str.charAt(i)=='s')
                    count++;
            }
            System.out.println("字符串里面“s”出现的次数:"+count);
        }
    }

    截图:

  • 统计该字符串中子串“is”出现的次数。
  • 代码:
    public class Wrold{
        public static void main(String[] args) {
            String str="this is a test of java";
            int count=0;
            for(int i=0;i<str.length();i++) {
                if(str.charAt(i)=='i'&&str.charAt(i+1)=='s')
                    count++;
            }
            System.out.println("字符串中子串“is”出现的次数:"+count);
        }
    }

    截图:

  • 统计该字符串中单词“is”出现的次数。
  • 代码:
    public class Wrold{
        public static void main(String[] args) {
            String str="this is a test of java";
            String s[]=str.split(" ");
            int count=0;
            for(int i=0;i<s.length;i++) {
                if(s[i].equals("is"))
                    count++;
            }
            System.out.println("该字符串中单词“is”出现的次数:"+count);
        }
    }

    截图:

  • 实现该字符串的倒序输出。
  • 代码:
    public class Wrold{
        public static void main(String[] args) {
            StringBuffer n=new StringBuffer();
            n.append("this is a test of java");
            String str=n.reverse().toString();
            System.out.println(str);
        }
    }

    截图:

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

 代码:

public class Wrold{
    public static void main(String[] args){
        String str="123456";
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        char m[]=str.toCharArray();
        char n[]=new char[50];int j=0;
        for(int i = m.length-3;i<m.length;i++) {
                n[j]=m[i];
                j++;
        }
        for(int i=0;i<m.length-3;i++){
            n[j]=m[i];
            j++;
        }
            System.out.print(n);
    
     }
}

截图:

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

 代码:

public class Wrold{
    public static void main(String[] args) {
        String str="ddejidsEFALDFfnef2357 3ed";
        int a=0, b=0, c=0;
        for(int i=0;i<str.length();i++) {
            if(str.charAt(i)>=70&&str.charAt(i)<=95) {
                a++;
            }
            else if(str.charAt(i)>=70&&str.charAt(i)<=100) {
                b++;
            }
            else
                c++;
        }
        System.out.println("大写字母数:"+a);
        System.out.println("小写英文字母数:"+b);
        System.out.println("非英文字母数:"+c);
    }

}

截图:

 学习总结:本次实验第一部分主要是运用string类,老师在上课的时候大部分都讲了,我也拍了图。第二题,运行不出来,程序我觉得应该没有问题

this与super关键字的区别:

 this:当前对象

                super:直接父类对象

                this():当前类的无参构造方法,也可以指定有参的如:this(a)

                super():直接父类的无参构造方法,也可以指定有参的如:super(a)

注意:1,当在方法内调用一个变量b,编译器会向上遍历,直到找到最近的一个引用变量为止:b—>this.b—>super.b,如果未找到,编译器将显式提                   示错误信息;

           2,当变量b的定义只发生在父类时,此时  b=this.b=super.b;

           3,当局部变量b覆盖成员变量b时,使用this.b调用成员变量,此时的成员变量包括子类新增和继承的变量,不包含隐藏变量;

           4,当子类成员变量b覆盖父类成员变量b时,使用super.b调用这个被隐藏的成员变量;

           5,当子类重写了父类方法method(),可以使用super.method()来调用父类被隐藏的方法;

           6,super()与this()具备硬性使用条件,否则编译无法通过——Constructor call must be the first statement in a constructor.即

                   二者出现的位置必须是构造方法的第一行。

继承:Java中类只允许单一继承。

重载与覆盖:

书上169页.

猜你喜欢

转载自www.cnblogs.com/qxc0524/p/11599747.html