Java 对象数组题目 + 改进(封装方法)
1 /**
2 *
3 * @Description
4 * @author Bytezero·zhenglei! Email:420498246@qq.com
5 * @version
6 * @date 2021年9月12日上午9:06:47
7 * @
8 * 对象数组题目
9 * 定义类:Student,包含三个属性:学号number(int),年级state(int),
10 * 成绩score(int)
11 * 创建20个学生对象,学号为1-20,年级和成绩都有随机数确定
12 * 问题一:打印出3年级(state值为3)的学生信息
13 * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
14 *
15 * 提示:生成随机数 Math.random(),返回值double
16 * 四舍五入取整数:Math.round(double d),返回值类型long
17 */
18 public class StudentTest
19 {
20 public static void main(String[] args)
21 {
22 //声明Student类型的数组
23 Student[] stus = new Student[20];
24 for(int i =0; i <stus.length;i++)
25 {
26 //给数组元素赋值
27 stus[i] = new Student();
28 //给student对象的属性赋值
29 stus[i].number= (i+1);
30 //要求年级[1-6]
31 stus[i].state =(int)(Math.random()*(6-1+1)+1);
32 //成绩 [0-100]
33 stus[i].score = (int)(Math.random()*(100-0+1)+0);
34 }
35
36 //遍历学生数组
37 for(int i =0; i <stus.length; i++)
38 {
39 //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
40
41 System.out.println(stus[i].info());
42 }
43
44 System.out.println("******************************************");
45 //问题一:打印出3年级(state值为3)的学生信息
46 for(int i = 0; i <stus.length;i++)
47 {
48 if(stus[i].state==3)
49 {
50 System.out.println(stus[i].info());
51 }
52
53 }
54
55 //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
56 System.out.println("******************************************");
57 System.out.println( "问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息");
58
59 for(int i =0;i <stus.length-1;i++)
60 {
61 for(int j =0;j<stus.length-1-i;j++)
62 {
63 if(stus[j].score>stus[j+1].score)
64 {
65 //交换的是数组的元素:Student对象!!!
66 Student temp = stus[j];
67 stus[j] = stus[j+1];
68 stus[j+1] = temp;
69 }
70 }
71 }
72
73 //遍历学生数组
74 for(int i =0; i <stus.length; i++)
75 {
76 //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
77
78 System.out.println(stus[i].info());
79 }
80
81
82 }
83 }
84
85 class Student
86 {
87 int number; //学号
88 int state; //年级
89 int score; //成绩
90
91
92 //显示学生信息的方法
93 public String info()
94 {
95 return"学号:"+number+"\t年级:"+state+"\t成绩:"+score;
96 }
97
98 }
99
100
101
102 /****************************改进**************************/
103
104 /**
105 *
106 * @Description
107 * @author Bytezero·zhenglei! Email:420498246@qq.com
108 * @version
109 * @date 2021年9月12日上午9:06:47
110 * @
111 * 对象数组题目
112 * 定义类:Student,包含三个属性:学号number(int),年级state(int),
113 * 成绩score(int)
114 * 创建20个学生对象,学号为1-20,年级和成绩都有随机数确定
115 * 问题一:打印出3年级(state值为3)的学生信息
116 * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
117 *
118 * 提示:生成随机数 Math.random(),返回值double
119 * 四舍五入取整数:Math.round(double d),返回值类型long
120 */
121
122 /*********** 改进 **********/
123 /*将操作数组的功能封装到方法中*/
124
125
126
127 public class StudentTest2
128 {
129 public static void main(String[] args)
130 {
131 //声明Student类型的数组
132 Student2[] stus = new Student2[20];
133 for(int i =0; i <stus.length;i++)
134 {
135 //给数组元素赋值
136 stus[i] = new Student2();
137 //给student对象的属性赋值
138 stus[i].number= (i+1);
139 //要求年级[1-6]
140 stus[i].state =(int)(Math.random()*(6-1+1)+1);
141 //成绩 [0-100]
142 stus[i].score = (int)(Math.random()*(100-0+1)+0);
143 }
144
145 StudentTest2 test = new StudentTest2();
146 //遍历学生数组
147 test.print(stus);
148
149 //问题一:打印出3年级(state值为3)的学生信息
150 System.out.println("******************************************");
151 System.out.println("问题一:打印出3年级(state值为3)的学生信息");
152 test.serchState(stus, 3);
153
154
155 //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
156 System.out.println("******************************************");
157 System.out.println( "问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息");
158 test.sort(stus);
159 //遍历学生数组
160 test.print(stus);
161
162 }
163
164 /**
165 *
166 * @Description 遍历Student2[]数组的操作
167 * @author Bytezero·zhenglei!
168 * @date 2021年9月12日上午9:56:45
169 * @param stus
170 *
171 */
172 public void print(Student2[] stus)
173 {
174 //遍历学生数组
175 for(int i =0; i <stus.length; i++)
176 {
177 //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
178
179 System.out.println(stus[i].info());
180 }
181 }
182
183 /**
184 *
185 * @Description 查找Student数组中指定的学生信息
186 * @author Bytezero·zhenglei!
187 * @date 2021年9月12日上午9:52:52
188 * @param stus 要查找的数组
189 * @param state 要找的年级
190 *
191 */
192 public void serchState(Student2[] stus,int state)
193 {
194 //问题一:打印出3年级(state值为3)的学生信息
195 for(int i = 0; i <stus.length;i++)
196 {
197 if(stus[i].state==state)
198 {
199 System.out.println(stus[i].info());
200 }
201
202 }
203 }
204
205 /**
206 *
207 * @Description 给Student2数组排序
208 * @author Bytezero·zhenglei!
209 * @date 2021年9月12日上午9:55:35
210 * @param stus
211 *
212 */
213 public void sort(Student2[] stus)
214 {
215 for(int i =0;i <stus.length-1;i++)
216 {
217 for(int j =0;j<stus.length-1-i;j++)
218 {
219 if(stus[j].score>stus[j+1].score)
220 {
221 //交换的是数组的元素:Student对象!!!
222 Student2 temp = stus[j];
223 stus[j] = stus[j+1];
224 stus[j+1] = temp;
225 }
226 }
227 }
228 }
229
230
231
232
233 }
234
235 class Student2
236 {
237 int number; //学号
238 int state; //年级
239 int score; //成绩
240
241
242 //显示学生信息的方法
243 public String info()
244 {
245 return"学号:"+number+"\t年级:"+state+"\t成绩:"+score;
246 }
247
248 }

