Java第七次作业

1.编写一个方法,实现冒泡排序(由小到大),并调用该方法

import java.util.Scanner;

 

public class wxl {

    public static void main(String[] args) {

     Scanner input = new Scanner(System.in);

     System.out.println("输入5个数:");

     int a[] = new int[5];

     int b;

     for (int i = 0; i < a.length; i++) {

         a[i] = input.nextInt();

     }

     for (int i = 0; i < a.length - 1; i++) {

         for (int j = i+1; j < a.length; j++) {

             if (a[i] > a[j]) {

                 b = a[i];

                 a[i] = a[j];

                 a[j] = b;

             }

         }

     }

     for (int i = 0; i < a.length; i++) {

         System.out.print(a[i]);

         System.out.print("  ");

     }

     System.out.println();

 }

 

 }

 

 

 

2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]

 

public class wxl {

    public static int jc(int a){

        int sum=1;

        for(int i=1;i<=a;i++){

            sum*=i;

        }

        return sum;

    }

    public static void main(String[] args){

        System.out.println(jc(5));

    }

}

 

 

3.编写一个方法,判断该年份是平年还是闰年。[必做题]

import java.util.Scanner;

 

public class wxl {

    public static void prn(){

        Scanner input = new Scanner(System.in);

        System.out.println("请输入年份");

        int year = input.nextInt();

        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {

            System.out.println("是闰年");

          } else {

          System.out.println("是平年");

        }

      }

 

    public static void main(String[] args) {

        prn();

    }

 }

 

 

4.使用方法重载,定义一个可以求出圆形面积和矩形面积的方法getArea

 

public class wxl {

         //求矩形面积

            public static double Area(double width, double height) {

                return width * height;

            }

 

            //求圆形面积

            public static double Area(double radius) {

                return radius * Math.PI;

            }

 

            public static void main(String[] args) {

                System.out.println(Area(6,7));

                System.out.println(Area(4));

 

            }

        }

 

 

5.定义一个笔记本类,该类有颜色(char) 和cpu型号(int) 两个属性。[必做题]

(1)无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;

(2) 输出笔记本信息的方法

(3) 然后编写一个测试类,测试笔记本类的各个方法。

 

public class wxl {

         //求矩形面积

 

          public static void main(String[] args) {

              Computer c1 = new Computer();

              c1.showComputer();

              Computer c2 = new Computer('黑', 64);

              c2.showComputer();

          }

 

        }

 

        class Computer {

          private char color;

          private int cpuNum;

 

          public Computer() {

 

          }

 

          public Computer(char color, int cpuNum) {

              this.color = color;

              this.cpuNum = cpuNum;

          }

 

          public char getColor() {

              return color;

          }

 

          public void setColor(char color) {

              this.color = color;

          }

 

          public int getCpuNum() {

              return cpuNum;

          }

 

          public void setCpuNum(int cpuNum) {

              this.cpuNum = cpuNum;

          }

 

          public void showComputer() {

              System.out.println("笔记本的颜色:" + getColor());

              System.out.println("笔记本的CPU型号:" + getCpuNum());

 

          }

        }

 

 

posted @ 2023-05-12 14:10  未步乘风  阅读(22)  评论(0)    收藏  举报