面向对象Java编程上机作业2023-上机题目3:类的特性、抽象类和接口_第1页
面向对象Java编程上机作业2023-上机题目3:类的特性、抽象类和接口_第2页
面向对象Java编程上机作业2023-上机题目3:类的特性、抽象类和接口_第3页
面向对象Java编程上机作业2023-上机题目3:类的特性、抽象类和接口_第4页
面向对象Java编程上机作业2023-上机题目3:类的特性、抽象类和接口_第5页
全文预览已结束

下载本文档

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

文档简介

《面向对象Java编程》上机作业3

学号

姓

名

班

级

日

期

1.创建一个名为Rectangle的类,来表示一个使用宽度和高度作为属性的矩形。

矩形的宽度和高度由构造方法来确定(要求长和高的范围为10~50)o为Rectangle

类创建下列方法:

①getArea返回矩形的面积;

②getPerimeter返回矩形的周长;

③drawRect使用星号(衿作为描绘字符画出该矩形的形状(画出的矩形为空

心,边长为宽度和高度去除小数点之后的整数)。

在另一个类TeslReclangle中编写main方法来测试Rectangle类。

>问题分析

>代码实现

》运行结果

:思考及总结

2.Defineaninterface,calledVolumeArea,withinwhichthereVe:

-astaticandfinalvariablePl(withthevalue3.14159),and

-twoabstractmethodsvolume(doubleradius)andarea(doubleradius).

WriteanotherclassMyCirclewhichimplementstheinterfaceVolumeArea.

Overridingthetwomethods:

-volume(doubleradius)toreturnavaluezero(asthevolumeofacircleis0),

and

-area(doubleradius)toreturntheareaofthecircle.

WritethethirdclassMySphere,alsoimplementsVolumeArea.Overridingthetwo

methods:

-volume(doubleradius)tocalculateandreturnthevolumeofthesphere

(using(4K*r*r*r)/3);

-area(doubleradius)tocalculateandreturnthesurfaceareaofthesphere

(using4兀*r*r).

WriteamainclasswiththemainmethodtolestthemethodsinMyCircleand

MySphere,settheradiusofthecircleandspherebothto3.5.Anddisplaytheresults.

>问题分析

>代码实现

>运行结果

>思考及总结

3.按如下要求定义两个类ClassA和ClassB:

-类ClassA中定义一个char类型属性xVar(将其赋值为65)和一个窗口中输

出xVar值的方法myPrint。。

-类ClassB是类ClassA的子类,其中定义一个im类型属性yVar(将其赋值

为16)和String类型的属性strVar(将其赋值为"javaprogram!”);类ClassB中

也定义了一个名为myPrint的方法,其功能为在窗口中输出yVarWstrVar值;

类ClassB中还有一个方法prinlAll(),该方法中分别调用父类和子类的myPrinl。

方法做输出。

编写一个测试类TestClassDriver,在主方法中创建类ClassB的对象bObj,

调用printAll()方法输III对象bObj的属性值。

>问题分析

>代码实现

>运行结果

>思考及总结

4.请根据以下UML图,编写相关的三个类:

GeometricObjectAbstractclassnameisitalicized

-color:String

-filied:boolean

-dateCreated:java.util.Date

The#signindicates

protectedmodifieriKieometricObjectO

#CeometricObject(color:string,

filled:boolean)

+getColor():String

+setColor(color:String):void

+isFilled():boolean

+setFilled(filled:boolean):void

+getDateCreated():java.util.Date

+toString():String

■^getAreaO:double

Abstractmethods

+getPerimeter():double

areitalicizedMethodsgetAreaandgetPerimeterare

overriddeninCircleandRectangle.

Superclassmethodsarcgenerallyomitted

tintheUMI.diagramforsubclasses.

CircleRectangle

-radius:double-width:double

-height:double

+CircleO

+Circle(radius:double)+Rectangle()

+Circle(radius:double,color:string,fRectangle(width:double,height:double)

filled;booled。)+Rectangle(width:double,height:double,

+getRadius():doublecolor:string,filled:boolean)

+setRadiusCradius:double):void+getwidth():double

+getDiameter():double+setWidth(width:double):void

+getHeight():double

+setHeight(height:double):void

另外定义一个坪,main方法中对上图中设计的各个方法进行测试。—

TIPs:符号表示public修饰的属性/方法;,「表示private修饰;#表示protected

修饰;斜体表示abstract修饰的类/方法。

>问题分析

»代码实现

》运行结果

了思考及总结

5.按如下要求,编写应用程序:

•编写一个抽象类Animal,其成员变量有name,age,weight表示动物名、

年龄和重量。方法有showlnfo()、movc()和cat(),其中后面两个方法是抽象方

法;

■编写一个类Bird继承Animal,实现相应的方法,通过构造方法给name,

age,weight分别赋值,showlnfo()打印鸟名、年龄和重量,move()方法打印鸟

的运动方式,eat()打印鸟喜欢吃的食物;

-编写一个类Dog继承Animal,实现相应的方法,通过构造方法给name,

age,weight分别赋值,showlnfo()打印狗名、年龄和重量,move()方法打印狗

的运动方式,eal()打印狗喜欢吃的食物;

-最后编写测试类TestAnimaL用Animal类型的变量,调用Bird和Dog对

象的三个方法。

-要求:将Animal、Bird和Dog三个类定义在包中(包名为你的名字或姓

名缩写),而测试类TcstAnimal在缺省包中。

>问题分析

A代码实现

A运行结果

£思考及总结

6.(Math:TheComplexclass)Acomplexnumberisanumberoftheformwherea

andbarerealnumbersandiisThenumbersaandbareknownastherealpartand

imaginarypartofthecomplexnumber,respectively.Youcanperformaddition,

subtraction,multiplication,anddivisionfbrcomplexnumbersusingthefollowing

formula:

a+bi+c+di=(a+c)+(b+d)i

a+bi-(c+di)=(a-c)+(b-d)i

(a+bi)*(c+di)=(ac—bd)+(be+ad}i

(a+bi)/(c+di)=(ac+bd)/(c2+d2)+(be-ad)i/(c2+J2)

Youcanalsoobtaintheabsolutevalueforacomplexnumberusingthefollowing

formula:

\a+bi\=y/a2+b2

DesignaclassnamedComplexforrepresentingcomplexnumbersandthemethods

add,subtract,multiply,divide,andabsforperformingcomplex-numberoperations,

andoverridethetoStringmethodforreturningastringrepresentationforacomplex

number.ThetoStringmethodreturnsa+biasastring.Ifbis0,itsimplyreturnsa.

ProvidethreeconstructorsComplex(a,b),Complex(a),andComplex

温馨提示

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

评论

0/150

提交评论