使用java语言编写下列题目,3.定义一个哥哥brother类,有姓名年龄妹妹属性 定义一个妹妹sister类,有姓名,年龄,哥哥属性 哥哥类中有一个getinfo方法,其中能显示自己的姓名,年龄和妹妹的姓名年龄 妹妹类中有一个getinfo方法,其中能显示自己的姓名,年龄和哥哥的姓名年龄 定义一个测试类,创建妹妹和哥哥对象,然后测试 4. 一.创建一个tangle类,两个属性width,height 二.在tangle在添加两个方法计算矩形的周长和,面积 三.编程利用tangle输出一个矩形的周长和面积
时间: 2023-10-10 12:05:50 浏览: 108
3. 哥哥类和妹妹类的定义如下:
Brother.java
```java
public class Brother {
private String name;
private int age;
private Sister sister;
public Brother(String name, int age, Sister sister) {
this.name = name;
this.age = age;
this.sister = sister;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Sister getSister() {
return sister;
}
public void setSister(Sister sister) {
this.sister = sister;
}
public void getInfo() {
System.out.println("My name is " + name + ", I'm " + age + " years old.");
if (sister != null) {
System.out.println("My sister's name is " + sister.getName() + ", she's " + sister.getAge() + " years old.");
} else {
System.out.println("I don't have a sister.");
}
}
}
```
Sister.java
```java
public class Sister {
private String name;
private int age;
private Brother brother;
public Sister(String name, int age, Brother brother) {
this.name = name;
this.age = age;
this.brother = brother;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Brother getBrother() {
return brother;
}
public void setBrother(Brother brother) {
this.brother = brother;
}
public void getInfo() {
System.out.println("My name is " + name + ", I'm " + age + " years old.");
if (brother != null) {
System.out.println("My brother's name is " + brother.getName() + ", he's " + brother.getAge() + " years old.");
} else {
System.out.println("I don't have a brother.");
}
}
}
```
测试类Test.java
```java
public class Test {
public static void main(String[] args) {
Sister sister = new Sister("Lucy", 18, null);
Brother brother = new Brother("Tom", 20, sister);
sister.setBrother(brother);
brother.getInfo();
sister.getInfo();
}
}
```
输出结果:
```
My name is Tom, I'm 20 years old.
My sister's name is Lucy, she's 18 years old.
My name is Lucy, I'm 18 years old.
My brother's name is Tom, he's 20 years old.
```
4. 矩形类的定义如下:
Rectangle.java
```java
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
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 double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
```
测试类Test.java
```java
public class Test {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
System.out.println("The area of the rectangle is " + rectangle.getArea());
System.out.println("The perimeter of the rectangle is " + rectangle.getPerimeter());
}
}
```
输出结果:
```
The area of the rectangle is 15.0
The perimeter of the rectangle is 16.0
```
阅读全文