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


    实验二 Java简单类与对象

    实验目的

    • 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值
    • 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性
    • 理解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.实验源码

    package test;
    
    import java.util.Scanner;
    
    class Rectangle{
        private double width;
        private double height;
        private String color;
    
        public Rectangle(double width,double height,String color){
    	    this.setHeight(height);
    	    this.setWidth(width);
    	    this.setColor(color);
        }
    
        public void getArea() {
    	    double area=0;
    	    area=this.width*this.height;
    	    System.out.println("面积="+area);
        }
    
        public void getLength() {
    	    double length=0;
    	    length=(this.width+this.height)*2;
    	    System.out.println("周长="+length);
        }
    
        public void tell() {
    	    System.out.println("宽=" +getWidth()+"  "+"长="+getHeight()+"  "+"颜色:"+getColor());
        }
    
        public double getWidth() {
    	    return width;
        }
        public void setWidth(double width) {
    	    this.width = width;
        }
        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 class Report2 {
    
        public static void main(String[] args) {
    	    double a=sc.nextDouble();
    	    double b=sc.nextDouble();
    	    String s=sc.next();
    	    Rectangle p1=new Rectangle(a,b,s);
    	    p1.tell();
    	    p1.getArea();
    	    p1.getLength();
        }
    }
    

    实验结果

    2.实验源码

    package test;
    import java.util.Date;
    import java.util.Scanner;
    
    class Account{
        private String id,name,dataopend,password;
        private double balance;
        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 getDataopend() {
    	    return dataopend;
        }
        public void setDataopend(String dataopend) {
    	    this.dataopend = dataopend;
        }
        public String getPassword() {
    	    return password;
        }
        public void setPassword(String password) {
    	    this.password = password;
        }
        public double getBalance() {
    	    return balance;
        }
        public void setBalance(double balance) {
    	    this.balance = balance;
        }
    
        public void  开户(){
    		    System.out.println("请输入您的姓名:");
    		    Scanner s=new Scanner(System.in);
    		    String name=s.nextLine();
    		    setName(name);
    		    System.out.println("开户成功");
    		    setId("6527 1226 5436 1232 123");
    		    System.out.println("您的账号为:"+getId());
    		    Date date=new Date(); 
    		    setDataopend(date.toString());
    		    System.out.println("您的开户日期为:"+getDataopend());
    		    setPassword("123456");
    		    System.out.println("您的初始密码为:"+getPassword());
    		    setBalance(0.0);
    		    System.out.println("您当前的余额为:"+getBalance());
    	    }
        public void 存取款(int n) {
    		    Scanner s=new Scanner(System.in);
    		    System.out.println("请选择服务项目:1.存款           2取款.");
    		    int m=s.nextInt();
    		    switch(m) {
    		    case 1:System.out.println("请输入存款金额:");//存款
    		    setBalance(s.nextDouble()+getBalance());
    		    System.out.println("交易成功!当前余额为:"+getBalance());break;
    		    case 2:System.out.println("请输入取款金额:");//取款
    		    double x=s.nextDouble();
    		    if(x>getBalance()) {
    			    System.out.println("当前余额不足");
    		    }
    		    else {
    			    setBalance(getBalance()-x);
    			    System.out.println("交易成功!当前余额为:"+getBalance());
    			    break;
    			}
    	    }	
        }
        public void  更改密码(){
    	Scanner ss=new Scanner(System.in);
    	System.out.println("请输入新密码");
    	String x=ss.next();
    	setPassword(x);
    	System.out.println("交易成功!");
    	
        }
        public void 查询() {
    	System.out.println("请选择您要查询的内容:1.账户   2.用户名   3.开户日期    4.当前余额");
    	Scanner ss=new Scanner(System.in);
    	int n=ss.nextInt();
    	switch(n) {
    		case 1:System.out.println("您的账户为:"+getId());break;
    		case 2:System.out.println("用户名为:"+getName());break;
    		case 3:System.out.println("开户日期为:"+getDataopend());break;
    		case 4:System.out.println("当前余额为:"+getBalance());break;
    	    }
        }
    }
    public class Report2_2 {
        public static void main(String[] args) {
        	System.out.println("WELCOME");
    	Account per=new Account();
    	Scanner ss=new Scanner(System.in);
    	per.开户();
    	System.out.println("请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出");
    	int n=ss.nextInt();
    	while(n>=0) {
    		if(n==0)
    		{
    			System.out.println("谢谢使用!");
    			break;
    		}
    		else
    		{
    				switch(n) {
    				case 1:per.存取款(n);break;
    				case 2:per.更改密码();break;
    				case 3:per.查询();break;
    			}
    		}
    		System.out.println("请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出");
    		n=ss.nextInt();
    	}	
        }
    }
    

    实验结果

    WELCOME
    请输入您的姓名:
    刘磊
    开户成功
    您的账号为:6527 1226 5436 1232 123
    您的开户日期为:Fri Sep 20 14:59:07 CST 2019
    您的初始密码为:123456
    您当前的余额为:0.0
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    1
    请选择服务项目:1.存款           2取款.
    2
    请输入取款金额:
    100
    当前余额不足
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    1
    请选择服务项目:1.存款           2取款.
    1
    请输入存款金额:
    1000
    交易成功!当前余额为:1000.0
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    1
    请选择服务项目:1.存款           2取款.
    2
    请输入取款金额:
    900
    交易成功!当前余额为:100.0
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    2
    请输入新密码
    234576
    交易成功!
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    3
    请选择您要查询的内容:1.账户   2.用户名   3.开户日期    4.当前余额
    1
    您的账户为:6527 1226 5436 1232 123
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    3
    请选择您要查询的内容:1.账户   2.用户名   3.开户日期    4.当前余额
    2
    用户名为:刘磊
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    3
    请选择您要查询的内容:1.账户   2.用户名   3.开户日期    4.当前余额
    3
    开户日期为:Fri Sep 20 14:59:07 CST 2019
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    3
    请选择您要查询的内容:1.账户   2.用户名   3.开户日期    4.当前余额
    4
    当前余额为:100.0   
    请选择服务项目:1.存款/取款   2.更改密码   3查询   0.退出
    0
    谢谢使用!
    

    实验过程

    第一题的实验过程不难,创建一个类,在类里面定义好矩形的属性,在类里构造输出结果的方法,在主类中传递参数,然后调用方法就行了。
    第二题有点难,先确定账户包含内容--标识、用户名、开户日期、密码、余额,然后确定要实现哪些操作--存取款、更改密码、查询,最后设计基本框架。
    在子类中定义标识、用户名、开户日期、密码、余额,创建存取款、更改密码、查询的方法,在主类中用switch()选择要调用的方法,然后完善功能。

    总结

    通过这周的实验,我体会到了用Java写代码的便捷性,同时对类和方法的调用有了进一步的了解。

    课程总结

    String类中的常用方法


    字符串的内容一旦声明则不可改变

    数组的定义

    数据类型 数组名[]=new 数据类型[个数]
    

    数据长度的取得

    数组名称.length    ->返回一个int型数据
    

    代码块

    普通代码块是指直接在方法或语句中定义的代码块

    public class CodeDemo01{
           public static void main(String[] args){
                    {
                        int x=30;
                        System.out,println("普通代码块-->x="+x);
                    }
                    int x=100;
                    System.out.println("代码块之外-->x="+x);
            }
    };
    

    构造代码块是直接写在类中的代码块

    class Demo{
            {
                System.out.println("构造块1");
            }
    }
    

    静态代码块是使用static关键字声明的代码块

    class Demo{
            static{
                System.out.println("构造块1");
            }
    }
    

    静态代码块先主方法执行,优先于构造块执行,静态代码块只执行一次。

  • 相关阅读:
    Day1_Python基础
    选择排序(java版)
    冒泡排序(java版)
    手写数据库连接池(动态代理)
    JDBC增删查改(使用配置文件)
    JDBC demo
    JSP入门&会话技术
    response实现验证码图片
    android 定制自己的日志工具
    服务的最佳实践——后台执行的定时任务
  • 原文地址:https://www.cnblogs.com/jk-liulei/p/11544725.html
Copyright © 2020-2023  润新知