Java第七次作业

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

 

package zuoye;

public class Fa {

public int[] mopao(int array[]) {
for(int i=0;i<array.length;i++) {
for(int j=0;j<array.length-i-1;j++)
{
int temp=0;
if(array[j]>array[j+1]) {
temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
}
}
}
return array;
}

}

package zuoye;

public class AA {

public static void main(String[] args) {
// TODO Auto-generated method stub


Fa fi=new Fa();
int array[]=new int[] {7,6,5,4,3,2,1};
for(int i:array)
{
System.out.print(i);
}
System.out.println();
array=fi.mopao(array);
for(int i:array)
{
System.out.print(i);
}
System.out.println();

}

}

 

 

 

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

package zuoye;

public class AA {

public static void main(String[] args) {
// TODO Auto-generated method stub

Fa fi=new Fa();
System.out.println("5的阶乘为:"+fi.jc(5));
}

 


}

 

package zuoye;

public class Fa {


public int jc(int a) {
if(a==1) {
return 1;
}
return a*jc(a-1);

}
}

 

 

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

 

package zuoye;

public class AA {

public static void main(String[] args) {
// TODO Auto-generated method stub
Fa fi=new Fa();
System.out.println("2023是:"+fi.RN(2023));

}


}

 

package zuoye;

public class Fa {


public String RN(int a) {
if(a%400==0||a%100!=0&&a%4==0) {
return "闰年";
}
return "平年";

}
}

 

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

package zuoye;

public class AA {

public static void main(String[] args) {
// TODO Auto-generated method stub
Fa fi=new Fa ();

System.out.println("圆:"+fi.getArea(6));
System.out.println("矩阵:"+fi.getArea(3,2));
}


}

 

package zuoye;

public class Fa {


public double getArea(int a) {
//园
return (a*a*3.14);
}
public int getArea(int a,int b) {
//矩阵
return a*b;
}
}

 

 

 

 

 

.

 

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

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

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

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

 

ackage zuoye;

public class AA {

public static void main(String[] args) {
// TODO Auto-generated method stub
Fa test_1=new Fa ('蓝', 15);
test_1.prt();

Fa test_2=new Fa();
test_2.color='黄';
test_2.cpu=20;
test_2.prt();
}


}

 

package zuoye;

public class Fa {


Fa(){

}

Fa(char color,int cpu){
this.color=color;
this.cpu=cpu;
}

char color;
int cpu;

public void prt() {
System.out.println("颜色:"+color);
System.out.println("型号:"+cpu);

}
}

 

 

posted @ 2023-06-23 23:56  MENGYINGCAI  阅读(16)  评论(0)    收藏  举报