一只小菜鸡。。 2022-10-14 12:20 采纳率: 63.2%
浏览 311
已结题

java写个类,写写注释就更好了

模仿circle类 /1、成员变量,针对类的属性 private int r; //2、成员方法,针对类的行为 public int getR()//get访问器,getter { return r; } public void setR(int a)//set访问器,setter { r = a; } public double getArea() { return Math.PI * r * r; }}

public class CircleTest { public static void main(String[] args) { // TODO Auto-generated method stub Circle c = new Circle(); c.setR(3); System.out.println("半径为" + c.getR() + "的圆的面积是" + c.getArea()); }}

写出矩形类、三角形类。随机生成100个长度和宽度范围在[1, 100]的矩形,然后对这些矩形排序。Rectangle[] a = new Rectangle[100];示例结果:第1大的矩形为:98 * 96 = ……第2大的矩形为:97 * 96 = ……第100大的矩形为:3 * 4 = 12

  • 写回答

5条回答 默认 最新

  • Java Fans 全栈领域优质创作者 2022-10-14 14:13
    关注

    1、矩形类:

    package cn.personal.demo06;
    
    public class Rectangle {
    
        // 定义成员变量,宽和高
        private int width;
        private int heigh;
    
        // 添加有参构造方法
        public Rectangle(int width, int heigh) {
            super();
            this.width = width;
            this.heigh = heigh;
        }
    
        // 添加get/set方法
        public int getWidth() {
            return width;
        }
    
        public void setWidth(int width) {
            this.width = width;
        }
    
        public int getHeigh() {
            return heigh;
        }
    
        public void setHeigh(int heigh) {
            this.heigh = heigh;
        }
    
        //获取面积的方法
        public int getArea(int wi,int he) {
            return wi*he;
        }
    }
    

    2、矩形测试类

    package cn.personal.demo06;
    
    import java.util.Arrays;
    
    public class RectangleTest {
    
        public static void main(String[] args) {
    
            // 定义Rectangle数组,长度为100;
            Rectangle[] rects = new Rectangle[100];
            
            // 定义Rectangle中的width数组,长度为100;
            int[] widths = new int[100];
            
            // 定义Rectangle中的height数组,长度为100;
            int[] heights = new int[100];
            
            for (int i = 0; i < rects.length; i++) {
                int wr=getRandon();
                int hr=getRandon();
                rects[i]=new Rectangle(wr, hr);
                widths[i] = rects[i].getWidth();
                heights[i] = rects[i].getHeigh();
            }
    
            //将宽度和高度进行升序排序
            Arrays.sort(widths);
            Arrays.sort(heights);
            
            //循环输出打印
            for (int i = 1; i <= rects.length; i++) {
                System.out.println("第"+i+"大的矩形为:"
                        +widths[rects.length-i]+"*"+heights[rects.length-i]+"="
                        +rects[rects.length-i].getArea(widths[rects.length-i],heights[rects.length-i]));
            }
        }
    
        public static int getRandon() {
            return (int) (Math.random() * 99 + 1);
        }
    }
    

    3、矩形测试效果

    img

    1、三角形类

    package cn.personal.demo06;
    
    public class Triangle {
        
        //定义成员变量,三条边
        private int line1;
        private int line2;
        private int line3;
    
        //添加有参构造
        public Triangle(int line1, int line2, int line3) {
            super();
            this.line1 = line1;
            this.line2 = line2;
            this.line3 = line3;
        }
        
        //添加get/set方法
        public int getLine1() {
            return line1;
        }
    
        public void setLine1(int line1) {
            this.line1 = line1;
        }
    
        public int getLine2() {
            return line2;
        }
    
        public void setLine2(int line2) {
            this.line2 = line2;
        }
    
        public int getLine3() {
            return line3;
        }
    
        public void setLine3(int line3) {
            this.line3 = line3;
        }
    
        //添加求面积方法
        public double getArea() {
            double p = (line1 + line2 + line3) / 2d;
            return Math.sqrt(p * (p - line1) * (p - line2) * (p - line3));
        }
    
    }
    

    2、三角形测试类

    package cn.personal.demo06;
    
    public class TriangleTest {
    
        public static void main(String[] args) {
    
            Triangle triangle = new Triangle(3, 4, 5);
            System.out.println("三边长为" + triangle.getLine1() + ","
                    + triangle.getLine2() + "," + triangle.getLine3() + "的三角形面积:"
                    + triangle.getArea());
            triangle = new Triangle(6, 7, 8);
            System.out.println("三边长为" + triangle.getLine1() + ","
                    + triangle.getLine2() + "," + triangle.getLine3() + "的三角形面积:"
                    + triangle.getArea());
        }
    
    }
    

    3、三角形测试效果

    img

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月21日
  • 创建了问题 10月14日

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了