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

    实验二 Java简单类与对象

    实验目的

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

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

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

    第四周总结:

    一::String类

    1:实例化String对象,String可以采取直接赋值的方式进行操作;

    2:==可以用来进行内容的比较,但是单纯写==不会只进行内容上的比较,还有地址的比较;

    3:equals()方法可以只进行内容上的比较;

    4:String还可以使用new关键字来实例化;

    5:String类常用操作方法 (p110);

    6:字符串可以使用toCharrArray()方法变成一个字符数组;

    7:String可以使用indexOf()方法返回指定的字符串位置。

    二:包的使用:

    1:package是在使用多个类或接口时,为了避免名称重复而采用的一种措施,直接在程序中加入package关键字即可。

    2:如果几个类存放不同包中,在使用类的时候必须通过import语句进行导入;

    3:系统常用包(p239);

    实验内容

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

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

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

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

    实验代码:

    class test {
    	private double width;
    	private double height;
    	private String color;
    
    	public test() {
    	}
    
    	public test(double width, double height, String color) {
    		this.setWidth(width);
    		this.setHeight(height);
    		this.setColor(color);
    	}
    
    	public void setWidth(double w) {
    		width = w;
    	}
    
    	public void setHeight(double h) {
    		height = h;
    	}
    
    	public void setColor(String c) {
    		color = c;
    	}
    
    	public double getWidth() {
    		return width;
    	}
    
    	public double getHeight() {
    		return height;
    	}
    
    	public String getColor() {
    		return color;
    	}
    
    	public double getArea() {
    		return width * height;
    	}
    
    	public double getLength() {
    		return 2 * (width + height);
    	}
    
    }
    
    public class Rectangle {
    	public static void main(String[] args) {
    		test t = null;
    		t = new test(15, 18, "blue");
    		System.out.println("周长为"+t.getLength());
    		System.out.println("面积为"+t.getArea());
    	}
    }
    
    

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

    实验代码:

    package yinhang;
    
    import java.util.Scanner;
    
    class bank {
    	private String id;
    	private String name;
    	private String createtime;
    	private int password;
    	private int money;
    
    	public bank(String id, String name, String createtime, int password, int money) {
    		this.setId(id);
    		this.setName(name);
    		this.setCreatetime(createtime);
    		this.setPassword(password);
    		this.setMoney(money);
    	}
    
    	public bank(String string, String string2, String string3, String string4, String string5) {
    		// TODO 自动生成的构造函数存根
    	}
    
    	public String getId() {
    		return id;
    	}
    
    	public void setId(String id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getCreatetime() {
    		return createtime;
    	}
    
    	public void setCreatetime(String createtime) {
    		this.createtime = createtime;
    	}
    
    	public int getPassword() {
    		return password;
    	}
    
    	public void setPassword(int password) {
    		this.password = password;
    	}
    
    	public int getMoney() {
    		return money;
    	}
    
    	public void setMoney(int money) {
    		this.money = money;
    	}
    
    	public void kaihu() {
    		System.out.println("您的账号标识符为:hhl296738954");
    		System.out.println("您的账户初始密码为123456");
    		System.out.println("请输入您的真实姓名");
    		Scanner sc = new Scanner(System.in);
    		char name = sc.next().charAt(0);
    		System.out.println("您的账户余额为0");
    	}
    
    	public void cunqu() {
    		System.out.println("请输入查询账号密码");
    		Scanner sc = new Scanner(System.in);
    		int num = sc.nextInt();
    		if (num == getPassword()) {
    			System.out.println("您的当前余额为" + getMoney());
    			int a = 0;
    			while (a != 6) {
    				System.out.println("1:存钱");
    				System.out.println("2:取钱");
    				System.out.println("3:查询开户时间");
    				System.out.println("4:查询账户标识");
    				System.out.println("5:查询账号姓名");
    				System.out.println("6:退出查询");
    				Scanner sc1 = new Scanner(System.in);
    				a = sc1.nextInt();
    
    				if (a == 1) {
    					System.out.println("请输入您所需存金额");
    					Scanner sc11 = new Scanner(System.in);
    					int q = sc11.nextInt();
    					setMoney(getMoney() + q);
    					System.out.println("您的当前余额为" + getMoney());
    				} else if (a == 2) {
    					System.out.println("请输入您所需取金额");
    					Scanner sc111 = new Scanner(System.in);
    					int w = sc111.nextInt();
    					if (w > getMoney()) {
    						System.out.println("您的当前余额不足");
    					} else {
    						setMoney(getMoney() - w);
    						System.out.println("您的当前余额为" + getMoney());
    					}
    				} else if (a == 3) {
    
    					System.out.println("您的开户日期为" + getCreatetime());
    				} else if (a == 4) {
    
    					System.out.println("您的账户标识为" + getId());
    				} else if (a == 5) {
    					System.out.println("您的账户姓名为" + getName());
    				}
    
    			}
    		} else {
    			System.out.println("您输入的密码有误");
    		}
    	}
    
    	public void change() {
    
    		System.out.println("请输入新密码");
    		Scanner sc2 = new Scanner(System.in);
    		int r = sc2.nextInt();
    		System.out.println("请再次输入新密码");
    		Scanner sc3 = new Scanner(System.in);
    		int t = sc3.nextInt();
    		if (t == r) {
    			System.out.println("更改成功");
    		} else {
    			System.out.println("俩次输入密码不相同,请重新输入");
    		}
    
    	}
    
    	public static void main(String[] args) {
    		bank per = new bank("hhl296738954", "何厚霖", "20181001", 123456, 1000);
    		int y = 0;
    		while (y != 4) {
    			System.out.println("1.开户");
    			System.out.println("2.查询与存取");
    			System.out.println("3.更改密码");
    			System.out.println("4.退出");
    			System.out.println("请输入您下一步操作的数字:");
    			Scanner sc5 = new Scanner(System.in);
    			y = sc5.nextInt();
    			if (y == 1) {
    				per.kaihu();
    			} else if (y == 2) {
    				per.cunqu();
    			} else if (y == 3) {
    				per.change();
    			}
    		}
    	}
    }
    

    运行截图:


    错误:当我最后实例化bank对象,想通过构造方法赋值的时候,我赋的值都无法输入,最开始的是bank per=new bank("hhl296738954","何厚霖","20181001","123456","1000");,后面查阅资料,发现int型不用加" ",应该更改为bank per=new bank("hhl296738954","何厚霖","20181001",123456,1000);.

  • 相关阅读:
    【万丈高楼平地起 第一季 链表是怎样链成的】
    【笔记——ASP.NET基础知识(一)】
    【万丈高楼平地起 第二季 队列和栈】
    【没有银弹No Silver Bullet】
    【简单示例:数据库表转XML】
    【软件工程小知识】
    【总结——SQL Server中的数据类型】
    【总结—.Net Framework集合类】
    【笔记——ASP.NET基础知识(二)】
    【总结——ASP.NET对象】
  • 原文地址:https://www.cnblogs.com/hhl296738954/p/11551470.html
Copyright © 2011-2022 走看看