记录我的成长吧~

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

实验报告

1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值

(2) 使用get…()和set…()的形式完成属性的访问及修改

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

class Rectangle {
    private double width,height;
    private  String color;
    public Rectangle(double width,double height,String color){
        this.setWidth(width);
        this.setHeight(height);
        this.setColor(color);
    }
    public void tell(){
        System.out.println("宽:"+width+"  "+"高:"+height+"  "+"颜色:"+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 void getArea(){
        System.out.println("面积为:"+(width*height));
    }
    public void getLength(){
        System.out.println("周长为:"+(width+height)*2);
    }

}
public class Test{
     public static void main(String[] args){
         Rectangle per=new Rectangle(30,60,"yellow");
         per.tell();
         per.getArea();
         per.getLength();
     }
}

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

import java.util.Scanner;
class Account {

    private String accId;
    private String name;
    private double money;
    String code;
    private String date;

    public Account(String accId, String name,String date){
        this(accId, name, 0,date);
    }

    private Account(String accId, String name, double money,String date) {
        this.accId = accId;
        this.name = name;
        this.money = money;
        this.code="123456";
        this.date=date;
    }

    public void saveMoney(double money) {
        if (money <= 0) {
            System.out.println("存款金额必须大于0");
        }
        this.money += money;
        System.out.println("存款成功"+"\n"+"账户余额:"+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("取款成功"+"\n"+"账户余额:"+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+"\n"+"姓名:"+name+"\n"+"开户日期"+date+"\n"+"余额:"+money+"\n\n\n");
    }
    public void tell1(){
        System.out.println("账户:"+accId);
    }
    public void tell2(){
        System.out.println("姓名:"+name);
    }
    public void tell3(){
        System.out.println("余额:"+money);
    }
}
class Test1{
    public static void main(String[] args){
        System.out.println("请输入开户人姓名");
        Scanner m=new Scanner(System.in);
        String v=m.next();
        System.out.println("请输入开户日期");
        Scanner p=new Scanner(System.in);
        String g=p.next();
        Account per=new Account("c1476351119",v,g);
        per.tell();
        System.out.println("请输入密码");
        Scanner x=new Scanner(System.in);
        String chr=x.next();
        if(chr.equals(per.code)){
            System.out.println("1.取款\n2.存款\n3.变更密码\n4.账户标识\n5.姓名\n6.当前余额"+"\n\n\n");
            Scanner n=new Scanner(System.in);
            int t=n.nextInt();
            switch(t){
                case 1:
                    System.out.println("请输入取款金额");
                    Scanner q=new Scanner(System.in);
                    double y=q.nextDouble();
                    per.getMoney(y);
                    break;
                case 2:
                    System.out.println("请放入存款金额");
                    Scanner c=new Scanner(System.in);
                    double s=c.nextDouble();
                    per.saveMoney(s);
                    break;
                case 3:
                    Scanner z=new Scanner(System.in);
                    String b=z.next();
                    per.alter(b);
                    break;
                case 4:
                    per.tell1();
                    break;
                case 5:
                    per.tell2();
                    break;
                case 6:
                    per.tell3();
                    break;
            }
        }
        else{
            System.out.println("密码错误");
        }
    }
}

课程总结

本周学习了String类的多种方法,对于我们做问题有很大的帮助,还有如果发现不会的问题要多去看帮助文档,帮助文档上面的解析也是很清楚的,对于Java还是有很多疑问的,上课听了还算听得懂,但是课后还要多练习,不然过几天就会忘记,所以要多练。

posted @ 2019-09-19 18:51  ToT!  阅读(332)  评论(1编辑  收藏  举报

愛生活 愛自己

你是最棒的