zoukankan      html  css  js  c++  java
  • 第五周课程总结&试验报告(三)

    实验三 String类的应用

    实验目的
    掌握类String类的使用;
    学会使用JDK帮助文档;
    实验内容
    1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)

    统计该字符串中字母s出现的次数。
    统计该字符串中子串“is”出现的次数。
    统计该字符串中单词“is”出现的次数。
    实现该字符串的倒序输出。
    2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

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

    1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
    1)统计该字符串中字母s出现的次数。
    实验代码

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

    实验结果:

    2)统计该字符串中子串“is”出现的次数
    实验代码

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

    实验结果:

    3)统计该字符串中单词“is”出现的次数。
    实验代码

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

    实验结果:

    4)实现该字符串的倒序输出。
    实验代码

    public static void main(String[] args) {
    		String s="this is a test of java";
    		char[]a=s.toCharArray();
    		for(int i=a.length-1;i>=0;i--) {
    			System.out.print(a[i]);
    		}
    	}	
    

    实验结果:

    遇到的问题:刚开始用Indexof来写,因为对这个知识点掌握的不够好,没写出来,所以换了一种思路用数组来写一下就搞完了。

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

    import java.util.Scanner;
    public class Account {
    	public static void main(String[] args) {
    		Scanner s=new Scanner(System.in);
    	    String a=s.nextLine();
    	    char[]b=a.toCharArray();
    	    int i=0,k = 3;
    	   int j=b.length-1;
    	   char t;
    	   for(i=0,j=b.length-1;i<j;i++,j--) {       //逆置数组元数
    	    	t=b[i];
    	    	b[i]=b[j];
    	    	b[j]= t;
    	    }
    		for(i=0,j=k-1;i<j;i++,j--) {                  //逆置前k个元素
    			t=b[i];
    	    	b[i]=b[j];
    	    	b[j]= t;
    		}
    	    for(i=k,j=b.length-1;i<j;i++,j--) {           //逆置后面的剩余元素
    	    	t=b[i];
    	    	b[i]=b[j];
    	    	b[j]= t;
    	    }
    		System.out.println(String.valueOf(b));
    	    }
    	}
    

    实验结果:

    遇到的问题:做这道题时我以为就是字符简单的向后移动三位,所以没一下就做完了,直到李钰发了老师的实例之后我才发现我的想法是多么幼稚,所以最后我用了字符逆置的方法来写终于写对了。

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

    public class b {
    	public static void main(String[] args) {
    		String s="ddejidsEFALDFfnef2357 3ed";
    		char[]a=s.toCharArray();
    		int b=0;
    		int c=0;
    		int d=0;
    		for(int i=0;i<a.length;i++) {                          //大写字母个数
    			if(a[i]>='A'&&a[i]<='Z') {
    				b++;
    			}
    			else if(a[i]>='A'+32&&a[i]<='Z'+32) {      //小写字母个数
    				c++;
    			}
    			else {                              //非英文字母个数
    				d++;
    			}
    		}
    		System.out.println("大写字母数为:"+b);
    		System.out.println("小写字母数为:"+c);
    		System.out.println("非英文字母数为:"+d);
    	}
    }
    

    实验结果:

    遇到的问题:不知道ascall码值,通过查找知道了大写字母与小写字母相差32.
    本周学到的知识:
    String类常用操作方法,super,final关键字,以及抽象类的基本概念。

    总结:
    这周作业感觉很轻松,没有难题,而且加深了对string类用法的认识,虽然其中饶了一些弯,但是最后还是写出来了。

    super关键字
    1).使用super可以访问父类中的方法和属性。
    2).调用父类构造,必须放在子类构造方法的首行,因此与this不能同时出现。
    final关键字
    1).使用final声明的类不能有子类。
    2).使用final声明的方法不能被子类所覆写。
    3).使用final声明的变量即为常量,不可修改。
    继承

    class 父类{}        //定义父类
    class 子类 extends 父类{}            //使用extends关键字实现继承
    
    public class Person {
    	private String cat;
    	private String dog;
        public void setCat(String cat) {
    	this.cat=cat;
        }
        public String getCat() {
        	return cat;
        }
        public void setDog(String dog) {
        	this.dog=dog;
        }
        public String getDog() {
        	return dog;
        }
    static class Student extends Person{
    	private String name;
    	public void setName(String name) {
    		this.name=name;
    	}
    	public String getName() {
    		return name;
    	}
    
    public  static void main(String[] args) {
    	Student stu=new Student();
    	stu.setName("小明");
    	stu.setCat("maiomaiomiao");
    	stu.setDog("wangwangwang");
    	System.out.println("姓名:"+stu.getName());
    	System.out.println("小猫:"+stu.getCat());
    	System.out.println("小狗:"+stu.getDog());	
    }
    }
    }
    
  • 相关阅读:
    Linux入门之常用命令(14) kill
    【JVM命令系列】jstack
    【JVM命令系列】javap
    【JVM命令系列】jmap
    Hadoop安全(2)——————UserGroupInformation
    Hadoop安全(1)——————美团Hadoop安全实践
    软件测试流程进阶----两年软件测试总结
    每个程序员应该阅读的10本书籍
    成为优秀程序员的黄金10条法则
    Java异常的深入研究与分析
  • 原文地址:https://www.cnblogs.com/tzmad/p/11593885.html
Copyright © 2011-2022 走看看