第四周编程总结

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

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

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

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

一,实验代码

package TXT;

public class Rectangle {
private double height;
private double width;
private String color;
public Rectangle(double width,double height,String color){
this.setColor(color);
this.setHeight(height);
this.setWidth(width);
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void getArea(){
double area=0;
area=this.height*this.width;
System.out.println("矩形的面积:"+area);
}
public void getLength(){
double length;
length=width+height+width+height;
System.out.println("矩形的周长:"+length);
}
public String toString(){
String recStr="矩形的高度:"+this.getHeight()+"宽度:"+this.getWidth()+"颜色:"+this.getColor();
return recStr;
}
public static void main(String[] args) {
Rectangle rec=new Rectangle(5,10,"黑色");
rec.getArea();
rec.getLength();
System.out.println(rec.toString());
}
}

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

一,实验代码:

package TXT2;

import java.text.DecimalFormat; //格式化数字
import java.util.Scanner; //调用控制台输入信息 class Account {
private String id,name;
private int time,key;
private double balance;
public Account(String id,String name,int time,int key,int balance){
this.setId(id);
this.setName(name);
this.setTime(time);
this.setKey(key);
this.setBalance(balance);
}
public /static/ void deposit(){ //此处static
System.out.println("1.存款 2.取款");
System.out.print("再次输入你要进行的操作的序号:");
DecimalFormat df=new DecimalFormat("0.00");
Scanner sc=new Scanner(System.in); //接受控制台输入,同下所有Scanner sc=new Scanner(......)
int a1=sc.nextInt();
if(a1==1){
System.out.println("当前余额:"+df.format(+getBalance())+"元");
System.out.print("请输入要存入的钱数:");
double money1=sc.nextDouble();
setBalance(getBalance()+money1);
System.out.println("成功!当前余额:"+df.format(+getBalance())+"元");
}
else if(a1==2){
System.out.println("当前余额:"+df.format(+getBalance())+"元");
System.out.print("请输入要取出的钱数:");
double money2=sc.nextDouble();
setBalance(getBalance()-money2);
if(getBalance()<0){
System.out.println("余额不足!");
}
else{
System.out.println("成功!当前余额:"+df.format(+getBalance())+"元");
}
}
}public /*static*/ void change(){
System.out.print("请设置新密码:");
Scanner sc=new Scanner(System.in);
int key1=sc.nextInt();
System.out.print("请再次确认密码:");
int key2=sc.nextInt();
if(key1==key2){
setKey(key1);
System.out.println("设置成功!你的密码为:"+getKey());
}
else{
System.out.println("两次输入的密码不同!");
} }
public /*static*/void inquire(){
System.out.print("输入六位数密码查询信息:");
Scanner sc=new Scanner(System.in);
int data=sc.nextInt();
if(data==getKey()){
DecimalFormat df=new DecimalFormat("0.00");
System.out.println("标识:"+getId());
System.out.println("姓名:"+getName());
System.out.println("开户日期:"+getTime());
System.out.println("余额:"+df.format(+getBalance())+"元");
}
else{
System.out.println("密码错误");
}
}
public String getId(){
return id;
}
public void setId(String i){
id=i;
}
public String getName(){
return name;
}
public void setName(String n){
name=n;
}
public int getTime(){
return time;
}
public void setTime(int t){
time=t;
}
public int getKey(){
return key;
}
public void setKey(int k){
key=k;
}
public double getBalance(){
return balance;
}
public void setBalance(double b){
balance=b;
}
}
class classdemo3 {
public static void main(String[] args) {
boolean R=false;
Account ac=new Account("Hazelnut826","陈振国",20190919,101010,0);
while(!R)
{
System.out.println("1.存取款");
System.out.println("2.修改密码");
System.out.println("3.查询信息");
System.out.println("4.退出程序");
System.out.print("请输入你要进行的操作的序号:");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(a==1){
ac.deposit();
}
else if(a==2){
ac.change(); }
else if(a==3){
ac.inquire();
}
else if(a==4){
System.out.println(" 感谢使用!");
break;
}
} }
}

实验总结:

总是编译错误

最新文章

  1. OPENGLES 基础(一些链接和随笔)
  2. Spring4学习笔记-AOP
  3. POI文件导出至EXCEL,并弹出下载框
  4. 2016 ICPC北京站现场赛总结(再度流水账)
  5. maven_创建quickstart模板时异常
  6. go语言中间的循环
  7. java中的JSON对象的使用
  8. 用Myeclipse 编写struts.xml时,自动提示
  9. listener笔记
  10. 设计模式六大原则-OCP
  11. Leetcode389
  12. Spring context:component-scan代替context:annotation-config
  13. 新版Azure CDN HTTPS加速服务正式上线
  14. C# Winform ListView实现单元格双击复制内容到剪贴板
  15. windows server 2012 FTP连接报530 User 用户名 cannot log in home directory inaccessible的解决方法
  16. toString() 和 强制类型转换 (String)
  17. Bitcoin Core钱包客户端的区块数据搬家指南
  18. Javascript var 和 let 的区别
  19. C# 文件过滤器filter
  20. 【Coursera】Security Introduction -Eighth Week(2)

热门文章

  1. Win10遇到蓝屏错误CRITICAL_STRUCTURE_CORRUPTION如何解决
  2. oracle 主键自增并获取自增id
  3. bzoj4998 星球联盟 LCT + 并查集
  4. 【BZOJ2870】最长道路
  5. python 布尔判断并做需要的返回值
  6. React Native 之导航栏
  7. java 如何实现大文件上传下载(传输)各种格式
  8. vue2.0 之 douban (二)创建自定义组件tabbar
  9. Jeecg心得篇--这个世界不缺程序员,而是缺少匠人和架构师
  10. ps 等程序的选项的三种风格