java第八次作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
import java.util.Scanner;
public class test {
public static void menthod() {
Scanner input = new Scanner(System.in);
System.out.println("请输入5位数组");
int x[] = new int[5];
for (int i = 0; i < x.length; i++) {
x[i] = input.nextInt();
}
for (int i = 0; i < x.length - 1; i++) {
for (int j = 0; j < x.length - 1; j++) {
if (x[j] > x[j + 1]) {
int t = x[j];
x[j] = x[j + 1];
x[j + 1] = t;
}
}
}
for (int i : x) {
System.out.println(i);
}
}
public static void main(String[] args) {
menthod();
}
}

2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
public class test {
public static int j(int n) {
int sum = 1;
for (int i = 1; i <= n; i++) {
sum = sum * i;
}
return sum;
}
public static void main(String[] args) {
System.out.println("阶乘为" + j(6));
}
}

1. 编写一个方法,判断该年份是平年还是闰年。[必做题]
import java.util.Scanner;
public class test {
public static void menthod() {
Scanner input = new Scanner(System.in);
System.out.println("输入年份");
int year = input.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("是闰年");
} else {
System.out.println("是平年");
}
}
public static void main(String[] args) {
menthod();
}
}

2.使用方法重载,定义一个可以求出圆形面积和矩形面积的方法getArea
package xingqidemo01;
public class mianj {
public double getArea(double r) {
double mj=0;
mj=3.14*r*r;
return mj;
}
public double getArea(double a,double b) {
return a*b;
}
}
package xingqidemo01;
public class mianjText {
public static void main(String[] args) {
// TODO Auto-generated method stub
mianj m=new mianj();
System.out.println("圆的面积为:"+m.getArea(10));
System.out.println("矩形的面积为:"+m.getArea(5.5, 10));
}
}

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


浙公网安备 33010602011771号