实验报告二

Java实验报告

班级 学号 姓名
完成时间
评分等级
实验二 Java简单类与对象
一、 实验目的
(1) 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
(2) 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
(3) 理解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. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
    (1) 使用构造函数完成各属性的初始赋值
    (2) 使用get…()和set…()的形式完成属性的访问及修改
    (3) 提供计算面积的getArea()方法和计算周长的getLength()方法
    ①实验代码
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(3, 4, "红色");
rec.getArea();
rec.getLength();
System.out.println(rec.toString());
}
}

②实验结果


有很多小细节没处理好,还需要谨慎操作

第二题
①实验代码

package text3;

import java.util.Scanner;
import java.util.Date;

public class Account {
 
 private String id;
 private String name;
 private Date date;
 private int password;
 private int money;
 
 public Account(String id, String name, int money) {
  super();
  this.id = id;
  this.name = name;
  this.money = money;
  this.date = new Date();
  this.password = 123456;
 }



 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 void jin(int num) {
  this.money = this.money+num;
 }

 public void chu(int num) {
  this.money = this.money-num;
 }
 
 public Date getDate() {
  return date;
 }

 public void setDate(Date date) {
  this.date = date;
 }

 public int getMoney() {
  return money;
 }

 public void setMoney(int money) {
  this.money = money;
 }
 
 public int getPassword() {
  return password;
 }

 public void setPassword(int password) {
  this.password = password;
 }



 public void mima() {
  Scanner sc=new Scanner(System.in);
     int password=sc.nextInt();
     this.password = password;
     sc.close();
    
 }

}

②实验截图

有些错误不知道为什么,找朋友帮忙改的
有些函数不知道为什么要加。嘻嘻
今天上完课知道为什么导入import语句。

四、总结
1、本周讲的内容有更加深入了一些,越来越深奥了,一会说这个一会又说另外的了,有点云里雾里,感觉上课不预习有点跟不上,预习不知道看哪;
2、String关键字、StringBuffer类,熟悉一些StringBuffer的常用方法;
3、包的概念及使用,利用package解决问题;
4、import语句的使用.

原文地址:https://www.cnblogs.com/gaotian250yj912/p/11528807.html