java第七次作业

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

package w;

import java.util.Arrays;

public class pxTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x[]= {1,3,5,6,7,2};
        px a = new px();
        a.paixu(x);
        System.out.println(Arrays.toString(x));

}
}
package w;

public class px {
    public static void paixu(int a[]) {
        for(int i=0;i<a.length-1;i++) {
        for(int j=0;j<a.length-1-i;j++) {
        if(a[j]>a[j+1]) {
        int temp=a[j];
        a[j]=a[j+1];
        a[j+1]=temp;
        }
        }
        }
}
}

 


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

package w;

public class jctest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jc q=new jc();
        int sum1=q.jc(4);
        System.out.println(sum1);
    }

}
package w;

public class jc {
            public static  int jc(int a) {
                int sum=1;
                for (int i = 1; i <=a; i++) {
                    sum*=i;
                }
                return sum;
            }
}

 

 

3.编写一个方法,判断该年份是平年还是闰年。

 

package w;

public class rnTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            rn aa=new rn();
            aa.year();
            
    }

}
package w;

import java.util.Scanner;

public class rn {
    public static void year() {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入年份");
        int a=input.nextInt();
        if(a%4==0&&a%100!=0||a%400==0) {
            System.out.println("该年是闰年");
        }else {
            System.out.println("该年是平年");
        }
    }

}

 

 


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

package w;

public class chongzaiTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        chongzai mianji=new chongzai();
        System.out.println(mianji.getArea(4));
        System.out.println(mianji.getArea(3, 2));    
    }

}
package w;

public class chongzai {
    public double getArea(double a) {
        return 3.14*a*a;
    }
    public int getArea(int a,int b) {
        return a*b;
        
    }
}

 


5.
定义一个笔记本类,该类有颜色(char) 和cpu型号(int) 两个属性。[
(1)无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
(2) 输出笔记本信息的方法
(3) 然后编写一个测试类,测试笔记本类的各个方法。

package w;

public class benTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            ben c1=new ben('红',2);
            ben c2=new ben();
            c2.color='蓝';
            c2.cpu=1;
            c1.show();
            c2.show();
                    
    }

}
package w;

public class ben {
        char color;
        int cpu;
        
        public ben() {
            super();
        }

        public char getColor() {
            return color;
        }
        
        public int getCpu() {
            return cpu;
        }

        public ben(char color, int cpu) {
            super();
            this.color = color;
            this.cpu = cpu;
        }
        public void show() {
            System.out.println("颜色为"+color+"    "+"型号为"+cpu);
        
        }
        
}

 

posted @ 2023-05-30 16:27  张云月  阅读(5)  评论(0编辑  收藏  举报