Day09作业----------------------Java面向对象,JavaObject01-2

作业:

1. 定义一个表示学生信息的类Student,要求如下:
 (1)类Student的成员变量:
       id表示学号;name表示姓名;gender表示性别;age表示年龄;java表示Java课程成绩。
   (2) 在定义一个print()方法,输出student对象的 name,gender,age,java的值(即输出学生信息)

   (3)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。

  提示: 数组既可以存储基本数据类型的值,也可以存储引用数据类型的值
  因此,5个Student对象可以放在一个Student数组中
    Student[] students = new Student[5];
    students[1] = student对象的引用

  求最大值,最小值,平均值,都可以通过循环遍历 students数组来完成

2. 写一个数组的工具类ArrayTool, 要求提供遍历,求最大值,最小值,逆置数组元素,查表(在数组中查找指定元素,若不存在,待查找元素返回-1,若存在返回元素在数组中首次出现的位置),找元素在int类型数组(int[])中最后出现的索引等操作。

  提示:所谓工具类,就是一个类中定义的都是静态方法,这样的类称为工具类。工具类中定义的静态方法 就是工具方法,所谓工具方法,简单来说,就是该方法实现了一些公用的功能,为了方便使用,定义为工具方法
  比如我们之前用过的Arrays.toString(数组),就是一个工具方法,工具方法处理的都是方法参数传递的数据。
  比如定义一个查找数组最大值的方法,在工具类中就可以这样定义
  

1 class ArrayTool{
2     /*
3       查找数组中的最大值
4     */
5     public static int max(int[] a) {}
6 }

答案:

1.

参考代码:

 1 public class Work1 {
 2 
 3     public static void main(String[] args) {
 4 
 5         // 创建Student对象数组,并给数组赋值
 6         Student[] students = new Student[5];
 7         students[0] = new Student(1, "张三", "男", 18, 80);
 8         students[1] = new Student(1, "李四", "女", 20, 90);
 9         students[2] = new Student(1, "王五", "男", 180, 100);
10         students[3] = new Student(1, "赵六", "女", 30, 70);
11         students[4] = new Student(1, "孙七", "男", 8, 80);
12 
13         // 平均值
14         double average = average(students);
15         // 最大值
16         int max = max(students);
17         // 最小值
18         int min = min(students);
19     }
20 
21     /*
22      * 求5个学生java成绩的平均值
23      */
24     public static double average(Student[] students) {
25         double sum = 0;
26         for (int i = 0; i < students.length; i++) {
27             sum += students[i].java;
28         }
29 
30         return sum / students.length;
31     }
32 
33     /*
34      * 求5个学生中,java成绩的最大值
35      */
36     public static int max(Student[] students) {
37         int max = students[0].java;
38         for (int i = 1; i < students.length; i++) {
39             if (students[i].java > max) {
40                 max = students[i].java;
41             }
42         }
43         return max;
44     }
45 
46     /*
47      * 求5个学生中java成绩的最小值
48      */
49     public static int min(Student[] students) {
50         int min = students[0].java;
51         for (int i = 1; i < students.length; i++) {
52             if (students[i].java < min) {
53                 min = students[i].java;
54             }
55         }
56         return min;
57     }
58 }

