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

实验三 String类的应用

实验目的

掌握类String类的使用;
学会使用JDK帮助文档;

实验内容

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

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

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

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

1、源代码:

public class 第一小题 {
     public static void main(String[] args) {
         String str="This is a test of java";
         int num1=0;
         for(int i=0;i<str.length();i++) {
             if(str.charAt(i)=='s')
            	 num1++;
        }
         System.out.println("字母s出现的次数:"+num1);
     }
}

结果截图:

设计思路:
书上有思路,翻了书。

2、源代码:

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

运行截图:

3、源代码:

public class 第三小问 {
    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);
    }
}

运行截图:

4、源代码:

public class 第四小问 {
    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]);
        }
        
    }

}

运行截图:

设计思路:
运用书上111页的方法5转为数组,然后i--,遍历数组。

第二大题:
源代码:

import java.util.*;
public class 用户加密{
    public static void main(String[] args) 
    {
        Scanner s=new Scanner(System.in);
        String b=s.nextLine();
        char c[]=b.toCharArray();
        char a[]=new char [c.length];
        int i,j=0;
        if(c.length==1)
        {
            System.out.println(c[0]);
        }
        if(c.length==2) 
        {
            System.out.print(c[1]);
            System.out.println(c[0]);
        }
        else
            for(i=c.length-3;i<c.length;i++)
            {
                a[j]=c[i];
                j++;
            }
            for(i=0;i<c.length-3;i++)
            {
                a[j]=c[i];
                j++;
            }
            String str=String.valueOf(a);
            System.out.print(a);
    }
}

运行截图:



设计思路:
题目要求每个字符都向后移动三位,那有可能会出现字符是一位或两位的情况,也需要考虑;在网上查找了一些java语句。



第三大题:
源代码:

public class 大写小写非字母数 {
    public static void main(String[] args) 
    {
        String str="ddejidsEFALDFfnef2357 3ed";
        int big=0, small=0, none=0;
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)>=65&&str.charAt(i)<=90)
            {
                big++;
            }
            else if(str.charAt(i)>=97&&str.charAt(i)<=122)
            {
                small++;
            }
            else
                none++;
        }
        System.out.println("大写字母数:"+big);
        System.out.println("小写英文字母数:"+small);
        System.out.println("非英文字母数:"+none);
    }

}

运行结果截图:

设计思路:
这题的细节就是用ASCII码来当做判断条件。

课程总结

这周讲了面向对象高级篇,其中有继承、多态、覆写、重载、final和抽象类的基本概念。
继承:
只允许多层继承不能多重继承;
super和this不能同时使用,因为他们都要在首行;
子类不能直接访问父类中的私有操作,可以通过其他操作间接访问(setter或getter);
要注意一点,记得在父类中加一个无参数的构造方法。
多态:
向上转型为自动转型,向下转型为强制转型,需要首先向上转型再强制向下。
覆写:
3种访问权限的大小关系:private<default<public;

main方法不能使用this和super。
final关键字:
定义类的前面,不能有子类;
定义属性的前面,所声明的变量即为常量,常量不可修改;
定义方法的前面,不能被子类所覆写。
抽象类:java可以创建一种类,专门用来当做父类,这种类称为抽象类,相当于模板。
{{uploading-image-414638.png(uploading...)}}(这张图片不知为什么上传不了,内容是书上175页,关于抽象类的使用规则及定义)。
抽象类不能final,因为彼此矛盾。

原文地址:https://www.cnblogs.com/qiuf99999/p/11593215.html