任务1: 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有: (1) 使用构造函数完成各属性的初始赋值 (2) 使用getter和setter的形式完成属性的访问及修改 (3) 提供计算面积的getArea()方法 (4) 重写toString()方法,输出矩形信息 (5) 自己增加与Rectangle类相关的方法 任务2:声明复数类Complex,成员变量包括实部和虚部,类型均为double。要求该类具有 (1) 实现多个构造方法(如无参构造方法、1个参数构造方法,2个参数构造方法,字符串参数的构造方法)。 (2) 使用getter和setter完成属性的访问和修改 (3) 成员方法包括实现复数加法add、减法minus、乘法multiply,计算模长length (4) 重写equals()方法,只有当两个复数实部和虚部都相等时,才返回真 (5) 重写toString(),输出复数信息 (6) 在main方法中,使用实际数据测试成员方法
时间: 2024-02-19 22:59:28 浏览: 29
任务1:
```java
public class Rectangle {
private double width;
private double height;
private String color;
public Rectangle(double width, double height, String color) {
this.width = width;
this.height = height;
this.color = color;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getArea() {
return width * height;
}
@Override
public String toString() {
return "Rectangle{" +
"width=" + width +
", height=" + height +
", color='" + color + '\'' +
'}';
}
public void printInfo() {
System.out.println("This is a " + color + " rectangle with width " + width + " and height " + height + ".");
}
}
```
任务2:
```java
public class Complex {
private double realPart;
private double imaginaryPart;
public Complex() {
this.realPart = 0;
this.imaginaryPart = 0;
}
public Complex(double realPart) {
this.realPart = realPart;
this.imaginaryPart = 0;
}
public Complex(double realPart, double imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
public Complex(String complexString) {
String[] parts = complexString.split("\\+");
this.realPart = Double.parseDouble(parts[0]);
this.imaginaryPart = Double.parseDouble(parts[1].substring(0, parts[1].length() - 1));
}
public double getRealPart() {
return realPart;
}
public void setRealPart(double realPart) {
this.realPart = realPart;
}
public double getImaginaryPart() {
return imaginaryPart;
}
public void setImaginaryPart(double imaginaryPart) {
this.imaginaryPart = imaginaryPart;
}
public Complex add(Complex other) {
double real = this.realPart + other.realPart;
double imaginary = this.imaginaryPart + other.imaginaryPart;
return new Complex(real, imaginary);
}
public Complex minus(Complex other) {
double real = this.realPart - other.realPart;
double imaginary = this.imaginaryPart - other.imaginaryPart;
return new Complex(real, imaginary);
}
public Complex multiply(Complex other) {
double real = this.realPart * other.realPart - this.imaginaryPart * other.imaginaryPart;
double imaginary = this.realPart * other.imaginaryPart + this.imaginaryPart * other.realPart;
return new Complex(real, imaginary);
}
public double length() {
return Math.sqrt(this.realPart * this.realPart + this.imaginaryPart * this.imaginaryPart);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Complex) {
Complex other = (Complex) obj;
return this.realPart == other.realPart && this.imaginaryPart == other.imaginaryPart;
}
return false;
}
@Override
public String toString() {
return realPart + "+" + imaginaryPart + "i";
}
public static void main(String[] args) {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(2, 3);
Complex c3 = new Complex("4+5i");
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
System.out.println("c3 = " + c3);
System.out.println("c1 + c2 = " + c1.add(c2));
System.out.println("c1 - c2 = " + c1.minus(c2));
System.out.println("c1 * c2 = " + c1.multiply(c2));
System.out.println("|c1| = " + c1.length());
System.out.println("c1 equals c2: " + c1.equals(c2));
System.out.println("c1 equals c1: " + c1.equals(c1));
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)