面向对象程序设计作业答案_第1页
面向对象程序设计作业答案_第2页
面向对象程序设计作业答案_第3页
面向对象程序设计作业答案_第4页
面向对象程序设计作业答案_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

1、面向对象程序设计作业一1. 类和对象的概念和关系是什么?对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位。一个对象由一组属性和对这组属性进行操作的一组服务组成,类是具有相同属性和服务的一组对象的集合。类是对象的模板,对象是类的实例2. 用UML表示交通工具Vehicle类及名为car1,car2及car3的三个Vehicle对象car2:Vehiclecar3:Vehiclecar1:VehicleVehicle对于账户的最小余额,每个账户都共享一个共同的值。3. 简述对象之间的消息传递机制是如何实现的? 当程序运行时,我们使用类和由类生成的对象来完成任务。而要命令类或对象执

2、行某项任务,就需要给它发送一条消息(message)。为了能够处理所接收到的消息,类或对象必须拥有相应的方法(method)。一个方法就是一个指令序列,也就是一段程序代码,类似于C语言中的函数。为类定义的方法称为类方法(class method),为对象定义的方法称为实例方法(instance method)。类方法可以通过类直接调用,实例方法则必须先创建类的实例才能够调用。4. import语句的用途是什么?Java程序是否总要包括import语句?import保留字用于引入其他包中的类。Java如果使用的都是同一包的类的话则不需要import保留字。5. 什么是Java的源文件?什么是字节

3、码文件?Java的源文件是以.java结尾的文本文件,字节码文件是将Java源文件经过Java编译器编译后的文件,字节码文件不能直接运行,只能运行于Java虚拟机之上。6. Java虚拟机是什么?它有作用是什么?Java虚拟机是一个想象中的机器,在实际的计算机上通过软件模拟来实现。Java语言的一个非常重要的特点就是与平台的无关性。而使用Java虚拟机是实现这一特点的关键。一般的高级语言如果要在不同的平台上运行,至少需要编译成不同的目标代码。而引入Java语言虚拟机后,Java语言在不同平台上运行时不需要重新编译。Java语言使用模式Java虚拟机屏蔽了与具体平台相关的信息,使得Java语言编

4、译程序只需生成在Java虚拟机上运行的目标代码(字节码),就可以在多种平台上不加修改地运行。Java虚拟机在执行字节码时,把字节码解释成具体平台上的机器指令执行。7. 描述对象声明和对象生成之间的区别。使用内存状态图来说明这种区别对象声明是为对象的引用创建一个空间,而对象生成则是创建一个类的实例,即为对象分配空间,如果需要的话,其还会将对象空间的地址赋给其应用。如 Tester t1;t1t1 = new Tester();t1 :Tester8. 编写Java应用程序,用一个对话框显示当前时间import javax.swing.*;import java.util.*;public cla

5、ss Test public static void main(String args) Date today = new Date( ); JOptionPane.showMessageDialog(null,today); 9. 下面的代码段会有什么样的输出:class Q2main public static void main(String args) QuestionTwo q2; q2= new QuestionTwo(); q2.init(); q2.increment(); q2.increment(); System.out.println(q2.getCount(); cl

6、ass QuestionTwo private int count; public void init() count=1; public void increment() count=count+1; public int getCount() return count; 输出结果:310. 编写可以根据用户的年龄和身高给出推荐的体重的程序,利用下面的公式计算出推荐的体重:recommandedWeight=(height -100 + age/10)*0.9定义名为Height(身高)的公共服务类,他应该有可以根据身高得出推荐体重的方法public class Test public st

7、atic void main(String args) Weight w1 = new Weight(); System.out.println( w1.getRecommendedWeight(30,170); class Weightpublic double getRecommendedWeight(int age,int height) return (height - 100 + age/10) *0.9; 作业二1.假如x的值为10,y的值为20,z的值为30,求出下列布尔表达式的值:a) x>y && y>x:falseb) (x<y+z) &a

8、mp;& (x+10<=20):truec) !(x<y+z) | !(x+10<=20):falsed) !(x=y) && (x!=y) && (x<y | y<x):true2.用switch语句重写下面的if语句。selection=Inter.parseInt(JOptionPane.showInputDialog(null,”Enter selection:”);if (selection=0) System.out.println(“You selected Magenta”);else if (selecti

9、on=1) System.out.println(“You selected Red”);else if (selection=2) System.out.println(“You selected Blue”);else if (selection=3) System.out.println(“You selected Green”);else System.out.println(“Invalid selection”);改写为:selection=Inter.parseInt(JOptionPane.showInputDialog(null,”Enter selection:”);swi

10、th (selection)case 0: System.out.println(“You selected Magenta”);break;case 1: System.out.println(“You selected Red”);break;case 2:System.out.println(“You selected Blue”);break;case 3:System.out.println(“You selected Green”);break;default: System.out.println(“Invalid selection”);)3.画出下面两个switch语句的控制

11、流程图a) switch (choice) case 1: a=0; break; case 2: b=1; break; case 3: c=2; break; case 4: d=3: break;b) switch (choice) case 1: a=0; case 2: b=1; case 3: c=2; default: d=3;a)b)4.分别用for、do-while和while语句计算下面的累加和:c) 1+1/2+1/3+1/4+1/15for循环:int x;double result=0.0F;for(double i=1;i<=15;i+) result+=1/

