zoukankan      html  css  js  c++  java
  • Java第六课课后作业

    Java第六课课后作业

      1. 创建一个int型数组,随机填充数组元素,用冒泡进行排序,并输出

        public class H1 {
           /**
            * 存储数组
            */
           int[] arr;
           /**
            * 当前数组的位置
            */
           int index = 0;

           /**
            * 无参初始化数组
            */
           public H1() {
               this(4);
          }

           /**
            * 自定义数组长度
            * @param capacity
            */
           public H1(int capacity) {
               this.arr = new int[capacity];
          }
           public void addNumber() {
               Random random = new Random();
               if (index == arr.length) {
                   int length = arr.length + (arr.length >> 1);
                   arr = Arrays.copyOf(arr, length);
              }
               arr[index++] = random.nextInt(20);
          }
           public void sortNumber() {
               if (arr.length == 0) {
                   System.out.println("数组为空,无法排序");
              } else {
                   for (int i = 0; i < arr.length - 1; i++) {
                       for (int j = 0; j < arr.length - i - 1; j++) {
                           if (arr[j] > arr[j + 1]) {
                               int temp = arr[j];
                               arr[j] = arr[j + 1];
                               arr[j + 1] = temp;
                          }
                      }
                  }
              }
          }
           public static void main(String[] args) {
               H1 h1 = new H1();
               for (int i = 0; i <= 10; i++) {
                   h1.addNumber();
              }
               System.out.println("原数组:"+Arrays.toString(h1.arr));
               h1.sortNumber();
               System.out.println("排序后的数组:"+Arrays.toString(h1.arr));
          }
        }
      2. 类:长方形.java 长 int 宽 int

        类: 正方形.java 边长 int

        类:形状.java zhouchang(长方形) int zhouchang(正方形) int

        package day06.homework;

        /**
        * copyright(c)2021 YCKJ.ALL rights Reserved
        * <p>
        * 描述:
        *
        * @author tanyi
        * @version 1.0
        * @date
        */
        public class Shape {
           /**
            * 计算长方形的周长
            * @param length 长方形的长
            * @param width 长方形的宽
            * @return 返回长方形的周长
            */
           public int rectangle(int length, int width) {
               return (length + width) * 2;
          }

           /**
            * 计算正方形的周长
            * @param length 正方形的边长
            * @return 返回正方形的周长
            */
           public int square(int length) {
               return length*4;
          }
        }

        class Rectangle extends Shape {
           /**
            * 长方形的长
            */
           private int length;
           /**
            * 长方形的宽
            */
           private int width;

           public Rectangle() {
          }

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

           public int getLength() {
               return length;
          }

           public void setLength(int length) {
               this.length = length;
          }

           public int getWidth() {
               return width;
          }

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

        class Square extends Shape {
           /**
            * 正方形的边长
            */
           private int length;

           public Square() {
          }

           public Square(int length) {
               this.length = length;
          }

           public int getLength() {
               return length;
          }

           public void setLength(int length) {
               this.length = length;
          }
        }
        package day06.homework;

        /**
        * copyright(c)2021 YCKJ.ALL rights Reserved
        * <p>
        * 描述:
        *
        * @author tanyi
        * @version 1.0
        * @date
        */
        public class Test {
           public static void main(String[] args) {
               Rectangle rectangle = new Rectangle(2, 6);
               System.out.println("长方形的周长为:"+rectangle.rectangle(rectangle.getLength(),rectangle.getWidth()));
               Square square = new Square(5);
               System.out.println("正方形的周长为:"+square.square(square.getLength()));
          }
        }
      3. Animal.java

        protected say(){ sout("animal say")}

        Cat 继承 Animal,重写say方法

        Dog继承Animal,重写say方法

        package day06.homework;

        /**
        * copyright(c)2021 YCKJ.ALL rights Reserved
        * <p>
        * 描述:
        *
        * @author tanyi
        * @version 1.0
        * @date
        */
        public class Animal {
           protected void say() {
               System.out.println("animal say");
          }

           public static void main(String[] args) {
               Dog dog = new Dog();
               dog.say();
               Cat cat = new Cat();
               cat.say();
          }
        }

        class Cat extends Animal {
           @Override
           protected void say() {
               System.out.println("Cat say");
          }
        }

        class Dog extends Animal {
           @Override
           protected void say() {
               System.out.println("Dog say");
          }
        }


    软件下载提取码:qwer
  • 相关阅读:
    [SUCTF 2019]EasySQL 1【BUUCFT】【SQL注入】
    [HCTF 2018]WarmUp 1【BUUCFT】【代码审计】
    [强网杯 2019]随便注 1 【BUUCFT】【SQL注入】
    网站如何做好防护
    【单片机】换行、回车
    【Win32】VC6 Visual C/C++ 6.0 修改程序图标
    【Win32】通过多线程自动关闭对话框的方法
    微服务demo
    Mac安装redis
    python---rsa加密根据指数和模生成加密参数模板
  • 原文地址:https://www.cnblogs.com/ty0910/p/14442101.html
Copyright © 2011-2022 走看看