Java 对象数组题目 + 改进(封装方法)的更多相关文章
- JSon_零基础_007_将JSon格式的"数组"字符串转换为Java对象"数组"
将JSon格式的"数组"字符串转换为Java对象"数组". 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“n ...
- java对象数组
问题描述: java 对象数组的使用 问题解决: 数组元素可以是任何类型(只要所有元素具有相同的类型) 数组元素可以是基本数据类型 数组元素也可以是类对象,称这样的数组为对象数组.在这种情况下 ...
- 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold&q ...
- java 对象数组
java 对象数组 from zhaocundang@163.com 先 用类声明数组: 再把类的实例赋给数组: package works; import java.util.Scanner; pu ...
- java中数组有没有length()方法?string没有lenght()方法?
java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int[] arr={1,2,3,4,5}; int length=arr.length;//求数组的长度 -- ...
- 【Java面试题】18 java中数组有没有length()方法?string没有lenght()方法?下面这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";
数组没有length()这个方法,有length的属性.String有有length()这个方法. int a[]; a.length;//返回a的长度 String s; s.length();// ...
- java对象数组的概述和使用
1 public class Student 2 { 3 // 成员变量 4 private String name; 5 private int age; 6 7 // 构造方法 8 public ...
- 【Java】数组不能通过toString方法转为字符串
java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的.object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toStrin ...
- SpringMVC接收对象数组参数进行封装
前台代码:注意.contentType : "application/json; charset=utf-8",必须要设置,只有这样SpringMVC才认识这个json数组参数 f ...
- java去除数组重复元素的方法
转载自:https://blog.csdn.net/Solar24/article/details/78672500 import java.util.ArrayList; import java.u ...
随机推荐
- 【JS 逆向百例】XHR 断点调试,Steam 登录逆向
声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 逆向目标 目标:Steam ...
- DevelopTool
目录 01-PostMan常用玩法详解
- 用python进行精细中文分句(基于正则表达式),HarvestText:文本挖掘和预处理工具
1.用python进行精细中文分句(基于正则表达式) 中文分句,乍一看是一个挺简单的工作,一般我们只要找到一个[.!?]这类的典型断句符断开就可以了吗. 对于简单的文本这个做法是已经可行了 ...
- 【4】jupyter notebook快速入门、以及常用快捷键使用
相关文章: [1]Anaconda安装超简洁教程,瞬间学会! [2]Anaconda下:ipython文件的打开方式,Jupyter Notebook中运行.py文件,快速打开ipython文件的方法 ...
- 8.2 C++ 引用与取别名
C/C++语言是一种通用的编程语言,具有高效.灵活和可移植等特点.C语言主要用于系统编程,如操作系统.编译器.数据库等:C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统.图形用户界面 ...
- 开源.NetCore通用工具库Xmtool使用连载 - 散列算法篇
[Github源码] <上一篇>详细介绍了Xmtool工具库中的加解密类库,今天我们继续为大家介绍其中的散列算法类库. 散列算法在某些特殊场景也可以当做加密方法使用:其特点是不可逆,同一内 ...
- 鹏程杯子2023 pwn
主要就是修改stdin的最后几位,使他变为write,然后泄露libc,为所欲为即可. 本人是卡在不知道stdin那里可以修改. 然后使用一下jmp qword rbp这个gadget 0x400a9 ...
- UVA12390 Distributing Ballot Boxes 题解
题目传送门 题意 有 \(n\) 个城市,\(b\) 个投票箱,第 \(i\) 个城市有 \(a_i\) 人,每个人均有一张票,将 \(b\) 个投票箱分给 \(n\) 个城市,每个城市的票分摊在投票 ...
- NC16681 [NOIP2003]加分二叉树
题目链接 题目 题目描述 设一个n个节点的二叉树tree的中序遍历为(l,2,3,-,n),其中数字1,2,3,-,n为节点编号.每个节点都有一个分数(均为正整数),记第j个节点的分数为di,tr ...
- Ehcache 3.x 笔记
现在Ehcache版本已经到3.10了, 网上查到的大部分还是2.x版本的使用说明, 把基础用法记了一下, 以后有时间再翻译. 基础使用, 创建 CacheManager CacheManager c ...