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

实验二 Java简单类与对象

实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验内容
(一)写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

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

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

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

实验代码:

package First;

public class Rectangle {

	   	 private double width=8,height=20;
	    private String color="blue";              
		    public double getHeight() {
		        return height;                
		    }
		    public void setHeight(double height) {
		        this.height = height;        
		    }				 		  
		    public String getColor() {
		        return color;
		    }
		    public void setColor(String color) {
		        this.color = color;
		    }
		    public void getLength() {
		        double length=0;
		        length=(this.height+this.width)*2;
		        System.out.println("周长为"+length);
		    }
		    public double getWidth() {
		        return width;
		    }
		    public void setWidth(double width) {
		        this.width = width;
		    }
		    public void getArea() {
		        double area=0;
		        area=this.height*this.width;
		        System.out.println("面积为"+area);
		    }
		  
		    public static void main(String args[]) {
		        Rectangle per=new Rectangle();
		        per.setWidth(7);
		        per.setHeight(4);
		        per.setColor("蓝色");
		        per.getArea();
		        per.getLength();
		        System.out.println("长:"+per.getWidth()+"
高:"+per.getHeight()+"
颜色:"+per.getColor());


	}

}

实验结果

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

实验代码;

package First;
import java.util.Scanner;
public class Bank{
		private String name,date="2019.9.20";                      
	    private int mima=123456,money=0;   
	    private String ccount="jayking";
	        public String getName() {
	        return name;
	}
	        public void setName(String name) {
	            this.name = name;
	        }
	        public String getDate() {
	            return date;
	        }
	        public void setDate(String date) {
	            this.date = date;
	        }
	        public int getMima() {
	            return mima;
	        }
	        public void setMima(int mima) {
	            this.mima = mima;
	        }
	        public int getMoney() {
	            return money;
	        }
	        public void setMoney(int money) {
	            this.money = money;
	        }
	        public String getCcount() {
	            return ccount;
	        }
	        public void setCcount(String ccount) {
	            this.ccount = ccount;
	        }
	        public void username(){                   
	            System.out.print("请输入用户名字:");
	            Scanner username = new Scanner(System.in);
	            String name = username.nextLine();
	            setName(name);
	        }
	        public void ChangePWD(){                    
	            System.out.print("请输入需要修改的密码:");
	            Scanner usermima = new Scanner(System.in);
	            int mima = usermima.nextInt();
	            setMima(mima);
	        }
	        public void all(){                                    
	            System.out.print("用户名:"+name+"
开户日期:"+date+"
账号:"+ccount+"
密码:"+mima+"
余额:"+money+"
");
	        }
	        public void Depositsandwithdrawals(){
	            System.out.print("存款:1
"+"取款:0
");
	            System.out.print("请选择:");
	            Scanner a = new Scanner(System.in);
	            int b = a.nextInt();
	            if(b==0) {
	            System.out.print("取款数:");
	            Scanner usermoney = new Scanner(System.in);
	            int money = usermoney.nextInt();
	            money=this.money-money;	     
	            setMoney(money);
	            }
	            else {
	            System.out.print("存款数:");
	            Scanner usermoney = new Scanner(System.in);
	            int money = usermoney.nextInt();
	            money=this.money+money;	           
	            setMoney(money);
	            }
	        }
	        public static void main(String[] args) {
	            System.out.print(" Welcome
");
	            System.out.print("进入系统请按:1
");
	            System.out.print("请选择输入的序号:");
	            Scanner a = new Scanner(System.in);
	            int A = a.nextInt();
	            int B=0;
	            Bank user = new Bank();
	            for(int k=1;k>0;) {
	                if(A==1||B==1) {
	                    System.out.print("开户:1
"+"更改密码:2
"+"查询信息:3
"+"存款取款:4
");
	                    System.out.print("请输入你需要办理的业务序号:");
	                    Scanner q = new Scanner(System.in);
	                    int Q = q.nextInt();
	                    if(Q==1) {
	                        user.username();
	                    }else if(Q==2){
	                        user.ChangePWD();
	                    }else if(Q==3){
	                        user.all();
	                    }else if(Q==4){
	                        user.Depositsandwithdrawals();
	                    }
	                    System.out.print("返回菜单:1
"+"退出服务:9
");
	                    System.out.print("请输入序号:");
	                    Scanner b = new Scanner(System.in);
	                    B = b.nextInt();
	                    A=0;
	                }else{
	                    break;
	            }
	          }
	            System.out.print("已退出,感谢您的使用,欢迎下次光临!");
	        }   
	    }

实验结果

 Welcome
进入系统请按:1
请选择输入的序号:1
开户:1
更改密码:2
查询信息:3
存款取款:4
请输入你需要办理的业务序号:1
请输入用户名字:邹健
返回菜单:1
退出服务:9
请输入序号:1
开户:1
更改密码:2
查询信息:3
存款取款:4
请输入你需要办理的业务序号:23
返回菜单:1
退出服务:9
请输入序号:1
开户:1
更改密码:2
查询信息:3
存款取款:4
请输入你需要办理的业务序号:2
请输入需要修改的密码:199903
返回菜单:1
退出服务:9
请输入序号:1
开户:1
更改密码:2
查询信息:3
存款取款:4
请输入你需要办理的业务序号:4
存款:1
取款:0
请选择:1
存款数:10000
返回菜单:1
退出服务:9
请输入序号:1
开户:1
更改密码:2
查询信息:3
存款取款:4
请输入你需要办理的业务序号:4
存款:1
取款:0
请选择:0
取款数:5555
返回菜单:1
退出服务:9
请输入序号:1
开户:1
更改密码:2
查询信息:3
存款取款:4
请输入你需要办理的业务序号:3
用户名:邹健
开户日期:2019.9.20
账号:jayking
密码:199903
余额:4445
返回菜单:1
退出服务:9
请输入序号:9
已退出,感谢您的使用,欢迎下次光临!

这次实验让我掌握了类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;第一题相对于第二题来说还是简单一些的,因为老师上课讲过类似的题目,书上也有这种构造函数的题目,慢慢来还是挺容易写出来的,第二题就感觉有点麻烦了,弄了半天才写出来,然后写出第二题让自己有了一种设置一个小的atm机的成就感,这是以前学c的时候没有体会到的,感觉java非常贴近于生活,而写c的作业只用于解决那些理想性的问题,总体来说,这周不错,以后还得继续加油。

原文地址:https://www.cnblogs.com/ZJ999999/p/11558515.html