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

实验三 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);
}
}

1580643-20190926230459421-1599302260.png

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);
}
}

1580643-20190926231152907-310228845.png

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);
}
}

1580643-20190926231502555-1837031931.png

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]);
}
}
}

1580643-20190926232127507-1431133201.png

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

1580643-20190926233219179-1527525467.png

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

1580643-20190926233343078-736224805.png

          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));
           }
       }
   }

1580643-20190927231004288-1944766096.png

(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);
 }
}

1580643-20190927165347074-1102897286.png

第五周课程总结

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

笔记:

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

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

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

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

super关键字的运用

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

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

final关键字

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

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

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

抽象类的基本概念

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

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

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

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

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

对象的多态性

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

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

转载于:https://www.cnblogs.com/1793979463hyx/p/11578363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值