Java实验报告二
第一题
写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
实验代码
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) {
this.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 a=null;
a = new Rectangle();
a.width=2.0;
a.height=3.0;
System.out.println(+a.getArea());
System.out.println(+a.getGetlength());
}
}
运行结果
第二题
不会
第四周学习总结
1、String类
(1)、String首字母大写。
(2)、一个字符串就是一个String类的匿名对象。
(3)、使用“equals()”来比较字符串的内容,“==”是用来进行地址值的比较。
(4)、字符串的内容一旦声明则不可改变。
(5)、s=s.replace(a;b)把a的值替换成b。substring( ; )截取字符串。
2、包
(1)、管理文件,避免同名文件。
(2)、包的定义:package 包名称。子包名称;
学习总结
好好学习,天天向上。