自己写的代码:

 1 package com.day009;
 2 /*
 3  * 1. 定义一个表示学生信息的类Student,要求如下:
 4  (1)类Student的成员变量:
 5 id表示学号;name表示姓名;gender表示性别;age表示年龄;java表示Java课程成绩。
 6    (2) 在定义一个print()方法,输出student对象的 name,gender,age,java的值(即输出学生信息)
 7     
 8   (3)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,
 9 以及计算并输出他们Java语言成绩的最大值和最小值。
10 
11        提示: 数组既可以存储基本数据类型的值,也可以存储引用数据类型的值
12               因此,5个Student对象可以放在一个Student数组中
13               Student[] students = new Student[5];
14               students[1] = student对象的引用
15 
16              //求最大值,最小值,平均值,都可以通过循环遍历 students数组来完成
17  */
18 public class Demo1 {
19     public static void main(String[] args) {
20         Student[] stu = new Student[5];
21         stu[0] = new Student(1,"赵一","男", 18, 88);
22         stu[1] = new Student(2,"赵二","男", 17, 97);
23         stu[2] = new Student(3,"赵三","女", 16, 89);
24         stu[3] = new Student(4,"赵四","男", 15, 78);
25         stu[4] = new Student(5,"赵五","女", 14, 84);
26         
27         //定义sum为学生Java成绩的总分
28         double sum = 0;
29         double max = stu[0].grade;
30         double min = stu[0].grade;
31         
32         for(int i = 0; i < stu.length; i++) {
33             stu[i].print();
34             sum += stu[i].grade;
35         }
36         for(int i = 1; i < stu.length; i++) {
37             if(stu[i].grade > max) {
38                 max = stu[i].grade;
39             }
40             if(stu[i].grade < min) {
41                 min = stu[i].grade;
42             }
43         }
44         
45         //几位学生的Java成绩平均分
46         double averageScore = sum/(stu.length);
47         System.out.println(stu.length +"位学生的Java成绩平均分为:" + averageScore);
48         System.out.println("Java语言成绩的最大值为:"+max);
49         System.out.println("Java语言成绩的最小值为:"+min);
50     }
51     
52     
53 }
54 
55 class Student{
56     //(1)定义成员变量
57     //id表示学号;name表示姓名;gender表示性别;age表示年龄;java表示Java课程成绩
58     int id;
59     String name;
60     String gender;
61     int age;
62     double grade;
63     
64     //(2)定义成员方法
65     public void print() {
66         System.out.println(this.id +"--"+ this.name +"--"+this.gender+"--"+this.age+"--"+this.grade);
67     }
68     //定义带5个参数的构造方法
69     public Student(int id, String name, String gender, int age, double grade) {
70         this.id = id;
71         this.name = name;
72         this.gender = gender;
73         this.age = age;
74         this.grade = grade;
75     }
76     
77 }

运行结果:

2.

参考代码:

 1 public class Work2 {
 2 
 3     public static void main(String[] args) {
 4 
 5         int[] a = { 1, 2, 3, 4, 5, 1 };
 6         // 使用工具方法
 7         int max = ArrayTool.max(a);
 8         int min = ArrayTool.min(a);
 9         int firstIndex = ArrayTool.findFirst(a, 2);
10         int lastIndex = ArrayTool.findFirst(a, 1);
11         ArrayTool.reverse(a);
12 
13     }
14 
15 }
16 
17 class ArrayTool {
18 
19     public static int max(int[] a) {
20         int max = a[0];
21         for (int i = 0; i < a.length; i++) {
22             if (a[i] > max) {
23                 max = a[i];
24             }
25         }
26         return max;
27     }
28 
29     public static int min(int[] a) {
30         int min = a[0];
31         for (int i = 0; i < a.length; i++) {
32             if (a[i] < min) {
33                 min = a[i];
34             }
35         }
36         return min;
37     }
38 
39     /*
40      * 逆置数组元素
41      */
42     public static void reverse(int[] a) {
43         int tmp;
44         for (int i = 0; i < a.length / 2; i++) {
45             tmp = a[i];
46             a[i] = a[a.length - i - 1];
47             a[a.length - i - 1] = tmp;
48         }
49     }
50 
51     /*
52      * 查表,即在数组中查找指定元素value,若找到,返回该值在数组中首次出现的位置,否则返回-1
53      */
54     public static int findFirst(int[] a, int value) {
55         for (int i = 0; i < a.length; i++) {
56             if (a[i] == value) {
57                 // 找到直接返回该元素的位置
58                 return i;
59             }
60         }
61 
62         return -1;
63     }
64 
65     /*
66      * 查找指定元素value在数组中最后出现的位置,未找到返回-1
67      */
68     public static int findLast(int[] a, int value) {
69 
70         int index = -1;
71         for (int i = 0; i < a.length; i++) {
72             if (a[i] == value) {
73                 // 找到直接返回该元素的位置
74                 index = i;
75             }
76         }
77         return index;
78     }
79 }

