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

    第四周课程总结

    1.String类
    (1)实例化String类对象:一、String可以直接采用赋值的形式进行处理,这一点感觉上和基本数据类型是非常相似的;二:使用标准的new调用构造方法完成实例化。
    (2)使用equals()方法对String的内容进行比较,以及使用“==”比较字符串内容的区别。
    (3)字符串内容不可改变。
    (4)String类中常用方法:P110~P111。
    2.学习总结:
    实验题第一题自己还能较快得解决,但是第二题就心有余而力不足了。在写第二题的时候参考了其他同学的代码用以解决自己遇到的一些问题,例如:日期显示等。但是自己的代码依旧不够完善,程序在修改了密码后终止运行了,暂时没有找到解决的办法。

    试验报告(二)

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

    源码1:

    package shiyan2;
    
    public class Rectangle {
    	private double w;
    	private double h;
    	private String c;
    	public Rectangle(){
    }
    	public Rectangle(double w,double h,String c){
    		this.setW(w);
    		this.setH(h);
    		this.setC(c);
    	}
    	public void setW(double i) {
    		w=i;
    	}
    	public void setH(double j) {
    		h=j;
    	}
    	public void setC(String k) {
    		c=k;
    	}
    	public double getW() {
    		return w;
    	}
    	public double getH() {
    		return h;
    	}
    	public String getC() {
    		return c;
    	}
    	public double getArea(){
    		return getW()*getH();
    	}
    	public double getLength(){
    		return 2*getW()+2*getH();
    	}
    	public static void main(String [] args){
    		Rectangle R=new Rectangle(11.f,11.f,"red");
    				
    				System.out.println("Color:"+R.c);
    				System.out.println("Area:"+R.getArea());
    				System.out.println("Length:"+R.getLength());
    	}
    }
    

    截图1:

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

    源码2:

    package shiyan2;
    
    import java.util.Scanner;
    import java.util.Date;
    class Account {
    
        private String accId;
        private String name;
        private double money;
        String code;
        private Date date;
    
        public Account(String accId, String name){
            this(accId, name, 0);
        }
    
        private Account(String accId, String name, double money) {
            this.accId = accId;
            this.name = name;
            this.money = money;
            this.code="123456";
            this.date=new Date();
        }
    
        public void saveMoney(double money) {
            if (money <= 0) {
                System.out.println("存款金额必须大于0");
            }
            this.money += money;
            System.out.println("存款成功"+"
    "+"存入:"+money+"元");
        }
    
        public double getMoney(double money) {
            if (money <= 0) {
                System.out.println("取款金额必须大于0");
                return 0;
            }
            if (this.money <= money) {
                System.out.println("余额不足,无法取款");
                return 0;
            }
            this.money -= money;
            System.out.println("取款成功"+"
    "+"取出:"+money+"元");
            return money;
        }
        public String alter(String code){
            this.code=code;
            System.out.println("修改成功");
            return code;
        }
    
        public double getBalance() {
            return this.money;
        }
    
        public double getOverdraft() {
            return 0;
        }
        public void tell(){
            System.out.println("账户:"+accId+"
    "+"姓名:"+name+"
    "+"开户日期"+date+"
    "+"余额:"+money+"
    
    
    ");
        }
        public void tell1(){
            System.out.println("账户:"+accId);
        }
        public void tell2(){
            System.out.println("姓名:"+name);
        }
        public void tell3(){
            System.out.println("余额:"+money+"元");
        }
    }
    class Bank{
        public static void main(String[] args){
            System.out.println("请输入开户人姓名");
            @SuppressWarnings("resource")
    		Scanner m=new Scanner(System.in);
            String v=m.next();
            Account per=new Account("98765432101",v);
            per.tell();
            System.out.println("请输入密码");
            @SuppressWarnings("resource")
    		Scanner x=new Scanner(System.in);
            String chr=x.next();
            while(chr.equals(per.code)) {
            	System.out.println("1.取款
    2.存款
    3.变更密码
    4.账户标识
    5.姓名
    6.当前余额
    7.退出"+"
    
    ");
            	@SuppressWarnings("resource")
            	Scanner n=new Scanner(System.in);
                int t=n.nextInt();
                if(t>6) {
                	System.out.println("结束操作");
                	break;
                	}
                switch(t){
                case 1:
                	System.out.println("请输入取款金额");
                	Scanner a=new Scanner(System.in);
                	double y=a.nextDouble();
                	per.getMoney(y);
                	break;
                	case 2:
                		System.out.println("请放入存款金额");
                		Scanner b=new Scanner(System.in);
                        double s=b.nextDouble();
                        per.saveMoney(s);
                        break;
                    case 3:
                        Scanner z=new Scanner(System.in);
                        String c=z.next();
                        per.alter(c);
                        break;
                    case 4:
                        per.tell1();
                        break;
                    case 5:
                        per.tell2();
                        break;
                    case 6:
                        per.tell3();
                        break;
                    
                }
            }
            }
    }
    

    截图2:

  • 相关阅读:
    Sql Server 2008卸载后再次安装一直报错
    listbox 报错 Cannot have multiple items selected when the SelectionMode is Single.
    Sql Server 2008修改Sa密码
    学习正则表达式
    Sql Server 查询第30条数据到第40条记录数
    Sql Server 复制表
    Sql 常见面试题
    Sql Server 简单查询 异步服务器更新语句
    jQuery stop()用法以及案例展示
    CSS3打造不断旋转的CD封面
  • 原文地址:https://www.cnblogs.com/leichen210/p/11558751.html
Copyright © 2011-2022 走看看