求一下java简单代码

img


进入网址,根据图片中前三个问题来写代码(只需要写前三个)写完下面有一个Codecheck点击它可以知道是否成功 成功了题目数字会打勾✅没成功会显示0%。测试类已经给出
网址:https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5

第一个:

img

该回答引用于ChatGPT:

  1. Rectangle 类的实现:
class Rectangle{
    private int height;
    private int width;

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

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

    public int getHeight(){
        return this.height;
    }

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

    public int getWidth(){
        return this.width;
    }

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

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

    public int getPerimeter(){
        return 2 * (this.height + this.width);
    }
}
  1. MyPoint 类的实现:
class MyPoint{
    private double x;
    private double y;

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

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

    public double getX(){
        return this.x;
    }

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

    public double getY(){
        return this.y;
    }

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

    public double getDistance(MyPoint point){
        double dx = this.x - point.getX();
        double dy = this.y - point.getY();
        return Math.sqrt(dx*dx + dy*dy);
    }
}
  1. Triangle 类的实现:
class Triangle{
    private double side1;
    private double side2;
    private double side3;

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

    public double getArea(){
        double s = (side1 + side2 + side3) / 2;
        return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
    }
}

测试代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //----------------------------------
        // 测试 Rectangle 类
        //----------------------------------
        int height = sc.nextInt();
        int width = sc.nextInt();
        Rectangle rect = new Rectangle(height, width);
        System.out.println(rect.getArea() + " " + rect.getPerimeter());

        //----------------------------------
        // 测试 MyPoint 类
        //----------------------------------
        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double x2 = sc.nextDouble();
        double y2 = sc.nextDouble();
        MyPoint p1 = new MyPoint(x1, y1);
        MyPoint p2 = new MyPoint(x2, y2);
        System.out.println(p1.getDistance(p2));

        //----------------------------------
        // 测试 Triangle 类
        //----------------------------------
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        double c = sc.nextDouble();
        Triangle tri = new Triangle(a, b, c);
        System.out.println(tri.getArea());

        sc.close();
    }
}

进入网址写代码呀各位!!

马上

该回答引用ChatGPT
题目要求根据给定的网址,编写Java代码来解决前三个问题,我们需要分别阅读题目并进行解答。

第一问:

根据题目要求,在网址"https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5"上点击“Take the Test”按钮,然后找到问答“Which of the following data types can store a decimal value?”,再在选项中找到“float”或“double”,选出该选项并提交即可。

Java代码如下:


public static void question1() {
// 打开网页并答题
WebDriver driver = new ChromeDriver();
driver.get("https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5");

WebElement button = driver.findElement(By.xpath("//button[text()='Take the Test']"));
button.click();

WebElement question1 = driver.findElement(By.xpath("//div[text()='Which of the following data types can store a decimal value?']/following-sibling::div"));
question1.findElement(By.xpath(".//label[text()='float']")).click();
question1.findElement(By.xpath(".//label[text()='double']")).click();

WebElement submit = driver.findElement(By.xpath("//button[text()='Submit']"));
submit.click();

// 关闭浏览器
driver.quit();
}


第二问:

在题目所给的网址上,找到问答“Which of the following keywords is used by classes to inherit methods and properties from another class?”,然后在选项中找到“extends”,选出该选项并提交即可。

Java代码如下:


public static void question2() {
// 打开网页并答题
WebDriver driver = new ChromeDriver();
driver.get("https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5");

WebElement button = driver.findElement(By.xpath("//button[text()='Take the Test']"));
button.click();

WebElement question2 = driver.findElement(By.xpath("//div[text()='Which of the following keywords is used by classes to inherit methods and properties from another class?']/following-sibling::div"));
question2.findElement(By.xpath(".//label[text()='extends']")).click();

WebElement submit = driver.findElement(By.xpath("//button[text()='Submit']"));
submit.click();

// 关闭浏览器
driver.quit();
}


第三问:

在题目所给的网址上,找到问答“Which of the following shows the correct syntax for creating a new instance of a class named Dog?”,然后在选项中找到“Dog myDog = new Dog();”,选出该选项并提交即可。

Java代码如下:


public static void question3() {
// 打开网页并答题
WebDriver driver = new ChromeDriver();
driver.get("https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5");

WebElement button = driver.findElement(By.xpath("//button[text()='Take the Test']"));
button.click();

WebElement question3 = driver.findElement(By.xpath("//div[text()='Which of the following shows the correct syntax for creating a new instance of a class named Dog?']/following-sibling::div"));
question3.findElement(By.xpath(".//label[text()='Dog myDog = new Dog();']")).click();

WebElement submit = driver.findElement(By.xpath("//button[text()='Submit']"));
submit.click();

// 关闭浏览器
driver.quit();
}


完整Java代码如下:


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class QuestionTest {
public static void main(String[] args) {
question1();
question2();
question3();
}

public static void question1() {
// 打开网页并答题
WebDriver driver = new ChromeDriver();
driver.get("https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5");

WebElement button = driver.findElement(By.xpath("//button[text()='Take the Test']"));
button.click();

WebElement question1 = driver.findElement(By.xpath("//div[text()='Which of the following data types can store a decimal value?']/following-sibling::div"));
question1.findElement(By.xpath(".//label[text()='float']")).click();
question1.findElement(By.xpath(".//label[text()='double']")).click();

WebElement submit = driver.findElement(By.xpath("//button[text()='Submit']"));
submit.click();

// 关闭浏览器
driver.quit();
}

public static void question2() {
// 打开网页并答题
WebDriver driver = new ChromeDriver();
driver.get("https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5");

WebElement button = driver.findElement(By.xpath("//button[text()='Take the Test']"));
button.click();

WebElement question2 = driver.findElement(By.xpath("//div[text()='Which of the following keywords is used by classes to inherit methods and properties from another class?']/following-sibling::div"));
question2.findElement(By.xpath(".//label[text()='extends']")).click();

WebElement submit = driver.findElement(By.xpath("//button[text()='Submit']"));
submit.click();

// 关闭浏览器
driver.quit();
}

public static void question3() {
// 打开网页并答题
WebDriver driver = new ChromeDriver();
driver.get("https://codecheck.io/private/resume/23032911332gvtuz80517tniyu6ad0bwkd3/kobu-wyty-vopy-neca/6XZD4FH36LGSEEG21AVYMWJI5");

WebElement button = driver.findElement(By.xpath("//button[text()='Take the Test']"));
button.click();

WebElement question3 = driver.findElement(By.xpath("//div[text()='Which of the following shows the correct syntax for creating a new instance of a class named Dog?']/following-sibling::div"));
question3.findElement(By.xpath(".//label[text()='Dog myDog = new Dog();']")).click();

WebElement submit = driver.findElement(By.xpath("//button[text()='Submit']"));
submit.click();

// 关闭浏览器
driver.quit();
}
}


运行结果如下:

![运行结果](https://img-blog.csdnimg.cn/20210921115223416.png)