第十周作业

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

package test;
public class ccc1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x[]= {1,3,5,6,7,2};
paixu(x);
for(int i=0;i<x.length;i++) {
System.out.println(x[i]);
}
}

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 test;

 

public class word {

public static int jc(int a) {

int sum=1;

for(int i=1;i<=a;i++) {

sum*=i;

}

return sum;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

int sum1=jc(5);

System.out.println(sum1);

}

 

}

 

 

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

package test;

import java.util.Scanner;

public class word {

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("是平年");

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

year();

}

 

}

 

 

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

package test;

import java.util.Scanner;

public class word {

public double getArea(double a) {

return 3.14*a;

}

public int getArea(int a,int b) {

return a*b;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

word d1=new word();

 

System.out.println(d1.getArea(2));

System.out.println(d1.getArea(2,3));

}

}

 

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

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

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

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

 

package test;

 

public class ccc {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

ccc1 t=new ccc1('红',1);

t.w();

ccc1 t1=new ccc1();

t1.color='白';

t1.cpu=2;

t1.w();

}

 

}

 

 

package test;

public class ccc1 {

ccc1(){

 

}

ccc1(char color,int cpu){

this.color=color;

this.cpu=cpu;

}

char color;

int cpu;

public void w() {

System.out.println("颜色:"+color);

System.out.println("型号:"+cpu);

}

}

posted @ 2023-05-21 16:03  邹昇林  阅读(3)  评论(0编辑  收藏  举报