1、中南大学网络教育课程考试面向对象程序设计作业参考答案面向对象程序设计作业及答案作业一1.类和对象的概念和关系是什么?对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位。一个对象由一组属性和对这组属性进行操作的一组服务组成,类是具有相同属性和服务的一组对象的集合。类是对象的模板,对象是类的实例。2.用UML表示交通工具Vehicle类及名为car1,car2及car3的三个Vehicle对象 3.简述对象之间的消息传递机制是如何实现的?当程序运行时,我们使用类和由类生成的对象来完成任务。而要命令类或对象执行某项任务,就需要给它发送一条消息(message)。为了能够处理所接收到的
2、消息,类或对象必须拥有相应的方法(method)。一个方法就是一个指令序列,也就是一段程序代码,类似于C语言中的函数。为类定义的方法称为类方法(class method),为对象定义的方法称为实例方法(instance method)。类方法可以通过类直接调用,实例方法则必须先创建类的实例才能够调用。4.import语句的用途是什么?Java程序是否总要包括import语句?import保留字用于引入其他包中的类。Java如果使用的都是同一包的类的话则不需要import保留字。5.什么是Java的源文件?什么是字节码文件?Java的源文件是以.java结尾的文本文件,字节码文件是将Java源文
3、件经过Java编译器编译后的文件,字节码文件不能直接运行,只能运行于Java虚拟机之上。6.Java虚拟机是什么?它有作用是什么?Java虚拟机是一个想象中的机器,在实际的计算机上通过软件模拟来实现。Java语言的一个非常重要的特点就是与平台的无关性。而使用Java虚拟机是实现这一特点的关键。一般的高级语言如果要在不同的平台上运行,至少需要编译成不同的目标代码。而引入Java语言虚拟机后,Java语言在不同平台上运行时不需要重新编译。Java语言使用模式Java虚拟机屏蔽了与具体平台相关的信息,使得Java语言编译程序只需生成在Java虚拟机上运行的目标代码(字节码),就可以在多种平台上不加修
4、改地运行。Java虚拟机在执行字节码时,把字节码解释成具体平台上的机器指令执行。7.描述对象声明和对象生成之间的区别。使用内存状态图来说明这种区别对象声明是为对象的引用创建一个空间,而对象生成则是创建一个类的实例,即为对象分配空间,如果需要的话,其还会将对象空间的地址赋给其应用。如 Tester t1;t1t1 = new Tester();t1 :Tester8.编写Java应用程序,用一个对话框显示当前时间import javax.swing.*;import java.util.*;public class Test public static void main(String args
5、) Date today = new Date( );JOptionPane.showMessageDialog(null,today);9.下面的代码段会有什么样的输出:class Q2mainpublic static void main(String args) QuestionTwo q2;q2= new QuestionTwo();q2.init();q2.increment();q2.increment();System.out.println(q2.getCount();class QuestionTwoprivate int count;public void init()co
6、unt=1;public void increment()count=count+1;public int getCount()return count;输出结果:310.编写可以根据用户的年龄和身高给出推荐的体重的程序,利用下面的公式计算出推荐的体重:recommandedWeight=(height -100 + age/10)*0.9定义名为Height(身高)的公共服务类,他应该有可以根据身高得出推荐体重的方法public class Testpublic static void main(String args)Weight w1 = new Weight();System.out.
7、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) xy & yx:falseb) (xy+z) & (x+10=20):truec) !(xy+z) | !(x+10=20):falsed) !(x=y) & (x!=y) & (xy | yx):true2.用switch语
8、句重写下面的if语句。selection=Inter.parseInt(JOptionPane.showInputDialog(null,”Enter selection:”);if (selection=0)System.out.println(“You selected Magenta”);else if (selection=1)System.out.println(“You selected Red”);else if (selection=2)System.out.println(“You selected Blue”);else if (selection=3)System.out
9、.println(“You selected Green”);else System.out.println(“Invalid selection”);改写为:selection=Inter.parseInt(JOptionPane.showInputDialog(null,”Enter selection:”);swith (selection)case 0:System.out.println(“You selected Magenta”);break;case 1:System.out.println(“You selected Red”);break;case 2:System.out
10、.println(“You selected Blue”);break;case 3:System.out.println(“You selected Green”);break;default: System.out.println(“Invalid selection”);)3.画出下面两个switch语句的控制流程图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:
11、c=2;default: d=3;a)b)4.分别用for、do-while和while语句计算下面的累加和:a)1+1/2+1/3+1/4+1/15for循环:int x;double result=0.0F;for(double i=1;i=15;i+)result+=1/i;System.out.println(result);do-while循环:double x=1;double result=0.0F;doresult+=1/x;x+;while(x=15);System.out.println(result);while循环:double x=1;double result=0.
12、0F;while(x=15)result+=1/x;x+; ;System.out.println(result);5+10+15+50for循环:int x;int result=0;for(int i=1;i=10;i+)result+=i*5;System.out.println(result);do-while循环:int x=1;int result=0;doresult+=x*5; x+;while(x=10);System.out.println(result);while循环:int x=1;int result=0;while(x=10)result+=x*5; x+;Sys
13、tem.out.println(result);5.编写一个计算闰年的程序,要求用户输入一个年份,如果用户输入的年份不在03000年内则给予用户提示要求其重新输入,否则判断该年份是否为闰年并返回结果。public class Testpublic 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
14、LeapYearpublic boolean computeLeapYear(int year)if (year % 4 = 0 & year % 100 != 0 ) return true;if (year % 100 = 0 & year % 400 = 0 ) return true;return false;6.下列哪一组重载方法是不合法的?a)public void compute(int num)public int compute(int num)b)public void move(double length)public void move()c)public int adjust(double amount)public void adjust(double amount , double charge)d)public void doWork() public void doWork(String name)public int doWork(double num)第 a)组7.完成下面这个类中的前四个构造方法。其中每一构造方法都是用关键字this调用第五个构造方法:class Catprivate static final
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1