Java.创建一个tangle类,两个属性width、height。 2.在tangle中添加两个方法计算矩形的周长和面积。 3.编程利用tangle输出一个矩形的周长和面积。
时间: 2023-11-27 17:52:20 浏览: 126
下面是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 getPerimeter() {
return 2 * (width + height);
}
public double getArea() {
return width * height;
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(3.5, 4.2);
System.out.println("矩形的周长为:" + rectangle.getPerimeter());
System.out.println("矩形的面积为:" + rectangle.getArea());
}
}
```
输出结果为:
```
矩形的周长为:15.4
矩形的面积为:14.7
```
相关问题
使用java语言编写下列题目,3.定义一个哥哥brother类,有姓名年龄妹妹属性 定义一个妹妹sister类,有姓名,年龄,哥哥属性 哥哥类中有一个getinfo方法,其中能显示自己的姓名,年龄和妹妹的姓名年龄 妹妹类中有一个getinfo方法,其中能显示自己的姓名,年龄和哥哥的姓名年龄 定义一个测试类,创建妹妹和哥哥对象,然后测试 4. 一.创建一个tangle类,两个属性width,height 二.在tangle在添加两个方法计算矩形的周长和,面积 三.编程利用tangle输出一个矩形的周长和面积
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
```
.Java创建一个tangle类,两个属性width、height。
下面是一个简单的Java代码示例,创建了一个名为"Tangle"的类,具有两个属性width和height:
```
public class Tangle {
private int width;
private int height;
public Tangle(int w, int h) {
this.width = w;
this.height = h;
}
public int getWidth() {
return this.width;
}
public void setWidth(int w) {
this.width = w;
}
public int getHeight() {
return this.height;
}
public void setHeight(int h) {
this.height = h;
}
}
```
在这个示例中,我们使用私有属性(width和height)来存储矩形的宽度和高度。我们还定义了一个构造函数来初始化这些属性,并使用公共getter和setter方法来访问和修改这些属性。
阅读全文