java第七次作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
package www; public class Wyyy { public static void main(String[] args) { // TODO Auto-generated method stub int a[]={5,4,3,2,1}; sort(a); for(int i:a) System.out.println(i); } public static void sort(int a[]) { for(int i=0;i<a.length-1;i++) { for(int j=0;j<a.length-i-1;j++) { if(a[j]>a[j+1]) { int m=a[j]; a[j]=a[j+1]; a[j+1]=m; } } } } }

2. 编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package www; import java.util.Scanner; public class Wyyy { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("输入一个数"); int a=input.nextInt(); System.out.println("该数的阶乘为:"+jc(a)); } public static int jc(int a) { int sum=1; for(int i=1;i<=a;i++) { sum*=i; } return sum; } }

3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package www; public class Wyyy { public static void main(String[] args) { // TODO Auto-generated method stub int a=2020; years(a); } public static void years(int a) { if(a%4==0||a%100!=0&&a%400==0) System.out.println("闰年"); else System.out.println("平年"); } }

4. 使用方法重载,定义一个可以求出圆形面积和矩形面积的方法getArea
package www; import java.util.Scanner; public class Wyyy { public static double getArea(double r){ return 3.14*r*r; } public static double getArea(double a,double b) { return a*b; } public static void main(String[] args) { System.out.println(getArea(2)); System.out.println(getArea(1,2)); } }

5.定义一个笔记本类,该类有颜色(char) 和cpu型号(int) 两个属性。[必做题]
(1)无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
(2) 输出笔记本信息的方法
(3) 然后编写一个测试类,测试笔记本类的各个方法。
package Wyyy; public class ben { char color; int cpu; public char getColor() { return color; } public void setColor(char color) { this.color = color; } public int getCpu() { return cpu; } public void setCpu(int cpu) { this.cpu = cpu; } public ben() { super(); } public ben(char color) { super(); this.color = color; } public ben(int cpu) { super(); this.cpu = cpu; } public ben(char color, int cpu) { super(); this.color = color; this.cpu = cpu; } public void show() { System.out.println("颜色为:"+color+" "+"型号为:"+cpu); } } package Wyyy; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ben c1=new ben(); ben c2=new ben(); c1.color='紫'; c1.cpu=5; c2.color='蓝'; c2.cpu=7; c1.show(); c2.show(); } }


浙公网安备 33010602011771号