第四周课程总结&试验报告
实验二 Java简单类与对象
- 实验目的
- 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
- 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
- 理解static修饰付对类、类成员变量及类方法的影响。
- 实验内容:
- 写一个名为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);
}
}运行结果:
- 银行的账户记录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的掌握还不够熟练,做题还缺乏足够的耐心和思考,还要多钻研
第四周课程总结&试验报告的更多相关文章
- 第四周课程总结&试验报告2
试验报告2 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...
- 第四周课程总结&试验报告(二)
实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...
- 第五周课程总结&试验报告(三)
第五周课程总结&试验报告(三) 实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档: 实验内容 ###1.已知字符串:"this is a ...
- 第四周课程总结&实验报告(二)
Java实验报告(二) 实验二 Java简单类与对象 一. 实验目的 (1) 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: (2) 理解类和对象的区别,掌握构造 ...
- 第四周课程总结&实验报告二
第四周课程总结 第四周课程总结 本周重点为学习String;首先String用以创建字符串,且通过有一次课堂练习加强理解到:String 类是不可改变的,一旦创建了 String 对象,那它的值就无法 ...
- 第四周课程总结与第二次实验报告(Java简单类与对象)
1.写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类具有: ...
- 实验报告2&&第四周课程总结
实验报告: 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...
- 第五周课程总结&试验报告三
第五周课程总结 一.第五周课程总结 1.this关键字 this可用于任何实例方法内指向当前对象,也可指向对其调用当前方法的对象,或者在需要当前类型对象引用时使用.当一个类的属性(成员变量)名与访问该 ...
- 第十四周课程总结&记事本功能的简单实现。
(1)课程总结: 这周简单学习了下JDBC的内容: JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据. 执行流程: (1)连接数据源,如:数据库. (2)为数据库传递查 ...
随机推荐
- 网络流最大流dinic
hdu 6214 #include <bits/stdc++.h> #include<cstdio> #include<cstring> #include<q ...
- java集合类图详解
- docker常用命令及操作
1).镜像操作 操作 命令 说明 检索 docker search 关 键 字 eg:docker search redis 我们经常去docker hub上检索镜像的详细信息,如镜像的TAG. 拉取 ...
- oracle数据库架构
3.1 Client/Server Oracle 采取的是 Client/Server 架构. oracle 服务端分为两部分: Instance 实例 Database 数据库 实例, 又称为数据库 ...
- Extjs中如何在一行textfield后面增加文字提示
添加监听事件: listeners: { render: function(obj) { var font=document.cre ...
- hbase完全分布式搭建
1.解压缩hbase的软件包,使用命令: tar -zxvf hbase-1.3.0-bin.tar.gz 2.进入hbase的配置目录,在hbase-env.sh文件里面加入java环境变量.即: ...
- handy源码阅读(五):PollerBase类
使用poll内核函数等待事件发生: struct PollerBase: private noncopyable { int64_t id_; int lastActive_; PollerBase( ...
- HTML5基础内容(二)
HTML(HyperText Markup Language)超文本标记语言 一.HTML注释 元素就是标签,标签就是元素. 注释中的内容不会在页面中显示,但是可以在源码中看到. 可以通过编写注释来对 ...
- PHP入门培训教程 php中的时间处理
php中的时间处理 PHP入门培训教程 兄弟连PHP培训 小编整理的 php中的时间处理: <? /** * 转换为UNIX时间戳 */ function gettime($d) { if(is ...
- 如何在MySQL中使用explain查询SQL的执行计划?
1.什么是MySQL执行计划 要对执行计划有个比较好的理解,需要先对MySQL的基础结构及查询基本原理有简单的了解. MySQL本身的功能架构分为三个部分,分别是 应用层.逻辑层.物理层,不只是MyS ...