Java(肆)

第五周课程总结:

   

实验三 String类的应用

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

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

  • 1.统计该字符串中字母s出现的次数。
  • 实验源码:
  • public class text1 {
     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.统计该字符串中子串“is”出现的次数。
  • 实验源码:
  • public class texe2 {
     public static void main(String[] args) {
            String str = "this is a test of java";
            int count = 0, index = 0;
            while (true) {
                int n = str.indexOf("is", index);
                if (n != -1) {
                    index = n + 1;
                    count++;
                } else {
                    break;
                }
            }
            System.out.print("is出现了" + count + "次");
        }
    }
  • 截图:
  • 3.统计该字符串中单词“is”出现的次数
  • 实验源码:
  • public class text3 {
     public static void main(String[] args) {
            int count=0;
            String str="this is a test of java";
            String s[]=str.split(" ");
            for(int i=0;i<s.length;i++){
                if(s[i].equals("is")){
                    count++;       
                }
            }
            System.out.println(count);
        }
    }
  • 截图:

  • 4.实现该字符串的倒序输出。
  • 实验源码:
  • public class text4 {
     public static void main(String[] args) {
            String str = "this is a test of java";
            char s[] = str.toCharArray();
            for (int i=s.length-1;i>=0;i--) {
                System.out.print(s[i]);
            }
        }
    }
  • 截图:
  • 总结:老师上课的时候基本都通过eclipse为我们演示了一遍,基本操作就是这么一个情况;

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

 

实验源码:

import java.util.*;
public class text1 {
 public static void main(String[] args) {
        System.out.print("输入一个字符串:");
        var scanner = new Scanner(System.in);
  String str1 = scanner.nextLine();
        System.out.println(str1);
        char c[] = str1.toCharArray();
        int ASCII;
        char c1;
        for (int i = 0; i < c.length; i++) {
            ASCII = c[i] + 3;
            // System.out.print(ASCII+" ");
            c1 = (char) ASCII;
            // System.out.print(c1+" ");
            String s = String.valueOf(c1);
            System.out.print(s);
        }
    }
}
截图:

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

实验源码:

public class text1 {
 public static void main(String[] args) {
        String str = "adhadhahdhahhH 1231";
        char c[] = str.toCharArray();
        int count1 = 0, count2 = 0, count3 = 0;
        for (int i = 0; i < c.length; i++) {
            int n = (int) c[i];
            if (65 <= n && n <= 90) {
                count1++;
            } else if (97 <= n && n <= 122) {
                count2++;
            } else {
                count3++;
            }
        }
        System.out.println("大写字母数:" + count1);
        System.out.println("小写字母数:" + count2);
        System.out.println("非英文字母数:" + count3);
    }
 
}
截图:

 课程总结:

表格:

继承(extends);

方法的重载与覆写的区别:

区别点

重载

覆写

单词

Overloading

Overriding

定义

方法名称相同,参数的类型或个数不同

方法名称、参数类型、返回值类型全部相同

定义

对权限没有要求

被覆写的方法中不能有更严格的权限

范围

发生在一个类中

发生在继承中

super关键字的使用;

final关键字:类&属性&方法;

.....受益匪浅.....

朴实无华且枯燥

猜你喜欢

转载自www.cnblogs.com/lsy2380821-/p/11598865.html