实验二 Java简单类与对象

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

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

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

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

  • public 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 double getWidth()
    {
    return width;
    }
    public double getHeight()
    {
    return height;
    }
    public String getColor()
    {
    return color;
    } public void setWidth(double width)
    {
    this.width = width;
    }
    public void setHeight(double height)
    {
    this.height = height;
    }
    public void setColor(String color)
    {
    this.color = color;
    } public double Area()
    {
    return width*height;
    }
    public double Perimeter()
    {
    return 2*(width+height);
    }
    }

    运行结果:

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

    实验内容:

  • package yinghang;
    import java.util.Scanner; public class Account
    {
    public String id;
    public int password;
    public String name;
    public int money;
    public String time="2012/08/21 星期天"; public Account(String id, int password, String name, int money)
    {
    this.id = id;
    this.password = password;
    this.name = name;
    this.money = money;
    this.time = time;
    } public void show()
    {
    System.out.println("账户标识:" + id);
    System.out.println("姓名: " + name);
    System.out.println("开户日期:"+time);
    System.out.println("余额: " + money);
    } public void takeMoney()
    {
    while(true)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("您好,请输入密码进行验证!");
    int pass = sc.nextInt();
    if(pass == password)
    {
    System.out.println("请输入需要取款的金额:");
    int adds = sc.nextInt();
    if(adds <= money)
    {
    money= money-adds;
    System.out.println("本次取款为: " + adds);
    System.out.println("账户余额为:" + money);
    }
    else
    System.out.println("操作失败,您当前余额不足" );
    break;
    } else
    System.out.println("您输入的密码有误,请重新输入!");
    }
    } public void saveMoney(int moneys)
    {
    money = money+moneys;
    System.out.println("本次存款为: " + moneys);
    System.out.println("账户余额为:" + money);
    } public void key()
    {
    System.out.println("设置新密码:"); Scanner sxc=new Scanner(System.in);
    int password1=sxc.nextInt();
    System.out.println("请再次输入:");
    int password2=sxc.nextInt();
    if(password1!=password2)
    {
    System.out.println("您两次输入密码不同!");
    while(true)
    {
    System.out.println("请再次输入:");
    int password3=sxc.nextInt();
    if(password1==password3)
    {
    System.out.println("操作成功");
    break;
    }
    }
    }
    else
    System.out.println("操作成功"); }
    }
    package yinghang;
    import java.util.Scanner; public class SSS
    { public static void main(String[] args)
    {
    Account x = new Account("sss393645216",123456,"鹏",666666666); Scanner sx = new Scanner(System.in);
    System.out.println("请输入需要执行的操作");
    System.out.println("***1、银行账户信息***");
    System.out.println("***2、取款操作*******");
    System.out.println("***3、存款操作*******");
    System.out.println("***4、修改密码*******");
    System.out.println("***5、退出系统*******"); while(true)
    {
    int s = sx.nextInt();
    int i=0;
    switch(s)
    {
    case 1:
    System.out.println("***银行账户信息***");
    x.show();
    break;
    case 2:
    System.out.println("***取款操作******");
    x.takeMoney();
    break;
    case 3:
    System.out.println("***存款操作******");
    x.saveMoney(1000);
    break;
    case 4:
    System.out.println("***修改密码******");
    x.key();
    break;
    case 5:
    System.out.println("感谢您的使用!");
    System.exit(0);
    i=1; break;
    default:i=1;break;
    }
    if(i==1)
    break;
    } }
    }
  • 运行结果:

实验总结:对于java的掌握还不够熟练,做题还缺乏足够的耐心和思考,还要多钻研

第四周课程总结&试验报告的更多相关文章

  1. 第四周课程总结&试验报告2

    试验报告2 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...

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

    实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...

  3. 第五周课程总结&试验报告(三)

    第五周课程总结&试验报告(三) 实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档: 实验内容 ###1.已知字符串:"this is a ...

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

    Java实验报告(二) 实验二 Java简单类与对象 一. 实验目的 (1) 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: (2) 理解类和对象的区别,掌握构造 ...

  5. 第四周课程总结&实验报告二

    第四周课程总结 第四周课程总结 本周重点为学习String;首先String用以创建字符串,且通过有一次课堂练习加强理解到:String 类是不可改变的,一旦创建了 String 对象,那它的值就无法 ...

  6. 第四周课程总结与第二次实验报告(Java简单类与对象)

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

  7. 实验报告2&&第四周课程总结

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

  8. 第五周课程总结&试验报告三

    第五周课程总结 一.第五周课程总结 1.this关键字 this可用于任何实例方法内指向当前对象,也可指向对其调用当前方法的对象,或者在需要当前类型对象引用时使用.当一个类的属性(成员变量)名与访问该 ...

  9. 第十四周课程总结&记事本功能的简单实现。

    (1)课程总结: 这周简单学习了下JDBC的内容: JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据. 执行流程: (1)连接数据源,如:数据库. (2)为数据库传递查 ...

随机推荐

  1. Codeforces Round #427 (Div. 2) - D

    题目链接:http://codeforces.com/contest/835/problem/D 题意:给定一个字符串,定义kth回文是左半部分等于右半部分,并且左半部分和右半部分都是(k-1)th回 ...

  2. 生成对抗网络 Generative Adversarial Networks

    转自:https://zhuanlan.zhihu.com/p/26499443 生成对抗网络GAN是由蒙特利尔大学Ian Goodfellow教授和他的学生在2014年提出的机器学习架构. 要全面理 ...

  3. 四、ARM 异常处理

    4.1 模式与异常 当正常程序流程被暂时停止发生异常,例如响应一个来自外设的中断.在处理异常前,必须保护当前的处理器状态,以便在完成处理程序后能恢复到原来的程序 . 异常的类型: Reset unde ...

  4. du df的用法

    1,两者区别 du,disk usage,是通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一些当前存在 的,没有被删除的.他计算的大小就是当前他认为存在的所有文件大小的累加和. df, ...

  5. bzoj4883 [Lydsy1705月赛]棋盘上的守卫 最小生成基环树森林

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4883 题解 每一行和每一列都必须要被覆盖. 考虑对于每一行和每一列都建立一个点,一行和一列之间 ...

  6. CF1103C Johnny Solving (Codeforces Round #534 (Div. 1)) 思维+构造

    题目传送门 https://codeforces.com/contest/1103/problem/C 题解 这个题还算一个有难度的不错的题目吧. 题目给出了两种回答方式: 找出一条长度 \(\geq ...

  7. git初步研究2

    $git init Git 使用 git init 命令来初始化一个 Git 仓库,Git 的很多命令都需要在 Git 的仓库中运行,所以 git init 是使用 Git 的第一个命令. 在执行完成 ...

  8. 【JavaScript】对象 obj.name 语法与 obj[name]语法

    obj.name ==> obj["name"]  底层的自动转化,所以直接写 obj["name"] 效率会高一些 var obj = { name: ...

  9. SpringBoot的启动简述

    一.注解和启动类SpringBootApplication 它是一个复式注解. @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME ...

  10. HTML中的表单<form>标签

    一.HTML表单 HTML 表单用于搜集不同类型的用户输入. HTML 表单包含表单元素,表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 关于表单的更多内容可以参考htt ...