zoukankan      html  css  js  c++  java
  • 第五周课程总结及实验报告

    实验三 String类的应用

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

    实验内容

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

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

    1.1实验代码

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

    运行截图

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

    1.2实验代码

    package text4;
    
    public class Text4 {
    
    	public static void main(String[] args) {
    		String str=new String("this is a test of java");
    		int count=0,a=0;
    		while(str.indexOf("is",a)!=-1) {
    			count++;
    			a=str.indexOf("is",a)+2;
    		}
    		System.out.println(count);
    
    	}
    
    }
    

    运行截图

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

    1.3实验代码

    package text4;
    
    public class Text4 {
    
    	public static void main(String[] args) {
    		String str=new String("this is a test of java");
    		int count=0,i;
    		String str1[]=str.split(" ");
    		for(i=0;i<str1.length;i++) {
    			if(str1[i].equals("is")==true) {
    				count++;
    			}
    		}
    		System.out.println(count);
    
    	}
    
    }
    

    运行截图

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

    实验代码

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

    运行截图

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

    2.实验代码

    package text4;
    
    import java.util.Scanner;
    
    public class Text4 {
    
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		System.out.print("输入一个字符串:");
    		String str=sc.nextLine();
    		char a[]=new char[str.length()];
    		char b[]=new char[str.length()];
    		a=str.toCharArray();
    		int i;
    		for(i=0;i<str.length();i++) {
    			if(a[i]>='a'&&a[i]<='z') {
    				if(a[i]=='x') {
    					b[i]='a';
    				}
    				if(a[i]=='y') {
    					b[i]='b';
    				}
    				if(a[i]=='z') {
    					b[i]='c';
    				}
    				if(a[i]>='a'&&a[i]<'x') {
    					b[i]=(char) (a[i]+3);
    				}
    			}
    			if(a[i]>='A'&&a[i]<='Z') {
    				if(a[i]=='X') {
    					b[i]='A';
    				}
    				if(a[i]=='Y') {
    					b[i]='B';
    				}
    				if(a[i]=='Z') {
    					b[i]='C';
    				}
    				if(a[i]>='A'&&a[i]<'X') {
    					b[i]=(char) (a[i]+3);
    				}
    			}
    		}
    		String str2=new String(b);
    		System.out.print("加密后的密码:");
    		System.out.println(str2);
    		sc.close();
    		
    	}
    
    }
    

    运行截图

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

    3.实验代码

    package text4;
    
    public class Text43 {
    
    	public static void main(String[] args) {
    		String str="ddejidsEFALDFfnef2357 3ed";
    		char a[]=new char[str.length()];
    		a=str.toCharArray();
    		int i;
    		int shuzi=0,da=0,xiao=0;
    		for(i=0;i<str.length();i++) {
    			if(a[i]<='9'&&a[i]>='0'||a[i]==' ') {
    				shuzi++;
    			}
    			if(a[i]<='z'&&a[i]>='a') {
    				xiao++;
    			}
    			if(a[i]<='Z'&&a[i]>='A') {
    				da++;
    			}
    		}
    		System.out.println("非英文字母数:"+shuzi);
    		System.out.println("小写字母:"+xiao);
    		System.out.println("大写字母:"+da);
    	}
    
    }
    

    运行截图

    总的来说呢,这次的实验题目做起来还算顺手,主要就是调用了一些string的函数。

    课程总结

    一:学习了Java的另一特性继承性

    1)继承格式

    class 父类{}
    class 子类 extends{}
    

    2)可以通过子类扩展父类
    3)只允许多层继承,不允许多重继承。
    4)子类不能直接访问父类中的私有操作,但可以通过get方法访问。
    5)子类构造方法中的super()。一般写类的时候要构造一个空方法,因为一般子类对象实例化之前会先默认调用父类中的构造方法,如果父类中没有构造方法,就会默认调用一个空的构造方法。 super()就是调用父类构造方法的语句。

    二:方法的覆写和重载

    覆写:就是指子类中定义了与父类中同名的方法,但是要考虑权限,被子类覆写的方法不能拥有比父类方法更严格的访问权限,例如:父类(private)<=子类(private||public)。
    重载:同一个类中相同名称不同参数的方法。

    三、final关键字(表示的意思是最终的意思,也可以称为完结器)

    1)使用final声明的类不能有子类
    2)使用final声明的方法不能被子类所覆写
    3)使用final声明的变量级成为常量,常量不可以修改

    四、抽象类

    1)包含一个抽象方法的类必须是抽象类。抽象方法如下:

    访问权限 abstract 返回值类型 方法名称(参数);
    

    2)抽象类和抽象方法都要使用abstract关键字声明
    3)抽象方法只需声明而不需要实现
    4)抽象类必须被子类继承,子类(如果不是抽象类)必须填写抽象类中的全部抽象方法

    五、对象的多态性

    1)向上转型:子类对象→父类对象(自动转换)
    2)向下转型:父类对象→子类对象(强制转换)

    加密

    import java.util.Scanner;
    public class Test532 {
       public static void main(String[] args) {
                   Scanner s=new Scanner(System.in);
                   String str = s.next();
                   char a[]= str.toCharArray();
                   char b[] = new char[10] ;
                   int n=0;
                   for(int i = a.length-3;i<a.length;i++) {
                       b[n]=a[i];
                           n++;
                   }
                   for(int j=0;j<a.length-3;j++) {
                       b[n]=a[j];
                           n++;
                   }
                      System.out.println(b);
               }
    }
    

  • 相关阅读:
    用XPath定位Web页面元素时,如何快速验证XPath语句是否正确?
    遇到Web页面禁用鼠标右键操作时,该如何解禁?
    Python之win32模块
    Python之smtplib模块
    Python之telnetlib模块
    使用HttpRunner3+Allure+Jenkins实现Web接口自动化测试
    Python实现Thrift Server
    Python之requests模块-hook
    php新手常用的函数(随时更新)
    手机测试pc端网页
  • 原文地址:https://www.cnblogs.com/H-Alice/p/11549238.html
Copyright © 2011-2022 走看看