Java面向对象程序设计实验报告_第1页
Java面向对象程序设计实验报告_第2页
Java面向对象程序设计实验报告_第3页
Java面向对象程序设计实验报告_第4页
Java面向对象程序设计实验报告_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一、 实验目的及内容目的:掌握和巩固Java 面向对象程序设计的概念、方法。内容:1 .设计一个Java程序,该程序运行后,说明:一个类的所有对象共享同一个类成员变量;一个类的多个对象拥有各自的实例成员变量。2 .编写一个表示人的类 Person,该类满足以下要求:有两个private成员变量:name (名字)、age (年龄);在 Person类外访问这两个成员变量均通过接口器函数(getXXX , setXXX )。该类具有一个有两个参数的构造函数,这两个参数分别用来初始化name和age成员变量。该类包含一个方法 public boolean older(Person r),用于比较当

2、前对象年纪是否比参数r的年纪大,若是,则返回true,否则,返回false。编写另外一个类:TestPerson,该类包含下面函数:public static void sortPerson (Person口 personList),在该函数中利用 Person类的 older方法实现对数组 personList 按年龄从小到大排序。main 函数位于该类中。在main 函数中(1) 创建Person类的10个对象,并保存到一个数组中,创建10个对象时,每个对象的年龄为0100的一个随机数(用 Math.random()函数得到)。2)输出这个数组中每个对象的名字、年龄。(3)调用sortPe

3、rson方法,对这个数组中的10个Person对象按年龄排序。4)输出排序后数组中每个对象的名字、年龄。二、要求1. 给出上述程序的中涉及到的类的类图(可用visio 画)、解决上述问题的思路、代码和测试结果。1. 设计一个Java 程序,该程序运行后,说明:一个类的所有对象共享同一个类成员变量;一个类的多个对象拥有各自的实例成员变量。设计思路 :要实现一个类的所有对象共享同一个类成员变量;一个类的多个对象拥有各自的实例成员变量。我创建了一个Number类,并创建了两个对象 number1 和 number2 , 该 类 有 一 个 类 成 员 函 数 static int numberofs

4、um(int a, int b) 来求和,一个类成员变量countnumber 来统计对象的个数,还有实例成员变量num通过两个对象求和以及统计对象个数,说明了一个类的所有对象共享同一个类成员变量,两个对象都有自己的num值说明了一个类的多个对象拥有各自的实例成员变量。类图: r -1 1Kl - r 乂 i 1rNumberI li ;一-int num-static int co un tn umbers-FNumbcr(int a)()Asiatic int numbcrofsum(int a. int b)()程序代码:/package numbe r public class Num

5、ber 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 se

6、cond 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 = 0int num。Number(int a) num =

7、acountnumbers+)static int numberofsum(int a, int b) int sum = a + breturn sum.测试结果:Iraw C :Wi n d owssyste m 3 2cmd. exeMicrosoft Windows6-1-7601F版权所有心2009 Microsoft CorpDit ionfl保留所有权利"C : User-s pc >G :G = >c d G:、大二课程3旗”曰程序设计7实验报告j已丫已实验一HumbefsrcnumberG:、大二课程(1石"d程序设计验报告java实验三Xhk

8、imbEF7rLe Snunber>javac Number. JavaG二、大二课程程序设计验报告实验二Huunhei"8片c Snunher>jaua Number Create two numbersf fiFtei* create the f ivst number, count number is 1 After ere ate the second n umberco on tn umber is 2 The tun niimbefg is 5 and 10 The sum of tuo nunheps is 152.编写一个表示人的类Person,以及另外一

9、个类:TestPerson具体要求见 实验内容。设计思路:定义两个类Person和TestPerson , TestPerson类中有public static void sortPerson(Person口 personList ) 来对创建的十个对象进行年龄大小排序。4 / 8类图:加 i同 iM i 0Person, -nanie : string-age : int Person(int getAgc. String getNameK) public boolean older(Person r)(卜斗public ini gciAgc()public String gtlName (

10、)public void sctAgc(int setage)()小M撕 public void sc(Nanic(String se(namc)()Tcsl Person-Person person Array - new Person number+public static void sori Person (Person t person I j$t)()程序代码:/package testperson import java.util.Scanner。public class TestPerson public static void main(String口 args) final

11、 int number = 10。Scanner input = new Scanner(System.in)Person口 personArray = new Personnumberfor (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()

12、* 100)。personArrayi = new Person(personAge, personName。 )System.out.println("The " + (i + 1) + " person's name is " + personArrayi.getName()+ " and the age is " + personArrayi.getAge()。sortPerson(personArray。)public static void sortPerson(Person personList) Person t

13、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 < personList.len

14、gth。k+) System.out.println(k + 1) + ", name: " + personListk.getName() + ", age: " + personListk.getAge()。7 / 8class Person private int age。private String name。Person(int getAge, String getName) age = getAge。name = getName。public int getAge() return age。public String getName() re

15、turn name。public void setAge(int setage) age = setagepublic void setName(String setname) name = setname。public boolean older(Person r) if (this.age > r.age) return true。 else return false。测试结果:C:W in dows$yst e E 3 2cm d. exe口 |回|G:、大二课程3m3程序设计,实聆报告jav已实验=TestP匕尸snns甘c、tewtper&anXjaQa Test Pe

16、psqfiPlease enter the name of person 1:aaaThe 1person's nameisaaa Andtheageis88Pleaseenterthe nameof person2:bbbThe 2person,s nameisbbb andtheageis55Pleaseentertlie nameoF person3 :cccThe 3person fs nameisccc Andtheageis58Pleaseenterthe nameof person4:dddThe 4peFCon's nameisddd andtheageJ _ IC2Pleaseenterthe nanuof person5:eeeThe 5person 13 naneissee andt heageisSIPleaseenterthe nameof person6 ;fffThe 6person's nameisFfF andtheageis56Pleaseenterthe nanaof person7:9(99The ?person's nameisffarsf andth

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论