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

实验二 Java简单类与对象

实验目的

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

实验内容

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

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

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

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

代码

 package hello.java;

 public class Rectangle {

  private double width;         
  private double height;
  private String color;
 
  public Rectangle(double width, double height, String color) {
     this.setWidth(width);
     this.setHeight(height);           
     this.setColor(color);
      }  
                                  
   public Rectangle() {
    
    }
  public void setWidth(double width) {
    t   his.width = width;
    }
  public void setHeight(double height) {
        this.height = height;
    }
   public void setColor(String color) {               
        this.color = color;
    }
  public double getWidth() {
     return width;
    }
  public double getHeight() {
    return height;
    }
  public String getColor() {                            
    return color;
    }
   public double getArea() {
    return this.width*this.height;               
   }
   public double getGetlength() {
    return (this.height+this.width)*2;        
 }
   public static void main(String[] args) {
     Rectangle juxing=null;
      juxing= new Rectangle();          
     juxing.width=2.0;                                          
     juxing.height=4.0;
    
     System.out.println("面积");
     System.out.println(juxing.getArea()); 
      System.out.println("周长");
     System.out.println(juxing.getGetlength());
    }
}

需要注意的问题:使用这个方法需注意矩形的声明

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

第四周课程总结

所学知识点

1.==是用来进行地址值比较的
2.equals 只比较内容

  if(strings[i].endsWith("ng"))
   cont++;。

3.如果 strings[i] 数据域中有以“ng”结尾则count++

4.s=new s.concat("E");才能被string类型调用

int n=0;
 StringTokenizer st =new StringTokenizer("I wonder should I go,or should I stay");
 while(st.hasMoreTokens()){
        n++;
    String ss=st.nextToken();
  }

5.该程序执行了十次循环,循环判断条件为(n<上面那句话的空格数?)

6.String类常用操作方法
大佬链接 https://www.cnblogs.com/cham88/p/6044623.html
https://www.w3school.com.cn/jsref/jsref_obj_string.asp

数组知识点

数据类型 数组名称=null;
数组名称=new 数据类型[长度]
数据类型[]数组名称=null
在java中想开辟新的堆内存则必须使用new关键字
数据类型 数组名[]=new 数据类型[个数]
int score[] =new int [10] //声明一个元素个数为10的整形数组score,同时一块内存空间供其使用
数组的赋值
数据类型 数组名[] ={初值1,2,……n}
二维数组声明格式
1.数据类型=new 数据类型 [行的个数][列的个数]
2.数据类型 数组名[][]=new 数据类型[行的个数][列的个数]

1.包的作用是分门别类管理,避免同名文件

2.包是为了迎合多人开发而开发出的一个工具

原文地址:https://www.cnblogs.com/1793979463hyx/p/11551024.html