• JavaSE编程题目练习(三)


    博客昵称:架构师Cool
    最喜欢的座右铭:一以贯之的努力,不得懈怠的人生。
    作者简介:一名Coder,欢迎关注小弟!
    博主小留言:哈喽!各位CSDN的uu们,我是你的小弟Cool,希望我的文章可以给您带来一定的帮助
    百万笔记知识库, 所有基础的笔记都在这里面啦,点击左边蓝字即可获取!助力每一位未来架构师!
    欢迎大家在评论区唠嗑指正,觉得好的话别忘了一键三连哦!😘


    接着上面的文章
    JavaSE编程题目练习(一)
    JavaSE编程题目练习(二)

    实验十一

    1、程序改错

    1-1、题目

    下列程序有错,请仔细阅读,找出错误并改正。
    (1) abstract class Man{
    (2) public String name;
    (3) public void Man(String name){
    (4) this.name=name;
    (5) }
    (6) public abstract void print(){ };
    (7) }
    (8) public class Test40 extend Man{
    (9) public Test40(String name){
    (10) super(name);
    (11) }
    (12) public void print(){
    (13) System.out.println(“name=”+name);
    (14) }
    (15) public static void main(String[] args) {
    (16) Test40 xm=new Test40(“tom”);
    (17) xm.print();
    (18) }
    (19) }
    第 行错误,改为
    第 行错误,改为
    第 行错误,改为

    1-2、代码

    第 三 行错误,改为 public Man(String name){
    第 六 行错误,改为 public void print(){ };
    第 八 行错误,改为 public class Test40 extends Man{

    2、计算不同图形面积和周长

    2-1、题目

    1.编写一个抽象类(Shape),长方形、三角形和圆形均为其子类,并各有各的属性。其父类中有计算周长和面积的方法。在测试类中,分别建立如干个对象,计算并输出各对象的周长和面积。

    2-2、代码

    abstract class Shape {
        //计算周长
        public abstract double calculatePerimeter();
        //计算面积
        public abstract double calculateArea();
    
    }
    class Rectangle extends Shape{
        private double length;
        private double width;
    
        public Rectangle(double length, double width) {
            this.length = length;
            this.width = width;
        }
        @Override
        public double calculatePerimeter() {
            return length * width;
        }
        @Override
        public double calculateArea() {
            return length * width;
        }
    }
    class Triangle extends Shape {
        private double side1;
        private double side2;
        private double side3;
        public Triangle(double side1, double side2, double side3) {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
        }
    
        public double calculatePerimeter() {
            return side1 + side2 + side3;
        }
        public double calculateArea() {
            //海伦公式:area=√(s*(s-a)(s-b)(s-c))
            double s = (side1 + side2 + side3) / 2;
            return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
        }
    }
    class Circle extends Shape {
        private double radius;
    
        public Circle(double radius) {
            this.radius = radius;
        }
    
        public double calculatePerimeter() {
            return 2 * Math.PI * radius;
        }
    
        public double calculateArea() {
            return Math.PI * radius * radius;
        }
    }
    public class ShapeTest{
        public static void main(String[] args) {
            Rectangle rectangle = new Rectangle(5, 3);
            System.out.println("Rectangle area: " + rectangle.calculateArea());
            System.out.println("Rectangle perimeter: " + rectangle.calculatePerimeter());
    
            Triangle triangle = new Triangle(3, 4, 5);
            System.out.println("Triangle area: " + triangle.calculateArea());
            System.out.println("Triangle perimeter: " + triangle.calculatePerimeter());
    
            Circle circle = new Circle(4);
            System.out.println("Circle area: " + circle.calculateArea());
            System.out.println("Circle perimeter: " + circle.calculatePerimeter());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    3、计算圆柱体

    3-1、题目

    (1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x 和y值的public方法。
    (2).设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、获取和设置r值的public方法、计算圆面积的public方法。
    (3).设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public方法。
    (4).建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值。

    实验要求:
    . 每个类包含无参数和有参数的构造方法。构造方法用于对成员变量初始化,无参数的构造方法将成员变量初始化为0值。
    .子类的构造方法调用父类的构造方法,对父类中的成员变量初始化。
    .方法名自定;

    3-2、代码

    
    public class Test01 {
        public static void main(String[] args) {
            Cylinder cylinder1 = new Cylinder(1, 2, 3, 4);
            Cylinder cylinder2 = new Cylinder(5, 6, 7, 8);
    
            System.out.println("圆柱体 1 - 轴心坐标: (" + cylinder1.getX() + ", " + cylinder1.getY() + ")");
            System.out.println("圆柱体 1 - 半径: " + cylinder1.getR());
            System.out.println("圆柱体 1 - 高: " + cylinder1.getH());
            System.out.println("圆柱体 1 - 体积: " + cylinder1.calculateCircleVolume());
    
            System.out.println("圆柱体 2 - 轴心坐标: (" + cylinder2.getX() + ", " + cylinder2.getY() + ")");
            System.out.println("圆柱体 2 - 半径: " + cylinder2.getR());
            System.out.println("圆柱体 2 - 高: " + cylinder2.getH());
            System.out.println("圆柱体 2 - 体积: " + cylinder2.calculateCircleVolume());
        }
    }
    class Point{
        protected int x;
        protected int y;
    
        public Point() {
            x = 0;
            y = 0;
        }
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    }
    class Circle extends Point{
        protected int r;
        public Circle() {
            super();
            r = 0;
        }
    
        public Circle(int x, int y, int r) {
            super(x, y);
            this.r= r;
        }
    
        public double getR() {
            return r;
        }
    
        public void setR(int r) {
            this.r = r;
        }
        public double calculateCircleArea() {
            return Math.PI * r * r;
        }
    }
    class Cylinder extends Circle{
        protected int h;
    
        public Cylinder() {
            super();
            h = 0;
        }
    
        public Cylinder(int x, int y, int r, int h) {
            super(x, y, r);
            this.h = h;
        }
    
        public double getH() {
            return h;
        }
    
        public void setH(int h) {
            this.h = h;
        }
        public double calculateCircleVolume() {
            return calculateCircleArea() * h;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    实验十二

    1、接口的使用

    1-1、题目

    接口的使用
    1,定义一个接口Shape,它含有一个抽象方法 double area( )
    2,定义一个表示三角形的类Triangle,该类实现接口Shape。此类中有两个分别用于存储三角形底边和高度的private成员变量int width和int height,在该类实现的方法area中计算并返回三角形的面积。
    3,定义一个表示矩形的类Rectangle,该类实现接口Shape。此类中有两个分别表示矩形长度和高度度的成员变量int width和int height,在该类实现的方法area中计算并返回矩形的面积。
    4,定义一个类ShapeTest,该类中有一个方法如下:
    public static void showArea(Shape s){
    System.out.println(“area=”+s.area());
    }
    在ShapeTest类中定义main函数,在main函数中创建Triang类的对象和Rectangle类的对象,并调用方法showArea两次以输出两个对象的面积。
     思考:两次调用showArea方法时调用的area方法各是在哪个类中定义的方法?答:三角形调用showArea方法时调用的area方法是Triangle类中定义的方法,而矩形调用showArea方法时调用的area方法是Rectangle类中定义的方法

    1-2、代码

    interface Shape {
        double area();
    }
    class Triangle implements Shape{
        private int width=50;
        private int height=20;
        @Override
        public double area() {
            return width*height/2;
        }
    }
    class Rectangle implements Shape{
        private int width=50;
        private int height=20;
        @Override
        public double area() {
            return width*height;
        }
    }
    public class ShapeTest{
        public static void main(String[] args) {
            Triangle triangle = new Triangle();
            Rectangle rectangle = new Rectangle();
            showArea(triangle);
            showArea(rectangle);
        }
        public static void showArea(Shape s){
            System.out.println("area="+s.area());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    2、计算器

    2-1、题目

    利用接口做参数, 写个计算器,能完成加减乘除运算:
    (1)定义一个接口Calculator含有一个方法int computer(int n, int m)。
    (2)设计四个类分别实现此接口,完成加减乘除运算。
    (3)设计一个类Computer,类中含有方法:public void useCal (Calculator com, int op1, int op2),要求调用computer(),对参数进行运算。
    (4)设计一个主类TestCh09_02,调用Computer中的方法computer来完成加减乘除运算。
    运行结果:
    25+6和为:31
    32-12差为:20
    15*5乘积为:75
    16/2商为:8

    2-2、代码

    interface Calculator{
        int computer(int n ,int m);
    }
    class Addition implements Calculator {
        @Override
        public int computer(int n, int m) {
            return n + m;
        }
    
    }
    
    class Subtraction implements Calculator {
        public int computer(int n, int m) {
            return n - m;
        }
    }
    
    class Multiplication implements Calculator {
        public int computer(int n, int m) {
            return n * m;
        }
    }
    
    class Division implements Calculator {
        public int computer(int n, int m) {
            if (m == 0) {
                throw new IllegalArgumentException("除数不能为0");
            }
            return n / m;
        }
    }
    class Computer{
        public void useCal(Calculator com ,int op1,int op2){
            int result = com.computer(op1,op2);
            System.out.print(result);
        }
    }
    public class TestCh09_02 {
        public static void main(String[] args) {
            Computer computer = new Computer();
    
            int operand1 = 25;
            int operand2 = 6;
    
            Calculator addition = new Addition();
            System.out.print(operand1+"+"+operand2+"和为:");
            computer.useCal(addition, operand1, operand2); // 执行加法运算
            System.out.println();
    
            operand1=32;
            operand2=12;
            Calculator subtraction = new Subtraction();
            System.out.print(operand1+"-"+operand2+"差为:");
            computer.useCal(subtraction, operand1, operand2); // 执行减法运算
            System.out.println();
    
            operand1=15;
            operand2=5;
            Calculator multiplication = new Multiplication();
            System.out.print(operand1+"*"+operand2+"乘积为:");
            computer.useCal(multiplication, operand1, operand2); // 执行乘法运算
            System.out.println();
    
            operand1=16;
            operand2=2;
            Calculator division = new Division();
            System.out.print(operand1+"/"+operand2+"商为:");
            computer.useCal(division, operand1, operand2); // 执行除法运算
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    实验十三

    1、监测体重

    1-1、题目

    1.定义一个类来监控体重是否超重,体重指数BMI=体重(kg)÷身高÷身高(m),中国成人居民BMI衡量标准是≤18.4为消瘦、18.5-23.9为正常、24-27.9为超重、≥28为肥胖。体重超重需要提示多运动。运行结果如图13- 1 超重提示图所示:
    在这里插入图片描述

    1-2、代码

    import java.util.Scanner;
    public class ExceptionTest {
        //1.定义一个类来监控体重是否超重,体重指数BMI=体重(kg)÷身高÷身高(m)
        // 中国成人居民BMI衡量标准是≤18.4为消瘦、18.5-23.9为正常
        // 24-27.9为超重、≥28为肥胖。体重超重需要提示多运动。
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            Weight wg = new Weight();
            System.out.println("请输入体重(kg):");
            wg.setWeight(sc.nextDouble());
            System.out.println("请输入身高(m):");
            wg.setHeight(sc.nextDouble());
            double bmi=wg.getBmi(wg);
            if (bmi<=18.4)
                System.out.println("太瘦了,多吃肉");
            else if (bmi<=23.9 && bmi>=18.5)
                System.out.println("非常健康的身体哦");
            else if (bmi<=27.9 && bmi>=24) {
                System.out.println("体重超重,多运动");
            }
            else {
                System.out.println("肥胖人群,需要减肥了");
            }
            System.out.println(bmi);
    
        }
    }
    class Weight{
        private double height;
        private double weight;
        public void setHeight(double height) {
            this.height = height;
        }
        public void setWeight(double weight) {
            this.weight = weight;
        }
        public double getBmi(Weight wg){
            return weight/height/height;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    2、成绩异常检测

    2-1、题目

    定义Student类,其属性:
    private String name;
    private int score;
    自定义IllegalScoreException异常类,代表分数相加后超出合理范围的异常。
    测试学生对象。要求以new作为输入标识,输入一行学生数据,格式为姓名 年龄,后调用addScore。addScore不成功则抛出异常,并打印异常信息,然后如正常则打印学生信息。
    运行结果如图13- 2成绩异常图所示:
    在这里插入图片描述

    2-2、代码

    import java.util.Scanner;
    //todo 还未完成的项目
    public class StudentScore {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String s=scanner.nextLine();
            //判断是否
            while(s.equals("new")){
                System.out.print("请输入姓名:");
                String name = scanner.nextLine();
                System.out.print("请输入成绩:");
                int score = scanner.nextInt();
                Student student = new Student(name,score);
                try {
                    student.addScore(score);
                    System.out.println(student.toString());
                }catch (IllegalScoreException e){
                    System.out.println("IllegalScoreException:"+e.getMessage());
                }
                scanner.nextLine();
                s=scanner.nextLine();
            }
    
        }
    }
    class Student{
        private String name;
        private int score;
    
        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }
    
        public void addScore(int scoreToAdd) throws IllegalScoreException{
            if(scoreToAdd+score > 200) {
                throw new IllegalScoreException(score);
            }
            score=scoreToAdd;
        }
    
        @Override
        public String toString() {
            return "Student[" +
                    "name='" + name + '\'' +
                    ", score=" + score +
                    ']';
        }
    }
    class IllegalScoreException extends RuntimeException{
        public IllegalScoreException(int score){
            super("成绩超过有效范围,成绩为=" + score);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    实验十四

    1、机票

    1-1、题目

    在这里插入图片描述
    在这里插入图片描述

    1-2、代码

    import java.util.Scanner;
    
    public class AirTicketTest {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            double newPrice;
            while (true) {
                System.out.println("输入机票原价:");
                int oldPrice = sc.nextInt();
                if (oldPrice==-1){
                    break;
                }
                System.out.println("输入月份:");
                int mouth = sc.nextInt();
                System.out.println("输入0则为头等舱1则为经济舱:");
                int wareHouse = sc.nextInt();//仓位
                if (mouth > 4 && mouth < 11) {
                    System.out.println("旺季");
                    if (wareHouse == 1) {
                        newPrice = oldPrice * (0.85);
                        System.out.print("经济舱");
                    } else {
                        newPrice = oldPrice * (0.9);
                        System.out.print("头等舱");
                    }
                } else {
                    System.out.println("淡季");
                    if (wareHouse == 1) {
                        System.out.print("经济舱");
                        newPrice = oldPrice * (0.65);
                    } else {
                        newPrice = oldPrice * (0.7);
                        System.out.print("头等舱");
                    }
                }
                System.out.println("票价为" + newPrice);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    2、随机五位数验证码

    2-1、题目

    在这里插入图片描述
    在这里插入图片描述

    2-2、代码

    import java.util.Random;
    
    //验证码
    public class Verification {
        public static void main(String[] args) {
            String verificationCode = getCode();
            System.out.println("验证码:" + verificationCode);
        }
    
        public static String getCode() {
            Random random = new Random();
            StringBuilder code = new StringBuilder();
    
            // 生成前四位随机字母
            for (int i = 0; i < 4; i++) {
                char c = (char) (random.nextInt(26) + 'A');
                code.append(c);
            }
    
            // 生成最后一位随机数字
            int digit = random.nextInt(10);
            code.append(digit);
    
            return code.toString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    3、数字加密

    3-1、题目

    在这里插入图片描述

    3-2、代码

    import java.util.Scanner;
    
    public class Encryption {
        public static void main(String[] args) {
            Code code = new Code();
            Scanner sc = new Scanner(System.in);
            int anInt = sc.nextInt();
            if (anInt>0) {
                code.enCode(anInt);
            }else {
                System.out.println("密码小于0");
            }
    
        }
    }
    class Code{
        public void enCode(int code){
            int qw,bw,sw,gw;
            qw=code/1000;
            bw=code%1000/100;
            sw=code/10%10;
            gw=code%10;
            int firstCode= (qw+5)%10*1000+(bw+5)%10*100+(sw+5)%10*10+(gw+5)%10;
            qw=firstCode/1000;
            bw=firstCode%1000/100;
            sw=firstCode/10%10;
            gw=firstCode%10;
            int nextCode=gw*1000+sw*100+bw*10+qw;
            System.out.println(code+"加密后的结果是"+nextCode);
        }
    }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    4、抢红包

    4-1、题目

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    4-2、代码

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class LuckyDraw {
        public static void main(String[] args) {
            List<Integer> prizes = new ArrayList<>();
            prizes.add(2588);
            prizes.add(888);
            prizes.add(1000);
            prizes.add(10000);
            prizes.add(2);
    
            Collections.shuffle(prizes); // 随机打乱奖项顺序
    
            for (int prize : prizes) {
                if (prize == 888) {
                    System.out.println("888元的奖金被抽出");
                } else if (prize == 588) {
                    System.out.println("588元的奖金被抽出");
                } else if (prize == 1000) {
                    System.out.println("1000元的奖金被抽出");
                } else if (prize == 10000) {
                    System.out.println("10000元的奖金被抽出");
                } else if (prize == 2) {
                    System.out.println("2元的奖金被抽出");
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    实验十五

    1、绘制基本图形

    1-1、题目

    第一题
    利用第三章的绘图知识
    1,画出4个长方形:
    其中一个长方形完全包含在另外一个长方形中;
    第三个长方形与前两个长方形有交叉,当没有完全包含起来;
    第四个长方形和其他三个长方形完全没有交叉。
    在这里插入图片描述
    第二题
    ,2. 改变图形的背景颜色。将其中两个长方形改为椭圆形,修改四个图形的背景颜色,保证每个图形颜色都和其他的不一样。
    在这里插入图片描述

    1-2、代码一

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    
    public class FourRectangle extends Application {
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
            Pane root = new Pane();
    
            // 第一个长方形
            Rectangle rectangle1 = new Rectangle(50, 50, 200, 100);
            rectangle1.setFill(null);
            rectangle1.setStroke(Color.RED);
            root.getChildren().add(rectangle1);
    
            // 第二个长方形,完全包含在第一个长方形中
            Rectangle rectangle2 = new Rectangle(60, 90, 50, 25);
            rectangle2.setFill(null);
            rectangle2.setStroke(Color.BLACK);
            root.getChildren().add(rectangle2);
    
            // 第三个长方形,与第一个和第二个长方形有交叉
            Rectangle rectangle3 = new Rectangle(70, 70, 200, 100);
            rectangle3.setFill(null);
            rectangle3.setStroke(Color.YELLOW);
            root.getChildren().add(rectangle3);
    
            // 第四个长方形,与前三个长方形完全没有交叉
            Rectangle rectangle4 = new Rectangle(300, 300, 200, 100);
            rectangle4.setFill(null);
            rectangle4.setStroke(Color.GREEN);
            root.getChildren().add(rectangle4);
    
            Scene scene = new Scene(root, 600, 600);
            primaryStage.setTitle("Rectangles");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    1-3、代码二

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    
    public class FourRectangle2 extends Application {
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
            Pane root2 = new Pane();
    
            // 第一个长方形
            Rectangle rectangle1 = new Rectangle(50, 50, 200, 100);
            rectangle1.setFill(Color.RED);
            rectangle1.setStroke(Color.BLACK);
            root2.getChildren().add(rectangle1);
    
            // 第二个长方形,完全包含在第一个长方形中
            Rectangle rectangle2 = new Rectangle(60, 90, 50, 25);
            rectangle2.setFill(Color.BLACK);
            rectangle2.setStroke(Color.BLACK);
            root2.getChildren().add(rectangle2);
    
            // 第三个长方形,与第一个和第二个长方形有交叉
            Ellipse ellipse1 = new Ellipse(170, 110, 100, 50);
            ellipse1.setFill(Color.YELLOW);
            ellipse1.setStroke(Color.BLACK);
            root2.getChildren().add(ellipse1);
    
            // 第四个长方形,与前三个长方形完全没有交叉
            Ellipse ellipse2 = new Ellipse(300, 300, 100, 50);
            ellipse2.setFill(Color.GREEN);
            ellipse2.setStroke(Color.GREEN);
            root2.getChildren().add(ellipse2);
    
            Scene scene = new Scene(root2, 600, 600);
            primaryStage.setTitle("Rectangles");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    2、修改snowman图形

    2-1、题目

    按照以下要求修改程序Snowman.java(参考源码)
    将雪人的嘴型变成哭脸的倒弧嘴样;
    把太阳移动到图片的右上角;
    在图片左上角显示你的名字;
    将整个雪人右移50个像素。
    在这里插入图片描述
    在这里插入图片描述

    2-2、代码

    import javafx.application.Application;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Arc;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.shape.Line;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Text;
    
    
    public class SnowMan extends Application {
        @Override
        public void start(Stage stage) {
            try {
                //最底层的雪球
                Ellipse baseSnowball =new Ellipse(80,210,80,60);
                baseSnowball.setFill(Color.WHITE);
                // 连接的中间雪球
                Ellipse connectSnowball =new Ellipse(80,130,50,40);
                connectSnowball.setFill(Color.WHITE);
                //头部的雪球
                Circle headerSnowball = new Circle(80,70,30);
                headerSnowball.setFill(Color.WHITE);
    
                //眼部
                Circle rightEye = new Circle(70,60,5);
                Circle leftEye = new Circle(90,60,5);
                // 手部
                Line leftArm= new Line(110,130,160,130);
                leftArm.setStrokeWidth(3);
                Line rightArm= new Line(50,130,0,100);
                rightArm.setStrokeWidth(3);
                // 嘴巴
                Arc mouth = new Arc(80,85,15,10,360,180);
                mouth.setFill(null);
                mouth.setStrokeWidth(2);
                mouth.setStroke(Color.BLACK);//设置画笔颜色
                //纽扣
                Circle upperButton = new Circle(80,120,6);
                upperButton.setFill(Color.RED);
                Circle lowerButton = new Circle(80,140,6);
                lowerButton.setFill(Color.RED);
    
    
                Rectangle upperHat = new Rectangle(60,0,40,50);
                Rectangle lowerHat = new Rectangle(50,45,60,5);
    
                //将图案放入面板
                Group hat = new Group(upperHat,lowerHat);
                hat.setTranslateX(10);
                hat.setRotate(15);
                // 组成雪人
                Group snowman = new Group(baseSnowball,connectSnowball,headerSnowball,leftEye,rightEye,leftArm,rightArm,mouth,
                        upperButton,lowerButton,hat);
                snowman.setTranslateX(170);
                snowman.setTranslateY(50);
                // 太阳
                Circle sun = new Circle(450,50,30);
                sun.setFill(Color.GOLD);
                // 下面的蓝色背景
                Rectangle ground = new Rectangle(0,250,500,100);
                ground.setFill(Color.STEELBLUE);
                //文字栏目
                Text bannerName=new Text();
                bannerName.setText("理工学院软件学院");
                bannerName.setX(50);
                bannerName.setY(50);
                // 组成图形
                Group root = new Group(ground,sun,snowman,bannerName);
                Scene scene = new Scene(root,500,350,Color.LIGHTBLUE);//画板背景为浅蓝色
                stage.setScene(scene);
                stage.setTitle("Snowman");
                stage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    3、绘制饼图

    3-1、题目

    编写一段JavaFX小程序,保存为PieChat.java,给出家庭收入的消费状况,具体数据如下:
    Rent and Utilities 35%
    Transportation 15%
    Food 15%
    Education 25%
    Miscellaneous 10%

    要求:饼图的每个部分要有不同的颜色。给每个部分设定一个标签,该标签出现在饼图的外围部分(提示:使用Arc方法画扇形图)。
    在这里插入图片描述

    3-2、代码

    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.chart.PieChart;
    import javafx.stage.Stage;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class PieChat extends Application {
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) {
            // 创建饼图的数据列表
            List<PieChart.Data> pieChartData = Arrays.asList(
    
                    new PieChart.Data("Miscellaneous", 10),
                    new PieChart.Data("Education", 25),
                    new PieChart.Data("Food", 15),
                    new PieChart.Data("Transportation", 15),
                    new PieChart.Data("Rent and Utilities", 35)
            );
    
            // 创建饼图
            PieChart pieChart = new PieChart();
            pieChart.getData().addAll(pieChartData);
            pieChart.setLabelLineLength(0);//设置标签线长度
    
            // 设置饼图的颜色
            int colorIndex = 0;
            for (PieChart.Data data : pieChartData) {
                data.getNode().setStyle("-fx-pie-color: " + getColor(colorIndex));
                colorIndex++;
            }
    
            pieChart.setLegendVisible(false);//取消图例
            // 创建场景
            Group root = new Group(pieChart);
            Scene scene = new Scene(root);
    
            stage.setTitle("Expense Pie Chart");
            stage.setScene(scene);
            stage.show();
        }
        // 获取不同的颜色
        private String getColor(int index) {
            String[] colors = { "PINK", "#0000FF", "#00FFFF","GREEN", "#FF0000"};
            return colors[index % colors.length];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
  • 相关阅读:
    工业品电商,新的隐形冠军藏不住了?
    图扑数字孪生北京故宫,推进旅游业元宇宙进程
    数据结构与算法之集合: Leetcode 349. 两个数组的交集 (Typescript版)
    pytest运行时参数说明,pytest详解,pytest.ini详解
    nginx代理minio教程 避坑过的教程 避开SignatureDoesNotMatch
    【将文本编码为图像灰度级别】以 ASCII 编码并与灰度级别位混合将文本字符串隐藏到图像像素的最低位中,使其不明显研究(Matlab代码实现)
    算法与数据结构 --- 栈和队列的定义与特点,以及案例引入
    极智Paper | 性能数据炸裂的多模态模型BEiT-3:Image as a Forign Language
    NSSCTF第13页(1)
    【D3.js】1.20-给 D3 元素添加工具提示
  • 原文地址:https://blog.csdn.net/qq_57473444/article/details/133952306