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

课程总结:
这一周讲的都是一些很重要的知识,最主要的是Sring类,对于String可以采用直接赋值的方式进行操作;还有String对象的内容比较,其中讲了""与"equals"的应用及区别,""是用来进行地址值比较的,而"equals()"是进行内容的比较的,这是值得外面区分的一点,"equals()"是专门负责进行字符串内容的比较;字符串的内容一旦声明就不可改变,所以如果要解决的话要调用"StringBuff"类来完成;除此之外外面还学习了String类常用的操作方法其中有4个构造方法,很多个普通方法,这些方法主要是讲了字符串与字符数组之间的转换。

实验二 Java简单类与对象
实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验内容
一、写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

1.源代码

package 作业;
class Rectangle{
	private String color;
	private double width;
	private double height;
	public Rectangle(String color,double width,double height){
		this.setColor(color);
		this.setWidth(width);
		this.setHeigth(height);
	}
	public void talk(){
		System.out.println("矩形面积:"+getArea()+" 矩形周长:"+getLength());
		System.out.println("矩形颜色:"+getColor());
	}
	public double getWidth(){
		return width;
	}
	public void setWidth(double width){
		this.width=width;
	}
	public double getHeigth(){
		return height;
	}
	public void setHeigth(double height){
		this.height=height;
	}
	public String getColor(){
		return color;
	}
	public void setColor(String color){
		this.color=color;
	}
	public double getArea(){
		double getArea=getWidth()*getHeigth();
		return getArea;
	}
	public double getLength(){
		double getLength=(getWidth()+getHeigth())*2;
		return getLength;
	}
	
}
public class Ren {
	public static void main(String[]arge){
		Rectangle str;
		str=new Rectangle("蓝色",6,6);
	    str.talk();
		
	}

}

截图

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

1.源代码

package 作业;

import java.util.Scanner;

class bank {
	private int time;                     //取款时间
	private int password;                 //密码
	private String id;                    
	private String name;                  //姓名
	private double yue;                   //余额
	public bank(int time,int password,String id,String name,double yue) {
		this.setTime(time);
		this.setPassword(password);
		this.setId(id);
		this.setName(name);
		this.setYue(yue);                          
	}


	public bank(String string, String string2, String string3, String string4) {
		// TODO 自动生成的构造函数存根
	}


	private void setId(String id) {
		this.id=id;
	}
	
	public String getId() {
		return id;
	}


	public int getTime() {
		return time;
	}


	public void setTime(int time) {
		this.time = time;
	}


	public int getPassword() {
		return password;
	}


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


	public String getName() {
		return name;
	}


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


	public double getYue() {
		return yue;
	}


	public void setYue(double yue) {
		this.yue = yue;
	}


    public void deposit() {                    //存款 取款
    	System.out.println("1.存款 2.取款");
    	System.out.print("请输入您下一步操作的数字:");
    	Scanner sc=new Scanner(System.in);
    	int p1=sc.nextInt();
    	if(p1==1) {
    		System.out.println("您的余额为:"+getYue()+"元");
    		System.out.println("请输入您将要存入的钱数:");
    		double money1=sc.nextDouble();
    		setYue(getYue()+money1);          //存钱
    		System.out.println("操作成功,当前您的余额为:"+getYue()+"元");
    	}
    	else if(p1==2) {
    		System.out.println("您的余额为:"+getYue()+"元");
    		System.out.println("请输入您将要取出的钱数:");
    		double money2=sc.nextDouble();
    		setYue(getYue()-money2);          //取钱
    		
			if (getYue()<0) {
    			System.out.println("您的余额不足!!!");
    		}
			else {
				System.out.println("操作成功,您当前余额为:"+getYue()+"元");
			}
    	}
    }
    
    public void mimabiangeng(){                     //变更密码
    	System.out.println("设置新密码:");
    	Scanner sc=new Scanner(System.in);
    	int password1=sc.nextInt();
    	System.out.println("请确认密码是否有误:");
    	int password2=sc.nextInt();
    	if(password1!=password2) {
    		System.out.println("您两次输入密码不同!");
    	}
    	else {
    		setPassword(password1);
    		System.out.println("操作成功,您的新密码:"+getPassword());
    		
    	}
    	
    	
    		
    	}
    public void chaxunzhanghu() {                    //查询账户的标识、姓名、开户日期、当前余额等信息    
    	System.out.println("输入密码查询信息:");
    	Scanner sc=new Scanner(System.in);
    	int num=sc.nextInt();
    	if(num==getPassword()) {
    		 System.out.println("标识:"+getId());
    		 System.out.println("姓名:"+getName());
    		 System.out.println("余额:"+getYue()+"元");
    		 System.out.println("开户日期:"+getTime());
    	}
    	else {
    		 System.out.println("您的密码错误");
    	}
    }
    	
	public static void main(String[] args) {
		boolean P=true;
		bank sa=new bank(2019091,123456,"sunhao666666","孙浩",1000000);
			
		while(P) {
		System.out.println("1.取款存款");
		System.out.println("2.修改密码");
		System.out.println("3.查询信息");
		System.out.println("4.退出");
		System.out.println("请输入您下一步操作的数字:");
		Scanner sc=new Scanner(System.in);
        int b=sc.nextInt();
        if(b==1) {
        	sa.deposit();
        }
        else if(b==2) {
        	sa.mimabiangeng();
        }
        else if(b==3) {
        	sa.chaxunzhanghu();
        }
        else if(b==4) {
            System.out.println("欢迎您下一次使用");
        }
	}

}
}

截图

这一道题对我来说是个很大的挑战,从下午三点半写到晚上八点,其中题目一开始不知道该如何下手,经过认真审题,先分为取款存款,修改密码,查询信息三个大步,最后逐步分析,一步一步调试,每一步都不能错,最后还是写出来了,但总体来说这个题目不算太难,就是步骤太多,有点繁琐

原文地址:https://www.cnblogs.com/edg4396/p/11550852.html