自己写的:

 1 package com.day009;
 2 
 3 import java.util.Arrays;
 4 
 5 /*
 6  * 2. 写一个数组的工具类ArrayTool, 要求提供
 7    遍历,求最大值,最小值,逆置数组元素,查表(在数组中查找指定元素,若不存在,待查找元素返回-1,若存在返回元素在数组中首次出现的位置),
 8    找元素在int类型数组(int[])中最后出现的索引等操作。
 9    
10    提示:所谓工具类,就是一个类中定义的都是静态方法,这样的类称为工具类。工具类中定义的静态方法  就是工具方法,
11    所谓工具方法,简单来说,就是该方法实现了一些公用的功能,为了方便使用,定义为工具方法
12 比如我们之前用过的Arrays.toString(数组),就是一个工具方法,工具方法处理的都是方法参数传递的数据。
13          比如定义一个查找数组最大值的方法,在工具类中就可以这样定义
14 */
15 public class Demo2 {
16     public static void main(String[] args) {
17         int[] arr = {1,5,2,7,3,9,4,5,12,2};
18         int num = 2;//为要查找的数组中的元素
19         //查找数组中的最大值
20         System.out.println("数组中的最大值为:"+ArrayTool.max(arr));
21         //查找数组中的最小值
22         System.out.println("数组中的最小值为:"+ArrayTool.min(arr));
23         //逆置数组元素
24         //arr1用来存放逆序后的数组
25         int[] arr1 = ArrayTool.reverse(arr);
26         System.out.println("逆置后的数组:"+ Arrays.toString(arr1));
27         //查表(在数组中查找指定元素,若不存在,待查找元素返回-1,若存在返回元素在数组中首次出现的位置)
28         //用place1来记录数组中查找num的返回位置,若为-1,则表示数组中不存在num元素
29         int place1 = ArrayTool.searchFirst(arr, num);
30         if(place1 == -1) {
31             System.out.println("数组中不存在"+num+"元素");
32         }else {
33             System.out.println(num + "在数组中首次出现的位置为"+ place1);
34         }
35         
36         //找元素在int类型数组(int[])中最后出现的索引
37         int place2 = ArrayTool.searchEnd(arr, num);
38         if(place2 == -1) {
39             System.out.println("数组中不存在"+num+"元素");
40         }else {
41             System.out.println(num + "在数组中首次出现的位置为"+ place2);
42         }
43     }
44     
45 }
46 
47 class ArrayTool{
48     
49     //查找数组中的最大值
50     public static int max(int[] a) {
51         int max = a[0];
52         for(int i = 1; i < a.length; i++) {
53             if(a[i]>max) {
54                 max = a[i];
55             }
56         }
57         return max;
58     }
59     //查找数组中的最小值
60     public static int min(int[] a) {
61         int min = a[0];
62         for(int i = 1; i < a.length; i++) {
63             if(a[i]<min) {
64                 min = a[i];
65             }
66         }
67         return min;
68     }
69     //逆置数组元素
70     public static int[] reverse(int[] a) {
71         int len = a.length;//数组a的长度
72         int[] b = new int[len];
73         for(int i = 0; i < len/2; i++) {
74             b[i] = a[i];
75             b[len-1-i] = a[len-1-i];
76         }
77         return b;
78     }
79     //查表(在数组中查找指定元素,若不存在,待查找元素返回-1,若存在返回元素在数组中首次出现的位置)
80     public static int searchFirst(int[] a, int num) {
81         for(int i = 0; i < a.length; i++) {
82             if(a[i] == num) {
83                 return i;
84             }
85         }
86         return -1;
87     }
88     //找元素在int类型数组(int[])中最后出现的索引
89     public static int searchEnd(int[] a, int num) {
90         //从后往前遍历,即最先匹配的数据就是数组中最后出现的索引
91         for(int i = a.length -1; i >= 0; i--) {
92             if(a[i] == num) {
93                 return i;
94             }
95         }
96         return -1;
97     }
98     
99 }

运行结果:

原文地址:https://www.cnblogs.com/dust2017/p/12731239.html