• 第四周课程总结&实验报告二


    								      第四周课程总结
    

    第四周课程总结

    
        本周重点为学习String;首先String用以创建字符串,且通过有一次课堂练习加强理解到:String 类是不可改变的,一旦创建了 String 对象,那它的值就无法改变了;再是;用于获取有关对象的信息;
       1、 String的两种运用:
            a、直接赋值给对象String定义的对象;
            b、新建栈内存并赋值给与对象;
        2、String 类有很多实用的方法,有些算法直接使用可以简化代码,方便操作;
    
    例:
    
    1	char charAt(int index)
    返回指定索引处的 char 值。
    2	int compareTo(Object o)
    把这个字符串和另一个对象比较。
    3	int compareTo(String anotherString)
    按字典顺序比较两个字符串。
    4	int compareToIgnoreCase(String str)
    按字典顺序比较两个字符串,不考虑大小写。
    5	String concat(String str)
    将指定字符串连接到此字符串的结尾。
    6	boolean contentEquals(StringBuffer sb)
    当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。
    7	static String copyValueOf(char[] data)
    返回指定数组中表示该字符序列的 String。
    8	static String copyValueOf(char[] data, int offset, int count)
    返回指定数组中表示该字符序列的 String。
    9	boolean endsWith(String suffix)
    测试此字符串是否以指定的后缀结束。
    10	boolean equals(Object anObject)
    将此字符串与指定的对象比较。
    11	boolean equalsIgnoreCase(String anotherString)
    将此 String 与另一个 String 比较,不考虑大小写。
    12	byte[] getBytes()
     使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
    13	byte[] getBytes(String charsetName)
    使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
    14	void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
    将字符从此字符串复制到目标字符数组。
    15	int hashCode()
    返回此字符串的哈希码。
    16	int indexOf(int ch)
    返回指定字符在此字符串中第一次出现处的索引。
    17	int indexOf(int ch, int fromIndex)
    返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
    18	int indexOf(String str)
     返回指定子字符串在此字符串中第一次出现处的索引。
    19	int indexOf(String str, int fromIndex)
    返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
    20	String intern()
     返回字符串对象的规范化表示形式。
    21	int lastIndexOf(int ch)
     返回指定字符在此字符串中最后一次出现处的索引。
    
    

    								  实验二 Java简单类与对象
    

    一、 实验目的

    1. 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
    2. 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
    3. 理解static修饰付对类、类成员变量及类方法的影响。
    
    

    二、 实验内容

    1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
        (1) 使用构造函数完成各属性的初始赋值
    
        (2) 使用get…()和set…()的形式完成属性的访问及修改
    
        (3) 提供计算面积的getArea()方法和计算周长的getLength()方法
    
    实验过程:
    

    实验代码:

    Rectangle类:
    
    public class Rectangle 
    {
    	private double width;
    	private double height;
    	private String color;
    	public Rectangle(double width,double height,String color)
    	{
            this.setWidth(width);
            this.setHeight(height);
            this.setColor(color);
    	}
    	
    	public double getWidth() 
    	{
    	return width;
    	}
    	public double getHeight() 
    	{
    	return height;
    	}
    	public String getColor() 
    	{
    	return color;
    	}
    	
    
    	public void setWidth(double width) 
    	{
    	this.width = width;
    	}
    	public void setHeight(double height) 
    	{
    	this.height = height;
    	}
    	public void setColor(String color) 
    	{
    	this.color = color;
    	}
    	
    	
    	public double Area()
    	{
    	return width*height;
    	}
    	public double Perimeter()
    	{
    	return 2*(width+height);
    	}
    }
    
    主类:
    
    public class Juxing 
    {
    
    	public static void main(String[] args) 
    	{
            Rectangle at = null;
            at =new Rectangle(100,360,"purple");
            
            System.out.println("宽      "+at.getWidth());
            System.out.println("高height:     "+at.getHeight());
            System.out.println("颜色color:    "+at.getColor());
            System.out.println("面积area:     "+at.Area());
            System.out.println("周长perimeter:"+at.Perimeter());
    	}
    
    }
    

    实验截图:

    以宽:100;高:360;颜色:紫色为例:
    

    2. 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
    
    实验过程:
    题目分析:要求建立一个类,能用以实现查询账户信息、存款、取款、变更密码等自由操作;
    

    实验代码:

    题目实现的类:
    
    package yinghang;
    import java.util.Scanner;
    
    public class Account 
    {
    	public String id;
    	public int password;
    	public String name;
    	public int money;
    	public String time="2012/08/21 星期天";
    
    	public Account(String id, int password, String name, int money) 
    	{
    		this.id = id;
    		this.password = password;
    		this.name = name;
    		this.money = money;
    		this.time = time;
    	}
    
    	public void show() 
    	{
    		System.out.println("账户标识:" + id);
    		System.out.println("姓名:       " + name);
    		System.out.println("开户日期:"+time);
    		System.out.println("余额:       " + money);
    	}
    	
    	public void takeMoney()
    	{
    		while(true)
    		{
    			Scanner sc = new Scanner(System.in);
    			System.out.println("您好,请输入密码进行验证!");
    			int pass = sc.nextInt();
    			if(pass == password)
    			{
    				System.out.println("请输入需要取款的金额:");
    				int adds = sc.nextInt();
    				if(adds <= money) 
    				{
    					money= money-adds;
    					System.out.println("本次取款为: " + adds);
    					System.out.println("账户余额为:" + money);
    				}
    				else 
    					System.out.println("操作失败,您当前余额不足" );
    				break;
    			}
    			
    			else
    				System.out.println("您输入的密码有误,请重新输入!");
    		}
    	}
    	
    	public void saveMoney(int moneys)
    	{	
    		money = money+moneys;
    		System.out.println("本次存款为: " + moneys);
    		System.out.println("账户余额为:" + money);
    	}
    	
        public void key()
        {
        	System.out.println("设置新密码:");
      
        	Scanner sxc=new Scanner(System.in);
        	int password1=sxc.nextInt();
        	System.out.println("请再次输入:");
        	int password2=sxc.nextInt();
        	if(password1!=password2) 
        	{
        		System.out.println("您两次输入密码不同!");
        		while(true) 
        		{
        			System.out.println("请再次输入:");
        	    	int password3=sxc.nextInt();
        	    	if(password1==password3)
        	    	{
        	    		System.out.println("操作成功");
        	    		break;	
        	    	}
        		}	
        	}
        	else
        		System.out.println("操作成功");
    
        }
    }
    
    
    主类:
    
    package yinghang;
    import java.util.Scanner;
    
    public class SSS 
    {
    
    	public static void main(String[] args) 
    	{
    		Account x = new Account("sss393645216",123456,"鹏",666666666);
    
    		Scanner sx = new Scanner(System.in);
    		System.out.println("请输入需要执行的操作");
    		System.out.println("***1、银行账户信息***");
    		System.out.println("***2、取款操作*******");
    		System.out.println("***3、存款操作*******");
    		System.out.println("***4、修改密码*******");
    		System.out.println("***5、退出系统*******");
    		
    		while(true)
    		{
    			int s = sx.nextInt();
    			int i=0;
    			switch(s) 
    			{
    				case 1:
    					System.out.println("***银行账户信息***");
    					x.show();
    					break;
    				case 2:
    					System.out.println("***取款操作******");
    					x.takeMoney();
    					break;
    				case 3:
    					System.out.println("***存款操作******");
    					x.saveMoney(1000);
    					break;
    				case 4:
    					System.out.println("***修改密码******");
    					x.key();
    					break;
    				case 5:
    					System.out.println("感谢您的使用!");
    					System.exit(0);
    					i=1;
    					
    					break;
    				default:i=1;break;
    			}
    			if(i==1)
    				break;
    		}
    		
    		
    	}
    }
    

    三、 总结:

    第一题总体内容要求较为简单,只是两题都是代码量有些大,其中语法还不太熟练,好在逻辑能理清编写;第二题,一点难在题目要求有些模糊,二是设计量相对较大且复杂,还容易出错,嗯,总体上来说掌握的知识还好。

  • 相关阅读:
    Exercice_3.8
    Exercice_3.13.1_练习使用vector2
    Exercice_3.13_练习使用vetor
    Exercice_3.10_去掉string对象中的标点符号
    Exercice_3.7_判断两个字符串的大小和长度
    1-日期时间视图 2-长按事件
    View(视图)2
    View(视图)
    计算器(UI事件)给按钮添加监听器
    Activity(活动)
  • 原文地址:https://www.cnblogs.com/youlanghua/p/11557284.html
Copyright © 2020-2023  润新知