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


    实验三 String类的应用

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

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

    1.统计该字符串中字母s出现的次数。

    package hello.java;
    
    public class hh {
    
    public static void main(String[] args) {
     int count=0;
     String str ="this is test of java";
     char[] cha =str.toCharArray();
     for(int i=0;i<cha.length;i++)
     {
    if(cha[i]=='s') {
    
    	 count++;
    		 
    	 }
         }
     System.out.println(count);
    }
    }
    

    2.统计该字符串中子串“is”出现的次数。

    package hello.java;
    
    public class hh {
    
    public static void main(String[] args) {
     int count=0;
     String str ="this is test of java";
      char [] cha =str.toCharArray();
     for(int i=0;i<cha.length;i++)
     {
    if((cha[i]=='i')&&(cha[i+1]=='s'))
    {
    
    	 count++;
    		 
    	 }
     }
     System.out.println(count);
    }
    }
    

    3.统计该字符串中单词“is”出现的次数。

    package hello.java;
    
    public class hh {
    
    public static void main(String[] args) {
     int count=0;
     String str ="this is test of java";
      char [] cha =str.toCharArray();
     for(int i=0;i<cha.length;i++)
     {
    if((cha[i]=='i')&&(cha[i+1]=='s')&&(cha[i-1]==' '))
    {
    
    	 count++;
    		 
    	 }
     }
     System.out.println(count);
    }
    }
    

    4.实现该字符串的倒序输出。

    package hello.java;
    
    public class hh {
    
    public static void main(String[] args) {
     int count=0;
     String str ="this is test of java";
      char [] cha =str.toCharArray();
     for(int i=19;i>=0;i--)
     {
    
    		 
    	     
     System.out.println(cha[i]);
    }
    }
    }
    

    遇到的问题:上面说字符长度超过20的界限,超过我不就直接填个19,结果就解决了

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

              package hello.java;
    
         import java.util.Scanner;
    
          public class heyong {
        public static void main(String[] args) {
            System.out.println("请输入字符串:");
            Scanner sc=new Scanner(System.in);
            String str1=sc.next();
            char c[] = str1.toCharArray();
            System.out.println("加密后的结果");
            for(int i=0;i<str1.length();i++){
                System.out.print( (char)(c[i]+3));
               }
           }
       }
    

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

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

    第五周课程总结

    继承包括:方式重载,方法覆写

    笔记:

    1.当子类实例化时首先父亲得有构造方

    2.只允许多层继承不能多重继承

    3.子类可以直接用super()调用父亲中的无参构造

    区别点 重载 覆写
    单词 overloading overriding
    定义 方法名称相同,参数的类型或个数不同.对权限没要求。 方法名称,参数的类型,返回值类型全部相同.被覆写的方法不能拥有更严格的权限
    范围 发生在一个类中 发生在继承类中

    super关键字的运用

    知识注意点:super,this不能同用

    区别点 this super
    属性访问 访问本类中的属性,如果本类没有此属性则从父类中继续查找 访问父亲中的属性
    方法 访问本类中的方法,如果本类中没有此方法,则从父类中继续查找 直接访问父类中的方法
    通用构造 调用本类构造,必须放在构造方法的首行 调用父亲构造,必须放在子类构造方法首行
    特殊 表示当前对象 无此概念

    final关键字

    1.使用final声明的类不能有子类

    2.使用final声明的方法不能被子类覆写

    3.使用final声明的变量即成为常量,常量不可修改

    抽象类的基本概念

    1.包含一个抽象方法的类必须是抽象类(还有可能是接口)

    2.抽象类和抽象方法都要使用abstract关键字声明

    3.抽象方法只需要声明而不需要实现

    4.抽象类必须被子类继承,子类(如果不是抽象类)必须覆写抽象类中的全部抽象方法

    5.抽象类的定义其实就是比普通类多了一些抽象方法而已,其他地方与普通类的组成基本上都是一样的

    对象的多态性

    向上类型:子类对象->父类对象(自动类型转换)

    向下类型:父类对象->子类对象(强制类型转换)

  • 相关阅读:
    UVA 10480 Sabotage (最大流最小割)
    bzoj2002 [Hnoi2010]Bounce 弹飞绵羊 (分块)
    poj3580 SuperMemo (Splay+区间内向一个方向移动)
    bzoj1500: [NOI2005]维修数列 (Splay+变态题)
    hdu3436 Queue-jumpers(Splay)
    hdu4710 Balls Rearrangement(数学公式+取模)
    hdu1890 Robotic Sort (splay+区间翻转单点更新)
    zoj2112 Dynamic Rankings (主席树 || 树套树)
    poj3581 Sequence (后缀数组)
    notepa++ Emmet的安装方法
  • 原文地址:https://www.cnblogs.com/1793979463hyx/p/11578363.html
Copyright © 2020-2023  润新知