实验7-接口技术实验报告_第1页
实验7-接口技术实验报告_第2页
实验7-接口技术实验报告_第3页
实验7-接口技术实验报告_第4页
实验7-接口技术实验报告_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

福建工程学院&信息科学与工程学院实验报告课程名称面向对象程序设计任课教师姓名潘万丁学号年级15级专业网络工程班级网工1502实验编号实验7实验名称接口技术实验内容:1接口技术的应用假设一个系统中包含三个类:教师(Teacher)、学校(School)、打印机(Printer),具有如下要求:(1) 教员和学校都具有方法tell,用于返回信息。(2) 打印机能够将学校或教员返回的信息输出。编写测试类进行测试,要求使用接口技术。public class LoginDemo public static void main(String args) 源代码:package shiyan;/1.接口public interface Tell public String tell();/2.教师(Teacher)类class Teacher implements TellString teacher;public Teacher(String teacher)this.teacher = teacher;public String tell()return this.teacher;/3.学校(School)类class School implements TellString school;public School(String school)this.school=school;public String tell()return this.school;package shiyan;public class Printer /* * param args */4.打印机(Printer)类public static void print(Tell s)System.out.println(s.tell();public static void main(String args) / TODO 自动生成的方法存根Teacher a = new Teacher(Java教师-唐老师);School a1 = new School(学校-福建工程学院);print(a);print(a1);2工厂设计模式开发需求:(1)信用卡分为VISA, AMERICANEXPRESS, MASTERCARD三种类型,为每种信用卡写一个类。(2)每个信用卡类都有一个处理POS机刷卡的方法swipeCard_POS()。(3)请使用接口技术和工厂模式实现开发需求。public class LoginDemo public static void main(String args) 源代码:package shiyan2;/接口public interface Card public void swipeCard_POS();package shiyan2;import java.util.Scanner;public class Login /* * param args */测试类public static Card getInstance(String type,String id)Card card = null;if(VISA.equals(type)card = new VISA(id);if(AMERICANEXPRESS.equals(type)card = new AMERICANEXPRESS(id);if(MASTERCARD.equals(type);card = new MASTERCARD(id);return card;public static void main(String args) / TODO 自动生成的方法存根Scanner in = new Scanner(System.in);String name,id;System.out.println(输入信用卡类型:);name=in.nextLine();System.out.println(输入卡号:);id = in.nextLine();Card card = getInstance(name, id);card.swipeCard_POS();/3.MASTERCARD类 class MASTERCARD implements CardString id;public MASTERCARD(String id) this.id = id;public void swipeCard_POS() System.out.println(MASTERCARD-+id+-by POS!);/2.AMERICANEXPRESS类class AMERICANEXPRESS implements CardString id;public AMERICANEXPRESS(String id) this.id = id;public void swipeCard_POS()System.out.println(AMERICANEXPRESS-+id+-by POS!);/1.VISA类 class VISA implements Card String id;public VISA(String id) this.id = id;public void swipeCard_POS() System.out.println(VISA-+id+-by POS!); 3对象排序创建一个学生类Student,拥有以下属性与方法:l 属性:学号、姓名、性别、身高、体重l 方法:输出学生信息创建一个班级类Class,拥有以下属性与方法:l 属性:班级名、班级学生l 方法:输出所属学生信息编写方法sortByID(),按学号从小到大对班级内的学生进行排序。编写方法sortByHeight(),按身高从小到大对班级内的学生进行排序。编写方法sortByWeight(),按体重从小到大对班级内的学生进行排序。public class LoginDemo public static void main(String args) 源代码:package shiyan7;/java.lang.Comparable的作用是自然排序,般用Comparator匿名内部类创建比较器对象,作为参数传递给集合/学生类Studentpublic class Student implements Comparable String id;String name;char sex;double hight;double weight;public int compareTo(Student s)return pareTo(s.id);public Student(String id,String name,char sex,double hight,double weight) this.id=id;=name;this.sex=sex;this.hight=hight;this.weight=weight;public void disp() System.out.println(学号:+id+ 姓名:+name+ 性别:+sex+ 身高:+hight+ 体重:+weight);package shiyan7;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class Class String bj;ArrayListstudents;public Class(String bj)this.bj=bj;this.students=new ArrayList();public void disp1()System.out.println(班级:+bj);for(Student s:students)s.disp();public static void main(String args) / TODO 自动生成的方法存根Class a= new Class(网络工程 1501);a.students.add(new Student(3151912001 , 二十一郎 , 男, 160, 110);a.students.add(new Student(3151912002 , 二十二郎 , 男, 172, 120);a.students.add(new Student(3151912003 , 二十三郎 , 男, 175, 125);a.students.add(new Student(3151912004 , 二十三妹, 女, 170, 100);a.disp1();System.out.println(n);System.out.println(按学号从小到大对班级内的学生进行排序:);Collections.sort(a.students,new sortByID();a.disp1();System.out.println(n);System.out.println(按身高从小到大对班级内的学生进行排序:);Collections.sort(a.students,new sortByHeight();a.disp1();System.out.println(n);System.out.println(按体重从小到大对班级内的学生进行排序:);Collections.sort(a.students,new sortByWeight();a.disp1();/sortByID类,按学号从小到大对班级内的学生进行排序。class sortByID implements Comparatorpublic int compare(Student i1,Student i2) return pareTo(i2.id);/sortByHeight类,按身高从小到大对班级内的学生进行排序class sortByHeight implements Comparatorpublic int compare(Student i1,Student i2)if(i1.hight i2.hight)return -1;if(i

温馨提示

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

评论

0/150

提交评论