首页 云大JAVA实验6Lab #6(陆歌浩)

云大JAVA实验6Lab #6(陆歌浩)

举报
开通vip

云大JAVA实验6Lab #6(陆歌浩)云南大学软件学院 实验报告 课程: Java程序设计实验 学期: 2012-2013学年 第一学期 任课教师: 陆歌浩 专业: 软件工程 学号: 20101120007 姓名: 孙金文 成绩: 实验6 Applying OO Concepts Using Java2 一、实验目的 练习Java中面向对象概念的应用--对象构造、包和文档注释。 二、实验内容 Program Write a class called Person with the f...

云大JAVA实验6Lab #6(陆歌浩)
云南大学软件学院 实验报告 课程: Java程序 设计 领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计 实验 学期: 2012-2013学年 第一学期 任课教师: 陆歌浩 专业: 软件 工程 路基工程安全技术交底工程项目施工成本控制工程量增项单年度零星工程技术标正投影法基本原理 学号: 20101120007 姓名: 孙金文 成绩: 实验6 Applying OO Concepts Using Java2 一、实验目的 练习Java中面向对象概念的应用--对象构造、包和文档注释。 二、实验内容 Program Write a class called Person with the following attributes:          title (Mr., Mrs., Ms., etc.)          first name          last name          nickname          age in years          sex (boolean - true/false to indicated either male or female)  Write a constructor that takes no parameters and performs no initializations.  Write a constructor that takes a parameter for each of the attributes listed above and sets them within the objects by calling the setter methods listed below.  The Person class should have a setter method and a getter method with public access for each attribute. In the setter methods, get rid of any leading or trailing spaces (String trim() method).  For a Person with the following attributes:  title = "Mr."  first name = "Michael"  last name = "Zheng"  nickname = "Mike"  age = 22  sex = true (true is male, false is female)  The Person class should have the following public access methods that return Strings as follows:  standardName()  concatenation of the first and last names  (i.e., "Michael Zheng")  formalName()  concatenation of the title, first name, lastname (i.e., "Mr. Michael Zheng")  casualName()  return the nickname if it is not null, otherwise return the first name  (i.e., "Mike")  Be realistic when generating names. If a particular attribute does not exist for a given person, don't try to concatenate it. If necessary, add appropriate spacing and punctuation, but do not leave any leading or trailing spaces in the String that is returned.    MakePerson  Write a class called MakePerson with a main() method that instantiates 2 Person objects.  Initialize the attributes of one of the Person objects by supplying parameters to it's constructor. Instantiate the other Person object with the default constructor (that does not accept any parameters), then set it's attributes via the appropriate setter methods.  For each of the Person objects, execute and print (System.out.println()) the results of all of the getter methods and of the standardName(), formalName(), and casualName() methods. 实验代码: class Person { //类内部声明变量; private String title; private String firstname; private String lastname; private String nickname; private int age; private boolean sex; public Person(){}; //声明一个默认的构造器,用于后面来构造对象 public Person(String title,String firstname,String lastname,String nickname,int age,boolean sex) { this.title = title; this.firstname = firstname; this.lastname = lastname ; this.nickname = nickname; this.age = age; this.sex = sex; } //在所有的访问器和更改器及需要返回的字符串变量中加入String trim()方法,用于去掉字符串首尾的空格; public void setTitle(String title) { this.title = title.trim(); } public String getTitle() { return title.trim() ; } public void setFirstname(String firstname) { this.firstname = firstname.trim(); } public String getFirstname() { return firstname.trim(); } public void setLastname(String lastname) { this.lastname = lastname.trim() ; } public String getLastname() { return lastname.trim(); } public void setNickname(String nickname) { this.nickname = nickname.trim(); } public String getNickname() { if(!nickname.equals("")) return nickname.trim(); else return "\"don't have one。\"";//若没有小名返回这个字符串告诉用户 } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setSex(boolean sex) { this.sex = sex; } public boolean getSex() { return sex; } public String standardName() { return firstname.trim() + " " + lastname.trim(); } public String formalName() { return title.trim() + " " + firstname.trim() + " " + lastname.trim(); } public String casualName() { if(!nickname.equals("")) //表示没有小名 return nickname.trim(); else return firstname.trim(); } } public class MakePerson { public static void main(String[] args) { //用俩个Person对象来填满name数组; Person[] name=new Person[2]; name[0]= new Person(" Mr. "," Michael "," Zheng "," Mike",22,true); //用带参数的构造器实例化一个对象 name[1] = new Person(); //用不带参数的构造器实例化一个对象 name[1].setTitle(" Mr. "); name[1].setFirstname(" Jinwen "); name[1].setLastname(" Sun "); name[1].setNickname(" "); name[1].setAge(22); name[1].setSex(true); int count=1;//声明一个计数器用于统计是第几个人的信息; for(Person e:name) //使用for each循环语句打印结果 { System.out.println("The "+count+"th person's information:"); System.out.println( "Title = " + e.getTitle() +"\nFirst name = " + e.getFirstname() +"\nLast name = " + e.getLastname() +"\nNick name = " + e.getNickname() +"\nAge = " + e.getAge() +"\nSex = "+ e.getSex() + " (true is male, false is female)" +"\nStandardName = "+ e.standardName() +"\nFormalName = "+ e.formalName() +"\nCasualName = " + e.casualName()+"\n" ); count++; } System.out.println("孙金文-20101120007"); } } 运行截图: PAGE 1
本文档为【云大JAVA实验6Lab #6(陆歌浩)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_190053
暂无简介~
格式:doc
大小:73KB
软件:Word
页数:5
分类:互联网
上传时间:2012-10-23
浏览量:102