Java第七次作业

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

package aaa;
 
public class Text1 {
 
​public static void main(String[] args) {
​​// TODO Auto-generated method stub
​​int[] x= {2,4,3,5,1};
​​mp(x);
​​for(int i:x) {
​​​System.out.println(i);
​​}
 
​}
​public static void mp(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 x=a[j];
​​​​​a[j]=a[j+1];
​​​​​a[j+1]=x;
​​​​}
​​​}
​​}
​
​​
​}
 
}

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

package aaa;
 
public class Text1 {
 
​public static void main(String[] args) {
​​// TODO Auto-generated method stub
​​Text1 t=new Text1();
​​
​​System.out.println(t.jc(5));
 
​}
​public int jc(int a) {
​​if(a==1)
​​​return 1;
​​else
​​​return a*jc(a-1);
​​
​
​​
​}
 
}

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

package aaa;
 
public class Text1 {
 
​public static void main(String[] args) {
​​// TODO Auto-generated method stub
​​Text1 t=new Text1();
​​System.out.println(t.nf(2000));
​​
​​
​}
​public String nf(int a) {
​​if(a%4==0||a%100==0&&a%400==0)
​​​return "闰年";
​​else
​​​return "平年";
​​​
​
​​
​
​​
​}
 
}

 

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

package aaa;
 
public class Text1 {
 
​public static void main(String[] args) {
​​// TODO Auto-generated method stub
​​Text1 t=new Text1();
​​System.out.println(t.getArea(3));
​​System.out.println(t.getArea(3,4));
​​
​}
​public double getArea(int a) {
​​return 3.14*a;
​​
​​
​}
​public int getArea(int a,int b) {
​​return a*b;
​​
​}
 
}

 

 

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

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

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

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

package book;
 
public class book {
​char ys;
​int xh;
​public book(){
​​
​}
​public book(char ys,int xh) {
​​this.ys=ys;
​​this.xh=xh;
​​show(ys,xh);
​}
​public void show(char ys,int xh) {
​​this.ys=ys;
​​this.xh=xh;
​​System.out.println("颜色是"+ys+"型号是"+xh);
​}
​
​public static void main(String[] args) {
​​book b=new book();
​​b.show('蓝', 123);
​}
 
}

 

posted @ 2023-05-19 12:28  TLWLS  阅读(10)  评论(0编辑  收藏  举报