Java第七次作业

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

 1 package gfttg;
 2 
 3  
 4 
 5 public class Hff {
 6 
 7  
 8 
 9 public static void main(String[] args) {
10 
11 // TODO Auto-generated method stub
12 
13 int []a={134,44,200,90};
14 
15         cc(a);
16 
17         for (int i = 0; i < a.length; i++) {
18 
19             System.out.println(a[i]);
20 
21         }
22 
23 }
24 
25     public static void cc(int[]b){
26 
27         for (int i = 0; i < b.length - 1; i++) {
28 
29             for (int j = 0; j < b.length - 1 - i; j++) {
30 
31                 if (b[j] > b[j + 1]) {
32 
33                     int temp = b[j];
34 
35                     b[j] = b[j + 1];
36 
37                     b[j + 1] = temp;
38 
39                 }
40 
41             }
42 
43         }
44 
45  
46 
47 }
48 
49  
50 
51 }

 

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

 1 package gfttg;
 2 
 3  
 4 
 5 public class Hff {
 6 
 7  
 8 
 9 public static void main(String[] args) {
10 
11 // TODO Auto-generated method stub
12 
13 int sum1=fn(5);
14 
15         System.out.println(sum1);
16 
17     }
18 
19     public static int fn(int n){
20 
21         int sum=1;
22 
23         for (int i =1; i <=n; i++) {
24 
25             sum*=i;
26 
27         }
28 
29         return sum;
30 
31  
32 
33 }
34 
35  
36 
37 }

 

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

 1 package gfttg;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Hff {
 6 
 7  
 8 
 9 public static void main(String[] args) {
10 
11 // TODO Auto-generated method stub
12 
13 nian();
14 
15 }
16 
17 public static void nian() {
18 
19         Scanner input = new Scanner(System.in);
20 
21         System.out.println("输入年份");
22 
23         int year = input.nextInt();
24 
25         if (year % 4 == 0 && year % 100 != 100 || year % 400 == 0) {
26 
27             System.out.println("该年份为闰年");
28 
29         } else {
30 
31             System.out.println("该年份是平年");
32 
33         }
34 
35  
36 
37 }
38 
39 }
40 
41  

 

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

 

 1 package aaa;
 2 
 3  
 4 
 5 public class Text1 {
 6 
 7  
 8 
 9 public static void main(String[] args) {
10 
11 // TODO Auto-generated method stub
12 
13 Text1 t=new Text1();
14 
15 System.out.println(t.getArea(3));
16 
17 System.out.println(t.getArea(3,4));
18 
19 }
20 
21 public double getArea(int a) {
22 
23 return 3.14*a;
24 
25 }
26 
27 public int getArea(int a,int b) {
28 
29 return a*b;
30 
31  
32 
33 }
34 
35 }
36 
37  

 

 

 

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

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

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

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

 

 1 package test;
 2 
 3  
 4 
 5 public class ccc {
 6 
 7  
 8 
 9 public static void main(String[] args) {
10 
11 // TODO Auto-generated method stub
12 
13 ccc1 t=new ccc1('红',1);
14 
15 t.w();
16 
17 ccc1 t1=new ccc1();
18 
19 t1.color='白';
20 
21 t1.cpu=2;
22 
23 t1.w();
24 
25 }
26 
27  
28 
29 }
30 
31  
32 
33  
34 
35 package test;
36 
37 public class ccc1 {
38 
39 ccc1(){
40 
41  
42 
43 }
44 
45 ccc1(char color,int cpu){
46 
47 this.color=color;
48 
49 this.cpu=cpu;
50 
51 }
52 
53 char color;
54 
55 int cpu;
56 
57 public void w() {
58 
59 System.out.println("颜色:"+color);
60 
61 System.out.println("型号:"+cpu);
62 
63 }
64 
65 }

 

 

posted @ 2023-05-21 16:04  coldlane  阅读(15)  评论(0编辑  收藏  举报