jscfa-java作业 20251030
文章内容发布于 2 天前;最后修改于 2 日前。其中的信息可能发生变化或产生更改,敬请留意。

作业 1

原题

应用JAVA类的创建与应用方法,完成四边形类的设计,功能包括求其周长与面积,已知条件为四个点的坐标 (先定义点Point类,属性坐标x,y;功能可以求两个点之间的长度,再定义四边形类,功能包括求周长与面积,求面积化为两个三角形面积和,三角形面积由海伦公式求)

代码

  • Point.java
public class Point {
    private  int x;
    private int y;
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Point() {
    }
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public double distance (Point p1, Point p2) {
        return (double)(Math.sqrt(Math.pow(p1.x-p2.x, 2)+Math.pow(p1.y-p2.y, 2)));
    }
}
  • Quadrilateral.java
public class Quadrilateral {
    private Point p1;
    private Point p2;
    private Point p3;
    private Point p4;
    public Point getP1() {
        return p1;
    }
    public Point getP2() {
        return p2;
    }
    public Point getP3() {
        return p3;
    }
    public Point getP4() {
        return p4;
    }
    public void setP1(Point p) {
        this.p1 = p;
    }
    public void setP2(Point p) {
        this.p2 = p;
    }
    public void setP3(Point p) {
        this.p3 = p;
    }
    public void setP4(Point p) {
        this.p4 = p;
    }
    public Quadrilateral() {
    }
    public Quadrilateral(Point p1, Point p2, Point p3, Point p4) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
        this.p4 = p4;
    }
    public double areaOfTriangle(Point p1, Point p2, Point p3) {
        double a = this.p1.distance(p1, p2);
        double b = this.p1.distance(p2, p3);
        double c = this.p1.distance(p1, p3);
        double p = (a+b+c)/2;
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }
    public double areaOfQuadrilateral(Point p1, Point p2, Point p3, Point p4) {
        double triangleA = areaOfTriangle(p1, p2, p3);
        double triangleB = areaOfTriangle(p1, p4, p3);
        return triangleA + triangleB;
    }
    public double perimeterOfQuadrilateral(Point p1, Point p2, Point p3, Point p4) {
        double a =  this.p1.distance(p1, p2);
        double b =  this.p1.distance(p2, p3);
        double c =  this.p1.distance(p3, p4);
        double d =  this.p1.distance(p4, p1);
        return (a + b + c + d);
    }
}
  • Main.java
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Point p1 = new Point();
        Point p2 = new Point();
        Point p3 = new Point();
        Point p4 = new Point();
        try (Scanner sc = new Scanner(System.in)) {
            p1.setX(sc.nextInt());
            p1.setY(sc.nextInt());
            p2.setX(sc.nextInt());
            p2.setY(sc.nextInt());
            p3.setX(sc.nextInt());
            p3.setY(sc.nextInt());
            p4.setX(sc.nextInt());
            p4.setY(sc.nextInt());
        }
        Quadrilateral Q = new Quadrilateral(p1, p2, p3, p4);
        System.out.println(Q.areaOfQuadrilateral(p1, p2, p3, p4));
        System.out.println(Q.perimeterOfQuadrilateral(p1, p2, p3, p4));
    }
}

输入与输出

预期输入

输入8个int类型数值,分别表示p1(x, y)p2(x, y)p3(x, y)p4(x, y)

预期输入示例

1 1 2 1 2 2 1 2

预期输出

输出两个数,分别表示四边形面积和四边形周长。

预期输出示例

0.9999999999999997
4.0

说明

  1. 未设置判断四边形是否成立的逻辑;
  2. 受限于浮点精度问题,面积的计算不精准。

作业2

原题

应用二维数组,完成四个学生五门课程成绩的统计,功能包括每个人的总分、平均分,每门课的平均分、最高分、最低分

代码

  • Main.java
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        // 创建数组
        // arr[i] -> 每个学生
        // arr[i][j] -> 学生的每门课
        int[][] arr = new int[4][5];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
        sc.close();
        // 计算每个学生的总分
        for (int i = 0; i < arr.length; i++) {
            int sum = 0;
            for (int j = 0; j < arr[i].length; j++) {
                sum += arr[i][j];
            }
            System.out.println(sum);
        }
        // 计算每个学生的均分
        for (int i = 0; i < arr.length; i++) {
            double sum = 0;
            for (int j = 0; j < arr[i].length; j++) {
                sum += arr[i][j];
            }
            sum = (sum / arr[i].length);
            System.out.println(sum);
        }
        // 每门课的平均分
        for (int i = 0; i < arr[0].length; i++) {
            double sum = 0;
            int count = 0;
            for (int j = 0; j < arr.length; j++) {
                sum += arr[j][i];
                count++;
            }
            sum = (sum / count);
            System.out.println(sum);
        }
        // 每门课的最高分
        for (int i = 0; i < arr[0].length; i++) {
            int max = 0;
            for (int j = 0; j < arr.length; j++) {
                if (arr[j][i] > max) {
                    max = arr[j][i];
                }
            }
            System.out.println(max);
        }
        // 每门课的最低分
        for (int i = 0; i < arr[0].length; i++) {
            int min = 100;
            for (int j = 0; j < arr.length; j++) {
                if (arr[j][i] < min) {
                    min = arr[j][i];
                }
            }
            System.out.println(min);
        }
    }
}

输入与输出

预期输入

输入20个int类型数值(输入范围1-100),分别表示学生1的科目1、科目2、科目3、科目4、科目5;学生2的科目1、科目2、科目3、科目4、科目5;学生3的科目1、科目2、科目3、科目4、科目5;学生4的科目1、科目2、科目3、科目4、科目5。

预期输入示例

62 14 89 33 7 91 47 25 78 55 3 96 41 19 72 84 11 50 68 29

预期输出

输出20个数,学生1的总分、学生2的总分、学生3的总分、学生4的总分;学生1的均分、学生2的均分、学生3的均分、学生4的均分;科目1的均分、科目2的均分、科目3的均分、科目4的均分、科目5的均分;科目1的最高分、科目2的最高分、科目3的最高分、科目4的最高分、科目5的最高分;科目1的最底分、科目2的最底分、科目3的最底分、科目4的最底分、科目5的最底分。

预期输出示例

205
296
231
242
41.0
59.2
46.2
48.4
60.0
42.0
51.25
49.5
40.75
91
96
89
78
72
3
11
25
19
7
文章「jscfa-java作业 20251030」,由本站用户「Admin」发布。文章仅代表Admin观点,不代表本站立场。
页面网页地址「https://xiaozhiyuqwq.top/p/2779」。
如您对文章及其附件提出版权主张,或进行引用转载等,请查看我们的【版权声明】
本页面暂时没有评论......

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