版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、成绩: 面向对象原理与Java实践课程实验报告实验1:对象和类姓 名 _ _ 班 级 _ _ 学 号 _ _ 实验地点 _ _ 实验时间 _ _ 指导教师 _ _ 面向对象原理与Java实践课程实验报告实验1:对象和类一、实验目的:l 掌握类的定义及应用方法l 掌握对象的创建方法,了解实例变量、实例方法和静态字段的特性l 体会Java在类和对象的设计中体现的面向对象的思想二、实验要求:l 具备Java基础知识(教材第1、2章内容)l 掌握Java中对象和类的基本概念和应用方法(教材第3章内容)l 设计类时注意保证类的封装性l 编写程序完成以下实验内容并上交实验报告(包括电子文档与纸质文档)三、
2、实验内容:设计并实现一个“广石化院电信学院学生通信录”系统。系统采用字符界面,在出现的主界面中按提示输入相应字符以选择“增加”、“删除”、“修改”、“查找”等功能进行操作(其中“查找”功能除能够查找某个学生的信息之外,还能够显示所有学生的信息)。记录的存储可采用数组或链表的方式。四、设计思路: 为了实现该系统,首先要需要两个类;一个类为主界面服务,用于接收用户输入信息以及做出相关反应;一个类为学生信息服务,定义学生通讯录信息的各个字段,以及操作字段的各个方法,考虑到部分字段的属性问题,所以将操作方法都写在学生类中,不再另外放置在其他类中。学生信息将会采用学生类的对象数组来储存。5、 程序源代码
3、:下面是ZhuJieMian 主界面类package pkg_TXL;import java.util.Scanner;public class ZhuJieMian /* * * 该类为通讯录系统的主界面提供主要功能显示,以及负责主菜单与子菜单的正常跳转 */public static void main(String args) / TODO Auto-generated method stub/XueShengXinXi Stu=ChuShiHua(); /初始化系统数据XueShengXinXi Stu=new XueShengXinXi100;Stu0=new XueShengXin
4、Xi("专业", "班级", "姓名", "性别", "电话", "家庭住址");Stu1=new XueShengXinXi("自动化", "自动化10-1", "张三", "男", "63256", "fnhjfdhsdbh");Stu2=new XueShengXinXi("自动化", "自动化10-2", &q
5、uot;李四", "女", "653256", "nfhggdfddhsdbh");Stu3=new XueShengXinXi("电子", "电子10-1", "王五", "男", "6156", "fnhvjbh");Stu4=new XueShengXinXi("电子", "电子10-2", "小红", "女", &quo
6、t;63956", "asdfnhjbh");Stu5=new XueShengXinXi("网络", "网络10-1", "小明", "男", "656", "nfgdgfnhjbh");Stu6=new XueShengXinXi("网络", "网络10-2", "小芳", "女", "62215", "nfgdmmmdfhbdsh&
7、quot;);Stu7=new XueShengXinXi("计算机", "计算机10-1", "黄龙", "男", "65584", "bbbdddfnhjbh");Stu8=new XueShengXinXi("计算机", "计算机10-2", "陈玲", "女", "666114", "mjgfddddfnhjbh");Stu9=new XueShen
8、gXinXi("测控", "测控10-1", "赵小斌", "男", "644814", "fthnrehbkjhjbh");Stu10=new XueShengXinXi("测控", "测控10-2", "钱颖", "女", "63641", "dbfsvnnhjbh");Stu11=new XueShengXinXi("电信", &q
9、uot;电信10-1", "孙武", "男", "6321514", "mmhmgvvdfnhjbh");Stu12=new XueShengXinXi("电信", "电信10-2", "李雨", "女", "6314", "43ghffvvdfnhjbh");for(int x=13;x<Stu.length;x+)Stux=new XueShengXinXi();System.
10、out.println("欢迎使用广石化电信学院通讯录系统!");ZhuJieMian: for(int exit=0;exit=0;)/exit为退出主菜单的指示变量,0为不退出,非0为退出System.out.println();System.out.println("*");System.out.println("*主菜单*");System.out.print(" 1。查询学生信息tt");System.out.println("2。新增学生信息");System.out.print(&
11、quot; 3。修改学生信息tt");System.out.println("4。删除学生信息");System.out.print(" 5。恢复已被删除信息tt");System.out.println("6。退出");System.out.println("*");System.out.println("*");System.out.print("请选择你要操作的功能序号:");Scanner scn_input=new Scanner(System.in);S
12、tring XuHao=scn_input.next();System.out.println();if(XuHao.equals("1") /二级菜单查询学生信息select:for(int exit2=0;exit2=0;) /exit为退出二级菜单的指示变量,0为不退出,非0为退出System.out.println("*");System.out.println("*二级菜单查询学生信息*");System.out.println("*");System.out.println();System.out.p
13、rint("请输入要查找的关键字(包括专业,班级,姓名等等),输入“all”显示全部信息,输入“back”返回主菜单:");String str_KeyWord=scn_input.next();System.out.println();if(str_KeyWord.equals("all")System.out.println();System.out.println("*学生通讯录信息*");XueShengXinXi.Show(Stu);System.out.println();System.out.println();cont
14、inue select;else if(str_KeyWord.equals("back")continue ZhuJieMian;elseint index=XueShengXinXi.Select(str_KeyWord,Stu);if(index0=0)System.out.println("抱歉,没有找到关于“"+str_KeyWord+"”的学生信息!");System.out.println();continue select;elseSystem.out.println("*学生通讯录信息*");Xu
15、eShengXinXi.Show(Stu,index);System.out.println();System.out.println();continue select;else if(XuHao.equals("2") /二级菜单新增学生信息insert:for(int exit3=0;exit3=0;)System.out.println();System.out.println("*");System.out.println("*二级菜单新增学生信息*");System.out.println("*");S
16、ystem.out.println();System.out.println("请按右边的格式输入新增学生的专业/班级/姓名/性别/电话/地址信息(前3项信息必填),输入“back”返回主菜单:");String str_Stu=scn_input.next();if(str_Stu.equals("")System.out.println("输入有误,请重新输入!");continue insert;else if(str_Stu.equals("back")System.out.println();contin
17、ue ZhuJieMian;elseboolean zuoFei=XueShengXinXi.Insert(Stu, str_Stu);if(zuoFei=true)System.out.println();System.out.println("由于学生信息必填项信息缺失,所以新增失败。返回至二级菜单.");System.out.println();continue insert;elseSystem.out.println();System.out.println("新增成功!返回至二级菜单.");System.out.println();conti
18、nue insert;/exit3=1;else if(XuHao.equals("3") /二级菜单修改学生信息System.out.println("*");System.out.println("*二级菜单修改学生信息*");System.out.println("*");System.out.println();System.out.println("*学生通讯录信息*");XueShengXinXi.Show(Stu);/System.out.println();update:for(
19、int exit4=0;exit4=0;) /exit为退出二级菜单的指示变量,0为不退出,非0为退出System.out.println();System.out.print("请输入要修改的学生前的序号(每次只能输入一个),输入“back”返回主菜单:");String str_XuHao=scn_input.next();System.out.println();if(str_XuHao.equals("back")System.out.println();continue ZhuJieMian;elseint id=XueShengXinXi.S
20、tr_Int(Stu,str_XuHao,false);if(id0<Stu.length&id0!=0)System.out.println();/int index=XueShengXinXi.Select_index(Stu, id0);System.out.println("该学生信息如下:");System.out.print(Stuid0.str_ZhuanYe+"/"+Stuid0.str_BanJi+"/");System.out.print(Stuid0.str_XingMing+"/&quo
21、t;+Stuid0.str_XingBie+"/");System.out.println(Stuid0.str_DianHua+"/"+Stuid0.str_DiZhi);System.out.println("请按以上格式输入修改后的信息(某一项无修改的话,则不用输入):");String str_Stu=scn_input.next();XueShengXinXi.Update(Stuid0, str_Stu);System.out.println("修改成功!已返回二级菜单");System.out.pri
22、ntln();System.out.println("*");System.out.println("*二级菜单修改学生信息*");System.out.println("*");System.out.println();System.out.println("*学生通讯录信息*");XueShengXinXi.Show(Stu);continue update;elseSystem.out.println("输入有误!已返回至二级菜单");continue update;/exit4=1;el
23、se if(XuHao.equals("4") /二级菜单删除学生信息System.out.println("*");System.out.println("*二级菜单删除学生信息*");System.out.println("*");System.out.println();System.out.println("*学生通讯录信息*");XueShengXinXi.Show(Stu);delete:for(int exit5=0;exit5=0;) /exit为退出二级菜单的指示变量,0为不退
24、出,非0为退出System.out.println();/System.out.println("*二级菜单删除学生信息*");System.out.print("请输入要删除的学生信息前的序号(输入多个序号时用/隔开),输入“all”删除全部信息,输入“back”返回主菜单:");String str_xuHao=scn_input.next();System.out.println();if(str_xuHao.equals("back")System.out.println();continue ZhuJieMian;else
25、if(str_xuHao.equals("all")XueShengXinXi.Delete_All(Stu);System.out.println("全部数据已被删除,已返回主菜单。");/XueShengXinXi.Show(Stu);continue ZhuJieMian;elseint id=XueShengXinXi.Str_Int(Stu,str_xuHao,false);if(id0<Stu.length)/System.out.println();XueShengXinXi.Delete_index(Stu, id);/System
26、.out.println("删除成功!已返回二级菜单");System.out.println();System.out.println("*");System.out.println("*二级菜单删除学生信息*");System.out.println("*");System.out.println();System.out.println();System.out.println("*学生通讯录信息*");XueShengXinXi.Show(Stu);continue delete;els
27、eSystem.out.println("输入有误!已返回至二级菜单");continue delete;/exit5=1;else if(XuHao.equals("5") /二级菜单恢复已被删除信息System.out.println("*");System.out.println("*二级菜单恢复已被删除信息*");System.out.println("*");System.out.println();System.out.println("*回收站信息*");boo
28、lean bol=XueShengXinXi.Show_Del(Stu);if(bol=true)System.out.println();System.out.println("回收站为空,目前没有被删除的信息!已返回至主菜单。");continue ZhuJieMian;elserecover:for(int exit6=0;exit6=0;) /exit为退出二级菜单的指示变量,0为不退出,非0为退出System.out.println();/System.out.println("*二级菜单删除学生信息*");System.out.print(&
29、quot;请输入要恢复的信息的序号(输入多个序号时用/隔开),输入“all”恢复全部信息,输入“back”返回主菜单:");String str_xuHao=scn_input.next();System.out.println();if(str_xuHao.equals("back")System.out.println();continue ZhuJieMian;else if(str_xuHao.equals("all")XueShengXinXi.Recover_All(Stu);System.out.println("全部恢
30、复完毕!已返回主菜单。");continue ZhuJieMian;elseint id=XueShengXinXi.Str_Int(Stu,str_xuHao,true);if(id0<Stu.length)/System.out.println();XueShengXinXi.Recover_index(Stu, id);/System.out.println("删除成功!已返回二级菜单");System.out.println();System.out.println("*");System.out.println("*二
31、级菜单恢复已被删除信息*");System.out.println("*");System.out.println();System.out.println();System.out.println("*回收站信息*");XueShengXinXi.Show_Del(Stu);continue recover;elseSystem.out.println("输入有误!已返回至二级菜单");continue recover;else if(XuHao.equals("6")break ZhuJieMian;
32、elseSystem.out.println("输入有误,已返回主菜单!");System.out.println();continue ZhuJieMian;/exit=1;下面是XueShengXinXi 学生信息类package pkg_TXL;public class XueShengXinXi String str_ZhuanYe=""String str_BanJi=""/String str_XueHao=""String str_XingMing=""String str_Xin
33、gBie=""String str_DianHua=""String str_DiZhi=""Boolean bol_ZuoFei=true;/String str_ID=""int int_ID=0;XueShengXinXi()/有参构造方法public XueShengXinXi(String ZhuanYe,String BanJi,String XingMing,String XingBie,String DianHua,String DiZhi)str_ZhuanYe=ZhuanYe;str_BanJi
34、=BanJi;str_XingMing=XingMing;str_XingBie=XingBie;str_DianHua=DianHua;str_DiZhi=DiZhi;bol_ZuoFei=false;/新增学生信息的函数public static boolean Insert(XueShengXinXi stu,String str_stu)int i=0;for(;i<stu.length;i+)if(stui.bol_ZuoFei=true)break;stui.str_ZhuanYe=""stui.str_BanJi=""stui.str
35、_XingMing=""stui.str_XingBie=""stui.str_DianHua=""stui.str_DiZhi=""int k=0;for(int j=1;j<=str_stu.length();j+)if(str_stu.substring(j-1, j).equals("/")k+=1;/index=j;else/System.out.println(k);if(k=0)/stui.str_ZhuanYe=str_stu.substring(index, j);stu
36、i.str_ZhuanYe=stui.str_ZhuanYe+str_stu.substring(j-1, j);else if(k=1)/stui.str_BanJi=str_stu.substring(index, j);stui.str_BanJi=stui.str_BanJi+str_stu.substring(j-1, j);else if(k=2)/stui.str_XingMing=str_stu.substring(index, j);stui.str_XingMing=stui.str_XingMing+str_stu.substring(j-1, j);else if(k=
37、3)/stui.str_XingBie=str_stu.substring(index, j);stui.str_XingBie=stui.str_XingBie+str_stu.substring(j-1, j);else if(k=4)/stui.str_DianHua=str_stu.substring(index, j);stui.str_DianHua=stui.str_DianHua+str_stu.substring(j-1, j);else if(k=5)/stui.str_DiZhi=str_stu.substring(index, j);stui.str_DiZhi=stu
38、i.str_DiZhi+str_stu.substring(j-1, j);if(stui.str_ZhuanYe.equals("")|stui.str_BanJi.equals("")|stui.str_XingMing.equals("")stui.bol_ZuoFei=true;stui.str_ZhuanYe=""stui.str_BanJi=""stui.str_XingMing=""stui.str_XingBie=""stui.str_Di
39、anHua=""stui.str_DiZhi=""elsestui.bol_ZuoFei=false;return stui.bol_ZuoFei;public static void Show(XueShengXinXi stu) /格式化的输出全部学生的信息int col=new int6; /定义六个int类型变量,分别存储六列宽度for(int x=0;x<col.length;x+)colx=0;for(int j=0;j<stu.length;j+) /第一步,获取全部列的最大宽度if(stuj.str_ZhuanYe.lengt
40、h()>col0&stuj.bol_ZuoFei=false)col0=stuj.str_ZhuanYe.length();if(stuj.str_BanJi.length()>col1&stuj.bol_ZuoFei=false)col1=stuj.str_BanJi.length();if(stuj.str_XingMing.length()>col2&stuj.bol_ZuoFei=false)col2=stuj.str_XingMing.length();if(stuj.str_XingBie.length()>col3&stuj
41、.bol_ZuoFei=false)col3=stuj.str_XingBie.length();if(stuj.str_DianHua.length()>col4&stuj.bol_ZuoFei=false)col4=stuj.str_DianHua.length();if(stuj.str_DiZhi.length()>col5&stuj.bol_ZuoFei=false)col5=stuj.str_DiZhi.length();for(int i=0;i<col.length;i+) /第二步,将最大宽度加上6,得到实际宽度coli=coli+6;for
42、(int k=0;k<stu.length;k+) /第三步,将实际宽度减去字符长度得到空字符长度/stuk.ID=" "+k;/stuk.str_ID=Integer.toString(k);_ID=k;/System.out.println(stuk.ID);if(k=0)System.out.print("t"+GetSpace(stuk.str_ZhuanYe,col0);System.out.print(GetSpace(stuk.str_BanJi,col1);System.out.print(GetSpace(stuk
43、.str_XingMing,col2);System.out.print(GetSpace(stuk.str_XingBie,col3);System.out.print(GetSpace(stuk.str_DianHua,col4);System.out.println(GetSpace(stuk.str_DiZhi,col5);/for(int l=0;l<col0+col1+col2+col3+col4+col5+5;l+)/System.out.print("");/for(int l=0;l<6*4;l+)System.out.print("
44、");System.out.println("");else if(k>0&k<10&stuk.bol_ZuoFei=false)System.out.print(k+"t"+GetSpace(stuk.str_ZhuanYe,col0);System.out.print(GetSpace(stuk.str_BanJi,col1);System.out.print(GetSpace(stuk.str_XingMing,col2);System.out.print(GetSpace(stuk.str_XingBie,c
45、ol3);System.out.print(GetSpace(stuk.str_DianHua,col4);System.out.println(GetSpace(stuk.str_DiZhi,col5);else if(k>=10&stuk.bol_ZuoFei=false)System.out.print(k+"t"+GetSpace(stuk.str_ZhuanYe,col0);System.out.print(GetSpace(stuk.str_BanJi,col1);System.out.print(GetSpace(stuk.str_XingMin
46、g,col2);System.out.print(GetSpace(stuk.str_XingBie,col3);System.out.print(GetSpace(stuk.str_DianHua,col4);System.out.println(GetSpace(stuk.str_DiZhi,col5);private static String GetSpace(String str,int len) /根据每个字符不同加制表符/*String space=""if(len>=str.length()for(int i=0;i<len-str.length();i+)space=space+" "return str+'t'+'t'elser
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2030年中国单面背网超市货架数据监测研究报告
- 2025年中国油墨专用柔软剂市场调查研究报告
- 2025至2031年中国锻钢蝶阀行业投资前景及策略咨询研究报告
- 2025至2031年中国耐磨合金管行业投资前景及策略咨询研究报告
- 2025至2031年中国牙垫行业投资前景及策略咨询研究报告
- 大跨度钢桁架吊装施工过程模拟与力学分析
- 二零二四年度园林景观土方开挖与照明工程承包合同3篇
- 2025年度城市绿地除草养护工程合同范本3篇
- 2025年度山地林业资源开发承包经营合同3篇
- 2025年度个人专利许可使用及维权服务合同4篇
- 2025年度版权授权协议:游戏角色形象设计与授权使用3篇
- 心肺复苏课件2024
- 《城镇燃气领域重大隐患判定指导手册》专题培训
- 湖南财政经济学院专升本管理学真题
- 2024年湖南商务职业技术学院单招职业适应性测试题库带答案
- 全国身份证前六位、区号、邮编-编码大全
- 2024-2025学年福建省厦门市第一中学高一(上)适应性训练物理试卷(10月)(含答案)
- 2024年全国各地中考试题分类汇编:作文题目
- 《零售学第二版教学》课件
- 广东省珠海市香洲区2023-2024学年四年级下学期期末数学试卷
- 房地产行业职业生涯规划
评论
0/150
提交评论