Java面向对象设计.doc_第1页
Java面向对象设计.doc_第2页
Java面向对象设计.doc_第3页
Java面向对象设计.doc_第4页
Java面向对象设计.doc_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

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

文档简介

Java考试模拟试题机试部分(Java)考试时间:100分钟, 卷面分:60分题目:品尝饮料一、 语言和环境1. 实现语言:Java2. 环境要求:JDK1.4或以上版本及其联机帮助文档二、 要求1、 使用命令行参数(饮料类型),输出该饮料类型的味道,如:当命令行参数为1时,结果见图1:图12、 如果没有该种饮料,结果见如图2:图2三、 推荐实现步骤1、 建立一个Java抽象类Drink,应当:a、 声明一个抽象方法taste(),该方法负责输出饮料的味道;b、 声明int型常量来代表不同的饮料类型(咖啡、啤酒、牛奶),如:1 咖啡,2 啤酒,3 牛奶c、 声明静态方法getDrink(int drinkType),根据传入的参数创建不同的饮料对象,并返回该对象,建议使用switch语句。该方法要求声明DrinkNotFoundException,当没有相对应的饮料类型时,抛出该异常。2、 建立Drink的具体子类:a、 分别建立Drink的子类:Coffee(代表咖啡),Beer(代表啤酒),Milk(代表牛奶);b、 子类分别要求:1. Coffee类 私有属性品牌(brand),带有此属性参数的构造方法;并提供相应的setter和getter方法;2. Beer类 私有属性颜色(color),带有此属性参数的构造方法;并提供相应的setter和getter方法;3. Milk类 私有属性状态(state),带有此属性参数的构造方法;并提供相应的setter和getter方法;c、 实现taste()方法,要求在控制台打印各自的味道特征。3、 建立异常类DrinkNotFoundExceptiona、 继承Exceptionb、 在Drink的方法getDrink(int drinkType)中声明引发DrinkNotFoundException异常,当传入的参数没有相对应的饮料类型时,抛出异常。c、 在使用getDrink方法的类中捕捉该异常。4、 建立Test测试类,测试以上内容的正确性a、 编写main方法,通过命令行传参的方式传入某种饮料的类型。b、 在main方法中,调用Drink类的getDrink方法,获得相应的饮料对象。注意捕获DrinkNotFoundException。c、 然后调用该饮料对象的taste()方法,输出该饮料的味道。5、 编译程序,并运行。四、 注意事项1、 仔细审题,把题目要求理解准确;2、 请注意多态性的运用;3、 请注意代码的书写、命名符合规范,应有适当的注释。答案:public class Test public static void main(String args) Coffee c = new Coffee();c.taste();Beer B = new Beer();B.taste();Milk M = new Milk();M.taste();abstract class Drink public int coffee = 1;public int beer = 2;public int milk = 3;int drinkType = new Scanner(System.in).nextInt();public abstract void taste();public void getDrink(int drinkType) try switch (drinkType) case 1:System.out.println(咖啡);break;case 2:System.out.println(啤酒);break;case 3:System.out.println(牛奶);break;default:System.out.println(对不起!没有你输入的饮料类型);throw new DrinkNotFoundException(); catch (DrinkNotFoundException e) System.out.println();class Coffee extends Drink private String brand;public String getBrand() return brand;public void setBrand(String brand) this.brand = brand;public Coffee() this.brand = brand;System.out.println(蓝山咖啡);public void taste() System.out.println(咖啡是苦的);class Beer extends Drink private String color;public String getColor() return color;public void setColor(String color) this.color = color;public void Beer() this.color = color;System.out.println(啤酒是黑的);public void taste() System.out.println(啤酒是涩的);class Milk extends Drink private String state;public String getState() return state;public void setState(String state) this.state = state;public void Milk() this.state = state;System.out.println(牛奶是液态的);public void taste() System.out.println(牛奶是甜的);class DrinkNotFoundException extends Exception public DrinkNotFoundException(String string) super();答案:public class Test public static void main(String args) Coffee c = new Coffee();c.taste();Beer B = new Beer();B.taste();Milk M = new Milk();M.taste();abstract class Drink public int coffee = 1;public int beer = 2;public int milk = 3;int drinkType = new Scanner(System.in).nextInt();public abstract void taste();public void getDrink(int drinkType) try switch (drinkType) case 1:System.out.println(咖啡);break;case 2:System.out.println(啤酒);break;case 3:System.out.println(牛奶);break;default:System.out.println(对不起!没有你输入的饮料类型);throw new DrinkNotFoundException(); catch (DrinkNotFoundException e) System.out.println();class Coffee extends Drink private String brand;public String getBrand() return brand;public void setBrand(String brand) this.brand = brand;public Coffee() this.brand = brand;System.out.println(蓝山咖啡);public void taste() System.out.println(咖啡是苦的);class Beer extends Drink private String color;public String getColor() return color;public void setColor(String color) this.color = color;public void Beer() this.color = color;System.out.println(啤酒是黑的);public void taste() System.out.println(啤酒是涩的);class Milk extends Drink private String state;public String getState() return state;public void setState(String state) this.state = state;public void

温馨提示

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

评论

0/150

提交评论