12、i;System.out.println(result);do-while循环:double x=1;double result=0.0F;do result+=1/x; x+;while(x<=15);System.out.println(result);while循环:double x=1;double result=0.0F;while(x<=15) result+=1/x; x+; ;System.out.println(result);d) 5+10+15+50for循环:int x;int result=0;for(int i=1;i<=10;i+) result

13、+=i*5;System.out.println(result);do-while循环:int x=1;int result=0;do result+=x*5; x+;while(x<=10);System.out.println(result);while循环:int x=1;int result=0;while(x<=10) result+=x*5; x+;System.out.println(result);5.编写一个计算闰年的程序,要求用户输入一个年份,如果用户输入的年份不在03000年内则给予用户提示要求其重新输入,否则判断该年份是否为闰年并返回结果。public cl

14、ass Test public static void main(String args) LeapYear ly=new LeapYear(); System.out.println(puteLeapYear(1998); System.out.println(puteLeapYear(1900); System.out.println(puteLeapYear(2000); class LeapYearpublic boolean computeLeapYear(int year)if (year % 4 = 0 && year % 100 != 0 )return tru

15、e;if (year % 100 = 0 && year % 400 = 0 )return true;return false;6.下列哪一组重载方法是不合法的?e) public void compute(int num)public int compute(int num)f) public void move(double length)public void move()g) public int adjust(double amount)public void adjust(double amount , double charge)h) public void d

16、oWork() public void doWork(String name)public int doWork(double num)第 a)组7.完成下面这个类中的前四个构造方法。其中每一构造方法都是用关键字this调用第五个构造方法: class Cat private static final String DEFAULT_NAME = "NO NAME" private static final int DEFAULT_HGT=6; private static final double DEFAULT_WGT=10.0; private String name;

17、 private int height; private double weight; public cat() /分配缺省值给个成员变量 this(“”,0,0); public cat(String name) /将参数值赋给name这个成员变量,height和weight赋缺省值 this(name,0,0); public cat(String name int height) /将参数值分别赋给name和height两个成员变量,weight赋缺省值 this(name,height,0); public cat(String name int weight) /将参数值分别赋给na

18、me和weight两个成员变量,height赋缺省值 this(name,0, weight); publie cat(String name,int height,double weight) =name; this.height=height; this.weight=weight; .8.为Fraction定义一个类方法compare,compare接受两个Fraction对象f1和f2,并返回:1.如果f1小于f2,返回-12.如果f1等于f2,返回03.如果f1大于f2,返回1public static int compare (Fraction f1, Fract

19、ion f2) double f1_dec = f1.decimal();double f2_dec = f2.decimal();if (f1_dec <f2_dec) return -1; else if(f1_dec =f2_dec) return 0; else return 1;作业三1.什么是异常?Java的异常处理机制是?异常(exception)表示在程序正常运行过程中可能发生的错误的情形。Java通过面向对象的方法来处理异常。在一个方法的运行过程中,如果发生了异常,则这个方法生成代表该异常的一个对象,并把它交给运行时系统,运行时系统寻找相应的代码来处理这一异常。把生成异

20、常对象并把它提交给运行时系统的过程称为抛弃(throw)一个异常。运行时系统在方法的调用栈中查找,从生成异常的方法开始进行回朔,直到找到包含相应异常处理的方法为止,这一个过程称为捕获(catch)一个异常。2.分别输入-1,0和12XY,请写出这段代码的输出结果。int number1;trynumber1 = Integer.parseInt(JOptionPane.showInputDialog(null,"input");if (number1 !=0)throw new Exception("Not Zero");catch (NumberFor

21、matException e)System.out.println("Cannot convert to int");catch (Exception e) System.out.println("Error:"+e.getMessage();finallySystem.out.println("finally Clause Executed");输入-1时:Error:Not Zerofinally Clause Executed输入0时:finally Clause Executed输入12XY时:Cannot convert t

22、o intfinally Clause Executed3. 分析下面程序代码的输出结果: class MyException extends Exception public MyException() public MyException(String msg) super(msg); public class ExceptionTest public static void f() throws MyException System.out.println("The 1st line of f()"); throw new MyException("Orig

23、inated in f()"); public static void main(String args) System.out.println("The 1st line of main()"); try System.out.println("The 2nd line of main()"); f(); System.out.println("The 3rd line of main()"); catch(MyException e) System.out.println(e.getMessage(); finally

24、System.out.println("The 4th line of main()"); System.out.println("The 5th line of main()"); 输出结果为:The 1st line of main()The 2nd line of main()The 1st line of f()Originated in f()The 4th line of main()The 5th line of main()4. 分析下面程序代码的输出结果:class Shape void draw() System.out.printl

25、n("Shape.draw()"); class Circle extends Shape void draw() System.out.println("Circle.draw()"); class Square extends Shape void draw() System.out.println("Square.draw()"); class ShapeGenerator public Shape getShape(int index) switch(index) default: case 0: return new Cir

26、cle(); case 1: return new Square(); public void shapeDraw(Shape s) s.draw(); public class Shapes private static ShapeGenerator gen =new ShapeGenerator(); public static void main(String args) Shape s = new Shape3; for(int i = 0; i < s.length; i+) si = gen.getShape(i); for(int i = 0; i < s.length; i+) ge

温馨提示

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

最新文档

评论

0/150

提交评论