别着急打代码,等我抽根烟先————<( ̄ c ̄)y▂ξ 抽烟———— 博客首页

第四周课程总结&试验报告(二)

Java实验报告

 

班级:计科二班        学号:20188435       姓名:徐鸿敏         完成时间:

评分等级

实验二 Java简单类与对象

一、实验目的

(1) 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;

(2) 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;

(3) 理解static修饰付对类、类成员变量及类方法的影响。

二、实验内容

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

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

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

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

 

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

三、实验过程

(1)Rectangle矩形

1.源代码

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 void tell(){
            System.out.println("宽:"+ width + ",高:"+ height + ",颜色:"+ color);
        }
        public double getWidth(){
            return width;
        }
        public void setWidth(double w){
            width=w;
        }
        public double getHeight(){
            return height;
        }
        public void setHeight(double h){
            height=h;
        }
        public String getColor(){
            return color;
        }
        public void setColor(String c) {
            color=c;
        }
        public void getArea(){
            System.out.println("面积为:"+(width*height));
        }
        public void getLength(){
            System.out.println("周长为:"+(width+height)*2);
        }

    }
        class Rec{
        public static void main(String[] args){
            Rectangle per=new Rectangle(30,40,"黄色");
            per.tell();
            per.getArea();
            per.getLength();
        }
    }

2.代码截图

3.运行结果

 

4. 总结

根据书上,依葫芦画瓢。重要一点的是设置长宽颜色的部分,还有最后调用一个有用的参数结构,输出信息。

 

(2)银行

1.源代码

import java.util.Scanner;
class yunxing {
private String date = "2019.9.19", name, account = "752160536";
private double balance = 0, password = 123456;

public yunxing(String user, String date, String name, double balance, double password) {
this.setAccount(account);
this.setDate(date);
this.setName(name);
this.setBalance(balance);
this.setPassword(password);
}

public String getAccount() {
return account;
}

public void setAccount(String account) {
this.account = account;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public double getPassword() {
return password;
}

public void setPassword(double password) {
this.password = password;
}

public void yewu() {
System.out.println("1.存款 2.取款 3.变更密码 4.查询标识符 5.查询姓名 6.查询开户日期 7.查询当前余额 0.返回");
Scanner in = new Scanner(System.in);
int b = in.nextInt();
switch (b) {
case 1:
System.out.println("存款金额");
Scanner n = new Scanner(System.in);
double a = n.nextInt();
if (a > 0) {
Scanner userBalance = new Scanner(System.in);
double balance = userBalance.nextInt();
balance = this.balance - balance;
setBalance(balance);
System.out.println("存款成功!");
} else {
System.out.println("输入数字无效,请重新输入!");
yewu();
}
case 2:
System.out.println("取款金额:");
Scanner userBalance = new Scanner(System.in);
double balance = userBalance.nextInt();
if (balance > this.balance || balance < 0) {
System.out.println("输入错误!请重新输入!");
yewu();
} else {
balance = this.balance + balance;
setBalance(balance);
System.out.println("取款成功!");

}
case 3:
System.out.println("请输入新密码:");
Scanner username = new Scanner(System.in);
String name = username.nextLine();
System.out.println("修改成功!");
setName(name);
case 4:
System.out.println("您的账户标识为:" + account);
case 5:
System.out.println("您的姓名为:" + this.name);
case 6:
System.out.println("您的开户日期为:" + date);
case 7:
System.out.println("您的余额为:" + this.balance);
case 0:
break;
}
}

public void denglu() {
System.out.println("请输入您的账号:");
Scanner useraccount = new Scanner(System.in);
String account = useraccount.next();
if (account.equals(this.account)) {
System.out.println("请输入您的密码:");
Scanner password = new Scanner(System.in);
double pass = password.nextInt();
if (pass == this.password) {
System.out.println("登陆成功!");
} else {
System.out.println("密码有误,请重新输入");
denglu();
}
} else {
System.out.println("账号有误,请重新输入");
denglu();
}
}

public void zhuce() {
System.out.println("输入你想注册的用户名称");
Scanner username = new Scanner(System.in);
String name = username.nextLine();
setName(name);
}
}
class Test{
public static void main(String[] args) {
System.out.println("欢迎来到银行系统");
System.out.println("1:业务办理 2:注册账号");
Scanner input = new Scanner(System.in);
int c = input.nextInt();
yunxing user = null;
if (c == 1) {
user.yewu();
} else if (c == 2) {
user.zhuce();
} else {
System.out.println("输入错误,请重新输入");
}
}
}


2.代码截图

 

 

 

3.运行结果

 

 

 

4. 总结

试了很多次都是错误,然后换了一种方法。第二题比第一题目复杂很多,要求也是很多。有很多情况要考虑才可以使系统更加严谨和完善。能想到的多了很多,然后速度和思维能力和方式都有提高。

 

写代码还有很多缺点,希望各位不啬赐教

 

posted @ 2019-09-15 14:49  九亿  阅读(309)  评论(0编辑  收藏  举报
我抽完了也不想写————(~ ̄▽ ̄~) 装傻———— 博客首页