任务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 20:59:30 浏览: 74
ChitGPT是一个基于深度学习的自然语言处理模型,它的工作原理是通过预训练和微调的方式来生成自然语言文本。ChitGPT的应用场景包括文本生成、对话生成、摘要生成等。它可以广泛应用于机器人客服、智能问答、智能写作等领域。
下面是您提出的任务:
任务1:Rectangle类的代码如下:
```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 String getColorAndSize() {
return "This rectangle is " + color + " and has a width of " + width + " and a height of " + height;
}
}
```
任务2:Complex类的代码如下:
```java
public class Complex {
private double real;
private double imaginary;
public Complex() {
this.real = 0;
this.imaginary = 0;
}
public Complex(double real) {
this.real = real;
this.imaginary = 0;
}
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public Complex(String str) {
String[] parts = str.split("\\+|i");
this.real = Double.parseDouble(parts[0]);
this.imaginary = Double.parseDouble(parts[1]);
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public Complex add(Complex other) {
return new Complex(this.real + other.real, this.imaginary + other.imaginary);
}
public Complex minus(Complex other) {
return new Complex(this.real - other.real, this.imaginary - other.imaginary);
}
public Complex multiply(Complex other) {
double newReal = this.real * other.real - this.imaginary * other.imaginary;
double newImaginary = this.real * other.imaginary + this.imaginary * other.real;
return new Complex(newReal, newImaginary);
}
public double length() {
return Math.sqrt(this.real * this.real + this.imaginary * this.imaginary);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Complex other = (Complex) obj;
return Double.compare(this.real, other.real) == 0 && Double.compare(this.imaginary, other.imaginary) == 0;
}
@Override
public String toString() {
return this.real + "+" + this.imaginary + "i";
}
}
```
在main方法中,我们可以测试Rectangle类和Complex类的各项功能,例如:
```java
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 10, "blue");
System.out.println(rectangle); // 输出:Rectangle{width=5.0, height=10.0, color='blue'}
System.out.println(rectangle.getColorAndSize()); // 输出:This rectangle is blue and has a width of 5.0 and a height of 10.0
System.out.println(rectangle.getArea()); // 输出:50.0
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
System.out.println(c1.add(c2)); // 输出:4.0+6.0i
System.out.println(c1.minus(c2)); // 输出:-2.0-2.0i
System.out.println(c1.multiply(c2)); // 输出:-5.0+10.0i
System.out.println(c1.length()); // 输出:2.23606797749979
System.out.println(c1.equals(new Complex(1, 2))); // 输出:true
System.out.println(c1.equals(new Complex(2, 1))); // 输出:false
System.out.println(c1); // 输出:1.0+2.0i
}
```
阅读全文