加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 07:41:24 所属栏目:Java 来源:网络整理
导读:实验二 Java简单类与对象 .实验目的 .掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值; .理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性; .理解static修饰付对类、类成员变量及类方法

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

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

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

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

实验源码:
package test;

public class Rectangle {
private double width,height;
private String color;
public Rectangle(String color,double width,double height){
this.setColor(color);
this.setWidth(width);
this.setHeight(height);
}
public String getColor(){
return color;
}
public void setColor(String n){
color=n;
}
public double getWidth(){
return width;
}
public void setWidth(double a){
if(a>0){
width=a;
}
}
public double getHeight(){
return height;
}
public void setHeight(double b){
if(b>0){
height=b;
}
}
public double getArea(){
return width*height;
}
public double getLength(){
return width+width+height+height;
}
public static void main(String[] args) {
Rectangle rec=new Rectangle("红色",28,12);
System.out.println("颜色:"+rec.getColor());
System.out.println("宽:"+rec.getWidth());
System.out.println("高:"+rec.getHeight());
System.out.println("面积:"+rec.getArea());
System.out.println("周长:"+rec.getLength());
}

}
实现结果:

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

实现代码:
package test;
import java.util.Date;
import java.util.Scanner;

class Account{
private String id,name,password;
private Date date;
private double balance;
public Account() {
}
public Account(String id,String name,double balance) {
this.setId(id);
this.setName(name);
this.date=new Date();
this.password="123456";
this.setBalance(balance);
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Date getDate() {
return date;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public void setId(String i) {
id=i;
}
public void setName(String n) {
name=n;
}
public void setDate(Date d) {
date=d;
}
public void setPassword(String p) {
password=p;
}
public void setBalance(double b) {
balance=b;
}
public void cunqian(double money) {
this.balance=this.balance+money;
}
public void quqian(double money) {
this.balance=this.balance-money;
}
public Date date() {
return date;
}
public void changepassword() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入新密码:");
String password=sc.nextLine();
}
public static void main(String[] args) {
Account a=new Account("1234567890","张广",1000);
a.cunqian(20000);
a.quqian(5000);
a.changepassword();
a.date();
System.out.println("账户标识:"+a.getId());
System.out.println("姓名:"+a.getName());
System.out.println("开户日期:"+a.date());
System.out.println("当前余额:"+a.getBalance());
}
}
实现结果:

学习总结: 这周学习了String类 熟悉了String类的作用以及常用操作方法 例如String类字符串的内容不可改变的特性。String类有直接为String类赋值和直接调用String类中的构造方法两种实例化方法。我们能够通过“==”和“equals()”方法对String的内容惊醒比较,利用String类的操作方法进行一系列的操作。我还了解了String‘buffer类、对象数组、包的概念和使用方法。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读