求图片里前三个问题的代码

img



(1)创建一个简单的表示矩形的Rectangle类,满足以下条件:
◎定义两个成员变量height和width,表示短形的长和宽,类型为整型
②定义一个getArea方法,返回短形的面积
③定义一个getPerimeter方法,返回短形的周长
◎特类的成员变量进行封装,并编写相应的getter和setter方法
⑤设计无参构造方法和两个参数的构造方法
◎创建测试类,在main两数中,利用输入的2个参数分别作为矩形的长和宽,调用
getArea和getPermeter方法,计算并返回矩形的面积和周长
【输入要求】输入2个正整数,中间用空格隔开,分别作为短形的长和宽。
例如:58
【输出要求】输出2个正整数,中间用空格隔开,分别表示矩形的面积和周长。
例如:4026
(2)编写一个表示二维平面上的点的类MyPoint,满足以下条件:
①定义private的成员变量x和y,表示点的×和y坐标,类型为double ②定义两个MyPoint的构造方法,一个构造方法不带参数,而且×和y的初始值为 0,另一个构造方法有两个参数,参数名为x和y,类型为double,用这两个参数分
别作为初始×和y坐标 ③定义一个getDistance方法,有一个类型为MyPoint的对象参数,功能为返回当
前对象和参数对象这两个坐标点的距离,返回值为double类型
⑨封装MyPoint的属性,编写相应的getter和setter方法
⑨编写测试类,在main方法,调用getDistance计算两个点之间的距离。 【输入要求】输入2行数据,总共4个数。每2个数据一组,表示一个点的x和y坐
标,每行的2个数据用空格隔开。例如:
200.1200.2
200.3200.4
【输出要求】输出两个点之间的距离。例如:
0.28284271247464315
(3)编写一个三角形类Triangle,能根据输入的3个double型数据构造三角形对象, ①定义如下构造方法:如果这了个数据满足构成三角形的条件,直接构造三角形。否 则,如果3个数的最大值大于0,则构造以最大值为边的等边三角形。如果最大值也
不大于0,则将三角形类的三边都初始化为0。
②定义一个getArea方法,计算所构造的三角形的面积,返回类型为double。
③编写main方法,测试getArea方法,计算三角形的面积。
【输入要求】输入三个有理数,中间用空格隔开。例如:
8.96.47.2
【输出要求】输出三角形的面积。例如:
22.78812396293297

该回答引用于ChatGPT:
(1)Rectangle类代码如下:

public class Rectangle {
    private int height;
    private int width;

    public Rectangle() {

    }

    public Rectangle(int height, int width) {
        this.height = height;
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getArea() {
        return height * width;
    }

    public int getPerimeter() {
        return 2 * (height + width);
    }
}

测试类代码如下:

import java.util.Scanner;

public class RectangleTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int height = scanner.nextInt();
        int width = scanner.nextInt();
        Rectangle rectangle = new Rectangle(height, width);
        System.out.println(rectangle.getArea() + " " + rectangle.getPerimeter());
    }
}

(2)MyPoint类代码如下:

public class MyPoint {
    private double x;
    private double y;

    public MyPoint() {
        this.x = 0;
        this.y = 0;
    }

    public MyPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getDistance(MyPoint point) {
        double distance = Math.sqrt((x - point.x) * (x - point.x) + (y - point.y) * (y - point.y));
        return distance;
    }
}

测试类代码如下:

import java.util.Scanner;

public class MyPointTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        MyPoint point1 = new MyPoint(x1, y1);
        MyPoint point2 = new MyPoint(x2, y2);
        System.out.println(point1.getDistance(point2));
    }
}

(3)Triangle类代码如下:

public class Triangle {
    private double a;
    private double b;
    private double c;

    public Triangle(double a, double b, double c) {
        if (a + b > c && a + c > b && b + c > a) {
            this.a = a;
            this.b = b;
            this.c = c;
        } else if (Math.max(Math.max(a, b), c) > 0) {
            this.a = Math.max(Math.max(a, b), c);
            this.b = this.a;
            this.c = this.a;
        } else {
            this.a = 0;
            this.b = 0;
            this.c = 0;
        }
    }

    public double getArea() {
        double p = (a + b + c) / 2;
        double area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
        return area;
    }
}

测试类代码如下:

import java.util.Scanner;

public class TriangleTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
        Triangle triangle = new Triangle(a, b, c);
        System.out.println(triangle.getArea());
    }
}
package pack2;
 
public class TestRectangle {    //测试矩形类
    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle(4, 40);
        Rectangle rectangle2 = new Rectangle(3.5, 35.9);
        
        System.out.println("The first rectangle: ");
        System.out.println("Width: " + rectangle1.getWidth() + 
                "\nHeight: " + rectangle1.getHeight() +
                "\nArea: " + rectangle1.getArea() + 
                "\nPerimeter: " + rectangle1.getPerimeter());
        System.out.println();
        
        System.out.println("The second rectangle: ");
        System.out.println("Width: " + rectangle2.getWidth() + 
                "\nHeight: " + rectangle2.getHeight() +
                "\nArea: " + rectangle2.getArea() + 
                "\nPerimeter: " + rectangle2.getPerimeter());
    }
}
 
class Rectangle {    //矩形类
    private double width;    //宽度
    private double height;    //高度
    
    public Rectangle() {
        this(1, 1);
    }
 
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    public double getArea() {
        return width * height;
    }
    
    public double getPerimeter() {
        return (width + height) * 2;
    }
 
    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;
    }
}
 
 
 
    

【1】

img

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^