- 编写一个方法,实现冒泡排序(由小到大),并调用该方法
package nb;
public class gra {
public static void main(String[] args) {
// TODO Auto-generated method stub
fa s=new fa();
int a[]=new int[] {5,4,3,2,1};
for (int i : a)
System.out.print(i+" ");
a=s.maopao(a);
System.out.println();
for (int i : a)
System.out.print(i+" ");
}
}
package nb;
public class fa {
public int[] maopao(int a[]) {
for(int j=0;j<a.length;j++) {
for(int i=0;i<a.length-j-1;i++) {
int s=0;
if(a[i]>a[i+1]) {
s=a[i];
a[i]=a[i+1];
a[i+1]=s;
}
}
}
return a;
}
}
- 编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package dfgssdfgrh;
public class gtbh {
public static void main(String[] args) {
// TODO Auto-generated method stub
fdff s=new fdff();
System.out.println(s.jiechen(5));
}
}
package dfgssdfgrh;
public class fdff {
public int jiechen(int a) {
int v=1;
for(int i=1;i<=a;i++)
v=i*v;
return v;
}
}
- 编写一个方法,判断该年份是平年还是闰年。[必做题]
package dfgssdfgrh;
public class gtbh {
public static void main(String[] args) {
// TODO Auto-generated method stub
fdff s=new fdff();
System.out.println(s.year(2023));
}
}
package dfgssdfgrh;
public class fdff {
public String year(int a) {
if(a%4==0||a%100!=0&&a%400==0)
return "闰年";
else
return "平年";
}
}
- 使用方法重载,定义一个可以求出圆形面积和矩形面积的方法getArea
package dfgssdfgrh;
public class gtbh {
public static void main(String[] args) {
// TODO Auto-generated method stub
fdff s=new fdff();
System.out.println(s.mian(5));
System.out.println(s.mian(5, 4));
}
}
package dfgssdfgrh;
public class fdff {
public double mian(int a) {
double s;
s=a*a*3.14;
return s;
}
public double mian(int a,int b) {
double s;
s=a*b;
return s;
}
}
5.
定义一个笔记本类,该类有颜色(char) 和cpu型号(int) 两个属性。[必做题]
(1)无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
(2) 输出笔记本信息的方法
(3) 然后编写一个测试类,测试笔记本类的各个方法。
package nb;
public class gra {
public static void main(String[] args) {
// TODO Auto-generated method stub
fa s=new fa('黑',1);
s.pri();
fa s2=new fa();
s2.color='白';
s2.cpu=2;
s2.pri();
}
}
package nb;
public class fa {
fa(){
}
fa(char color,int cpu){
this.color=color;
this.cpu=cpu;
}
char color;
int cpu;
public void pri() {
System.out.println("颜色:"+color);
System.out.println("型号:"+cpu);
}
}