第四周课程总结&第二次实验报告

实验二 Java简单类与对象
实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。

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

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

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

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

package Rectangle;

class Rect {

    public static void main(String[] args)
    {
         
         Rectangle re = new Rectangle();
         re.get();
         double a =re.getArea();
         System.out.println(a);
         double b =re.getLength();
         System.out.println(b);
     }
 }

 class Rectangle {
     double width;
     double heigh;
     String color;

     Rectangle() {
         this.width=10;
         this.heigh=30;
         this.color="黑色";
     }

     public void get(){
         System.out.println("width "+this.width);
         System.out.println("higher "+this.heigh);
         System.out.println("color "+this.color);
     }

     public void setwidth(double a) {
         this.width=a;
     }

     public void setheigh(double b) {
         this.heigh=b;
     }

     public void setcolor(String color) {
         this.color=color;
     }

     public double getArea() {
         return this.width*this.heigh;
     }

     public double getLength(){
         return this.width*2+this.heigh*2;
     }
 }

1581766-20190920204757891-179100658.png

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

class yinghang {

    public static void main(String[] args)
    {
    
     Account re = new Account("bingo","ldh",2019,9,20,"123456",2000);
     re.setAll();
     re.changebalance(100);
     re.setAll();
     re.changepsd("123456");
     re.setAll();
                       }
                          }

     class Account 
     
       {
                 String ID;           
                 String psd;                
                 String name;          
                 double balance;         
                 int month,day,year;
                 
         Account()
                 {
                     this.ID="binggo";
                     this.name="null";
                     this.year=0;
                     this.month=0;
                     this.day=0;
                     this.psd="null";
                     this.balance=0;
                                          }

             Account(String ID, String name, int year, int month, int day, String psd, double balance)
             
                {
      
                 this.ID       =   ID;
                 this.name     =   name;
                 this.year     =   year;
                 this.month    =   month;
                 this.day      =   day;
                 this.psd      =   psd;
                 this.balance  =   balance;
                 
                  }
public void changebalance(double single) 
                 
   {
                 this.balance += single;
                                           }

 public void changepsd(String single)
   {
     this.psd = single;
                                           }

 public void setAll()
   
 {
     System.out.println(this.ID);
     System.out.println(this.name);
     System.out.println(this.year+"."+this.month+"."+this.day);
     System.out.println("balance:"+this.balance);
                                            }
 
}

```
1581766-20190920210002723-1584681886.png
学习总结:
(1)学习了string类常用操作方法,运用具体例子理解String类中常用方法的基本使用。
运用This is a test of Java做实例,演示了is的查找,其作为单词的查找,test的截取要注意下标,截取正确长度等等。
(2)字符串的两种比较方法“==”“equal()”方法
(3)s.concat();s.respace();的赋值操作等
(4)学习了包的概念和import语句,对象数组,系统常见包的名称及作用。
实验中的问题:还是不太会使用ecplise,出现了(wrong name: Rectangle/rectangle)的错误,还有就是一些操作问题。

1581766-20190920213839767-1872612088.png

转载于:https://www.cnblogs.com/1211li/p/11559961.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值