免费预览已结束,剩余2页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一、实验目的及内容目的:掌握和巩固Java面向对象程序设计的概念、方法。内容:1. 设计一个Java程序,该程序运行后,说明:一个类的所有对象共享同一个类成员变量;一个类的多个对象拥有各自的实例成员变量。2. 编写一个表示人的类Person,该类满足以下要求:l 有两个private成员变量:name(名字)、age(年龄);在Person类外访问这两个成员变量均通过接口器函数(getXXX,setXXX)。l 该类具有一个有两个参数的构造函数,这两个参数分别用来初始化name和age成员变量。l 该类包含一个方法public boolean older(Person r),用于比较当前对象年纪是否比参数r的年纪大,若是,则返回true,否则,返回false。编写另外一个类:TestPerson,该类包含下面函数:public static void sortPerson(Person personList),在该函数中利用Person类的older方法实现对数组personList按年龄从小到大排序。main函数位于该类中。在main函数中(1) 创建Person类的10个对象,并保存到一个数组中,创建10个对象时,每个对象的年龄为0100的一个随机数(用Math.random()函数得到)。(2) 输出这个数组中每个对象的名字、年龄。(3) 调用sortPerson方法,对这个数组中的10个Person对象按年龄排序。(4) 输出排序后数组中每个对象的名字、年龄。二、要求1. 给出上述程序的中涉及到的类的类图(可用visio画)、解决上述问题的思路、代码和测试结果。1.设计一个Java程序,该程序运行后,说明:一个类的所有对象共享同一个类成员变量;一个类的多个对象拥有各自的实例成员变量。设计思路:要实现一个类的所有对象共享同一个类成员变量;一个类的多个对象拥有各自的实例成员变量。我创建了一个Number类,并创建了两个对象number1和number2,该类有一个类成员函数static int numberofsum(int a, int b)来求和,一个类成员变量countnumber来统计对象的个数,还有实例成员变量num,通过两个对象求和以及统计对象个数,说明了一个类的所有对象共享同一个类成员变量,两个对象都有自己的num值说明了一个类的多个对象拥有各自的实例成员变量。类图:程序代码:/package number; public class Number public static void main(String args) System.out.println(Create two numbers!); Number number1 = new Number(5); System.out.println(After create the first number,countnumber is + number1.countnumbers); Number number2 = new Number(10); System.out.println(After create the second number,countnumber is + number2.countnumbers); System.out.println(The two numbers is + number1.num + and + number2.num); System.out.println(The sum of two numbers is + numberofsum(number1.num, number2.num); static int countnumbers = 0; int num; Number(int a) num = a; countnumbers+; static int numberofsum(int a, int b) int sum = a + b; return sum; 测试结果:2.编写一个表示人的类Person,以及另外一个类:TestPerson。具体要求见实验内容。 设计思路:定义两个类Person和TestPerson,TestPerson类中有public static void sortPerson(Person personList)来对创建的十个对象进行年龄大小排序。类图:程序代码:/package testperson; import java.util.Scanner;public class TestPerson public static void main(String args) final int number = 10; Scanner input = new Scanner(System.in); Person personArray = new Personnumber; for (int i = 0; i personArray.length; i+) System.out.print(Please enter the name of person + (i + 1) + :); String personName = input.next(); int personAge = (int) (Math.random() * 100); personArrayi = new Person(personAge, personName); System.out.println(The + (i + 1) + persons name is + personArrayi.getName() + and the age is + personArrayi.getAge(); sortPerson(personArray); public static void sortPerson(Person personList) Person t; for (int i = 0; i personList.length - 1; i+) for (int j = i + 1; j personList.length; j+) if (personListi.older(personListj) t = personListi; personListi = personListj; personListj = t; System.out.println(The age from young to old order is:); for (int k = 0; k r.age) return true; else return false; 测试结果:protection design of tall buildings GB50045-95 the construction interior trim design fire protection standard GB50222-95 6, building curtain wall design code for lightning protection lightning protection standard building GB50057-94 7, building curtain wall technical standards for lighting architectural lighting design standard GB/T50033-2001 optical properties of glass curtain wall GB/ T18091-2000 8, physical inspection and technical standards of architectural curtain wall construction performance testing method for in-plane deformation of curtain wall GB/T18250-2000 classification GB/T15225-94 wind pressure of building curtain wall building curtain wall physical properties deformation performance test method GB/T15227-94 curtain GB/T15226-94 curtain air permeability test methods rainwater seepage performance test methods GB/T15228-94 9, Windows testing technical standards of architectural curtain wall House Windows airtight, watertight and wind resistance performance test method JG/T211-2007 House Windows
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 餐饮外卖合作合同协议(2026年)
- 水晶吊灯拆卸清洗作业操作规范
- 蔬菜病虫害绿色防控认证
- 危化品火灾爆炸事故救援手册
- 中医拔罐排毒疗程手册
- 糖尿病低糖饮食配餐指引手册
- 夏季甜椒嫁接育苗技术规范
- 低脂低盐食谱设计规范
- 职业病危害事故应急处置指引
- 客户档案信息更新维护规范
- 2026年广西真龙彩印包装有限公司笔试题及答案
- (2026年)低钾血症诊治与管理专家共识解读
- 2026年中考政治考前冲刺押题试卷及答案(共九套)
- 法律实务2026年常见合同案例解析
- 20S515 钢筋混凝土及砖砌排水检查井
- 带状疱疹疫苗科普
- 走进人工智能-AI发展史及人工智能的应用
- 2026届苏锡常镇高三语文一模作文评分细则及标杆文:卓越源于有目的、有反馈的重复
- 22019+02306+05404+统计学原理或者叫统计与数据分析基础-国家开发大学期末考试题复习
- 2025年陕西供销集团有限公司社会招聘(8人)笔试参考题库附带答案详解(3卷合一版)
- 优生优育学课件
评论
0/150
提交评论