第四周学习总结暨第二周实验报告

1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值

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

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

一:实验代码

class Rectangle  {
			private double height;
			private double width;
			private String color;
			public Rectangle(){
			
			}
			public Rectangle(double width,double height,String color){
			this.setColor(color);
			this.setHeight(height);
			this.setWidth(width);
			}
			public void setHeight(double height) {
				this.height = height;
				}
			public void setWidth(double width) {
				this.width = width;
				}
			public void setColor(String color) {
				this.color = color;
				}
			public double getHeight() {
			return height;
			}
			
			public double getWidth() {
			return width;
			}
			
			public String getColor() {
			return color;
			}
			
			public double getArea(){
                double area;
			area=this.height*this.width;
			    return area;
			}
			public double getLength(){
			         double length;
			         length=width+height+width+height;
			         return length;
			}
			  };
			
			public class Demo {
			public static void main(String args[]) {
				Rectangle rec=null;
			rec=new Rectangle(3.0f,4.0f, "红色");
		    System.out.println(rec.getArea());
			System.out.println(rec.getLength());
			System.out.println("长:"+rec.getHeight());
			System.out.println("宽:"+rec.getWidth());
			}
			}

二:运行截图

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

package test;

import java.util.Scanner;
class Account{
  private String id;
  private String name;
  private int time,password;
  private double money;
  public Account(){
}
  public Account(String id,String name,int time,int password,double money){
  this.setId(id);
  this.setName(name);
  this.setTime(time);
  this.setPassword(password);
  this.setMoney(money);
}
 public void setId(String s){
  id=s;
}
 public void setName(String n){
  name=n;
}
 public void setTime(int m){
  time=m;
}
public void setPassword(int e){
  password=e;
}
public void setMoney(double c){
  money=c;
}
public String getId(){
  return id;
}
public String getName(){
  return name;
}
public int getTime(){
  return time;
}
public int getPassword(){
  return password;
}
public double getMoney(){
  return money;
}
public void showup(){
 System.out.println("账户名:"+id);
 System.out.println("姓名:"+name);
 System.out.println("开户时间:"+time);
 System.out.println("账户密码:"+password);
 System.out.println("账户余额:"+money);
}
public void qukuan(){
 while(true){
 Scanner sc =new Scanner(System.in);
 System.out.println("请输入密码:");
 int pass = sc.nextInt();
 if(pass==password){
 System.out.println("取款金额:");
 int sum = sc.nextInt();
 if(sum<=money){
 money=money-sum;
System.out.println("余额:"+money);
}
    else{
  System.out.println("余额不足,请重新输入金额");
}
break;
}
else
{
System.out.println("密码错误,请再次输入");
}
}
}
public void cunqian(int balance){
 money=money+balance;
 System.out.println("存入金额:"+balance);
 System.out.println("余额:"+money);
}
public static void main(String agrs[]){
Account acc =new Account("chenxin0527","陈新",19990527,123456,0);
Scanner sc = 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:退出");
int g = sc.nextInt();
switch(g){
case 1:
    System.out.println("账户基本信息");
     acc.showup();
 
case 2:
   System.out.println("取款");
   acc.qukuan();
case 3:
   System.out.println("存款");
    acc.cunqian(0);
   
case 4:
case 5:
    System.exit(0);
 break;
}
}
}

二:实验截图


三:问题所在
在该题目中出现了很多细节问题比如1:在输入的时候只能输入一次就会停止运行程序。2:在存钱时我的那个方法我定义了一个balance之后调用该方法的括号中必须加入数值才不会报错,所以导致接下来不管是存钱还是取钱都只有括号中数值的金额,所以导致后面的步步都是错的。3:该代码我的密码服务还没有加入,我还不知道怎么加入,有待加强!

四、总结
1、本周说的东西虽然在课堂上有掌握,但在课后自己又是一头雾水,让我觉得左右都是对的;
2、String关键字、StringBuffer类,了解StringBuffer的方法;
3、包的概念及使用,利用package解决问题;
4、import语句的使用.

我写作业的时候就是这个样子的,我到底该选谁了,就跟方法一样我用哪一个,我选了之后又该怎么用了???

原文地址:https://www.cnblogs.com/chenxinxin/p/11537672.html