JAVA期末速成库(12)第十三章

一、习题介绍

第十三章

Check Point:P501 13.3,13.17,13.28,13.29

Programming Exercise:13.1,13.6,13.11

二、习题及答案

Check Point:

13.3

True or false?

a. An abstract class can be used just like a nonabstract class except that you cannot

use the new operator to create an instance from the abstract class.

b. An abstract class can be extended.

c. A subclass of a nonabstract superclass cannot be abstract.

d. A subclass cannot override a concrete method in a superclass to define it as abstract.

e. An abstract method must be nonstatic.

13.3对还是错?

a.抽象类可以像非抽象类一样使用,只是不能

使用new操作符从抽象类创建实例。

b.抽象类可以被扩展。

c.非抽象超类的子类不能是抽象的。

d.子类不能重写父类中的具体方法来将其定义为抽象。

e.抽象方法必须是非静态的。

答:a. 对:抽象类不能使用 new 操作符直接创建实例,但可以被其他类继承。

    b. 对:抽象类被其他类继承

    c、d 错:非抽象的子类可以是抽象的,也可以重写父类中的具体方法,但不可以将其定义为抽象方法。

    e. 对:抽象方法必须是非静态的,因为抽象方法没有实现,而静态方法不能被子类覆盖。

13.17 True or false? If a class implements Comparable, the object of the class can invoke the compareTo method.

13.17对还是错?如果一个类实现了Comparable,则该类的对象可以调用compareTo方法。

答:对:如果一个类实现了 Comparable 接口,那么该类的对象可以调用 compareTo 方法,用于比较对象。

13.28

Define the terms abstract classes and interfaces. What are the similarities and differ

ences between abstract classes and interfaces?

13.28定义术语抽象类和接口有什么相似之处和不同之处?抽象类和接口之间的关系?

答:抽象类:不能被实例化,可以包含抽象方法和具体方法,可以有成员变量。

接口:完全抽象,不能包含具体的实现,所有的方法默认都是抽象的,可以有默认方法和静态方法,从Java 8开始。

相似之处:都可以被其他类实现或继承,都可以定义抽象方法。

不同之处:抽象类可以提供部分实现,接口完全抽象;一个类可以实现多个接口,但只能继承一个抽象类;接口中的方法默认是public的,抽象类中的方法可以有多种访问修饰符。

13.29 True or false?

a. An interface is compiled into a separate bytecode file.

b. An interface can have static methods.

c. An interface can extend one or more interfaces.

d. An interface can extend an abstract class.

e. An abstract class can extend an interface.

13.29对还是错?

a.将接口编译成单独的字节码文件。

b.接口可以有静态方法。

c.一个接口可以扩展一个或多个接口。

d.接口可以扩展抽象类。

e.抽象类可以扩展接口。

答:a. 对:接口编译后会生成单独的字节码文件。

    b. 对:接口可以有静态方法,从Java 8开始。

    c. 对:一个接口可以扩展一个或多个接口。

    d. 错:接口不能扩展抽象类,但抽象类可以实现一个或多个接口

    e. 对:抽象类可以实现接口。

Programming Exercise:

**13.1 (Triangle class)

Design a new Triangle class that extends the abstractGeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

**13.1 (Triangle类)设计一个新的Triangle类来扩展抽象类GeometricObject类。为类Triangle绘制UML图和GeometricObject,然后实现Triangle类。编写测试程序,提示用户输入三角形的三条边,一种颜色和一个布尔值,指示三角形是否被填充。程序应该创建用这些边创建一个三角形对象,并设置颜色和填充属性输入。程序应该显示区域、周长、颜色和真或假表示是否已填充。

abstract class GeometricObject {public abstract double getArea();public abstract double getPerimeter();}class Triangle extends GeometricObject {private double side1, side2, side3;private String color;private boolean filled;public Triangle(double side1, double side2, double side3, String color, boolean filled) {this.side1 = side1;this.side2 = side2;this.side3 = side3;this.color = color;this.filled = filled;}@Overridepublic double getArea() {// 计算三角形面积的公式}@Overridepublic double getPerimeter() {return side1 + side2 + side3;}// Getters and setters}

*13.6 (The ComparableCircle class) Define a class named ComparableCircle

that extends Circle and implements Comparable. Draw the UML diagram and

implement the compareTo method to compare the circles on the basis of area.

Write a test class to find the larger of two instances of ComparableCircle objects.

*13.6 (comparablecycle类)

定义一个名为comparablecycle的类。

它扩展Circle并实现Comparable。

绘制UML图和实现compareTo方法,根据面积对圆进行比较。

编写一个测试类来查找ComparableCircle对象的两个实例中较大的一个。

class Circle {private double radius;public Circle(double radius) {this.radius = radius;}public double getArea() {return Math.PI * radius * radius;}// Other methods}class ComparableCircle extends Circle implements Comparable<ComparableCircle> {public ComparableCircle(double radius) {super(radius);}@Overridepublic int compareTo(ComparableCircle other) {return Double.compare(this.getArea(), other.getArea());}}// 测试类逻辑// 创建两个 ComparableCircle 对象// 使用 compareTo 方法找出面积较大的圆

*13.11 (The Octagon class) Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal length. The area can be computed using the following formula:

area = (2 + 4/22)* side * side

Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable. Write a test program that creates an Octagon object with side value 5 and displays its area and perimeter. Create a new object using the clone method and compare the two objects using the compareTo method.

*13.11 (Octagon类)编写一个名为Octagon的类来扩展对可比性和可克隆性的脸。假设八边形的八个边都是等长。这个区域可以用下式计算:

面积=(2 + 4/22)*边*边

绘制UML图,包括八边形、几何对象、可比较,可克隆。编写一个测试程序,创建一个八边形,边值为5的对象,并显示其面积和周长。创建一个新对象使用clone方法并使用compareTo比较两个对象方法。

class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable {private double side;public Octagon(double side) {this.side = side;}@Overridepublic double getArea() {return (2 + (4 * Math.sqrt(2))) * side * side;}@Overridepublic double getPerimeter() {return 8 * side;}@Overridepublic int compareTo(Octagon other) {return Double.compare(this.getArea(), other.getArea());}@Overridepublic Octagon clone() {try {return (Octagon) super.clone();} catch (CloneNotSupportedException e) {// Handle exception}return null;}// Other methods}// 测试程序逻辑// 创建一个边长为5的 Octagon 对象// 显示其面积和周长// 使用 clone 方法创建新对象// 使用 compareTo 方法比较两个对象

 结语 

相信自己的力量

相信自己的梦想

因为你拥有改变世界的能力

!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/863660.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Nature Climate Change | 中国科学院地理资源所吴朝阳课题组发表生物多样性调控植被物候的研究成果!

本文首发于“生态学者”微信公众号&#xff01; 植被春季物候对气候变化的响应通常是通过测量其温度敏感性&#xff08;ST&#xff0c;温度每升高1度&#xff0c;植被提前展叶的天数&#xff09;来量化。ST是植被在当地历史气候环境的选择压力下演化形成的最优策略&#xff0c;…

如何在Ubuntu20上离线安装joern(包括sbt和scala)

在Ubuntu 20上离线安装Joern&#xff0c;由于Joern通常需要通过互联网从其官方源或GitHub等地方下载&#xff0c;但在离线环境中&#xff0c;我们需要通过一些额外的步骤来准备和安装。&#xff08;本人水平有限&#xff0c;希望得到大家的指正&#xff09; 我们首先要做的就是…

在QGIS中调用天地图

2019年 1月 1日起&#xff0c;天地图 API及服务接口调用需要获得开发授权&#xff0c;之前使用 QGIS等 GIS软件无法继续调用天地图&#xff0c;这就需要申请一个许可。 一、注册并申请 Key 具体申请可以登录如下地址&#xff1a;https://www.tianditu.gov.cn打开上述网址后点…

工厂方法模式:概念与应用

目录 工厂方法模式工厂方法模式结构工厂方法适合的应用场景工厂方法模式的优缺点练手题目题目描述输入描述输出描述**提示信息**解题&#xff1a; 工厂方法模式 工厂方法模式是一种创建型设计模式&#xff0c; 其在父类中提供一个创建对象的方法&#xff0c; 允许子类决定实例…

SQLite3的使用

14_SQLite3 SQLite3是一个嵌入式数据库系统&#xff0c;它的数据库就是一个文件。SQLite3不需要一个单独的服务器进程或操作系统&#xff0c;不需要配置&#xff0c;这意味着不需要安装或管理&#xff0c;所有的维护都来自于SQLite3软件本身。 安装步骤 在Linux上安装SQLite…

《概率论与数理统计》期末复习笔记_下

目录 第4章 随机变量的数字特征 4.1 数学期望 4.2 方差 4.3 常见分布的期望与方差 4.4 协方差与相关系教 第5章 大数定律和中心极限定理 5.1 大数定律 5.2 中心极限定理 第6章 样本与抽样分布 6.1 数理统汁的基本概念 6.2 抽样分布 6.2.1 卡方分布 6.2.2 t分布 6.…

Winform使用HttpClient调用WebApi的基本用法

Winform程序调用WebApi的方式有很多&#xff0c;本文学习并记录采用HttpClient调用基于GET、POST请求的WebApi的基本方式。WebApi使用之前编写的检索环境检测数据的接口&#xff0c;如下图所示。 调用基于GET请求的无参数WebApi 创建HttpClient实例后调用GetStringAsync函数获…

技术打包 催化剂浸渍制作方法设备

网盘 https://pan.baidu.com/s/1Bybbyy5qEA2uTUlaELmWwg?pwdepdk 改性加氢处理催化剂载体、催化剂及其制备方法和应用.pdf 水滑石基催化剂在高浓度糖转化到1,2-丙二醇中的应用.pdf 海泡石负载铁锰双金属催化剂及其制备方法和应用.pdf 甘油氢解催化剂及其制备方法和应用.pdf 用…

【原理】机器学习中的最小二乘法公式推导过程

本文来自《老饼讲解-BP神经网络》https://www.bbbdata.com/ 目录 一、什么是最小二乘法1.1. 什么是最小二乘法1.2. 最小二乘法的求解公式 二、最小二乘法求解公式的推导 最小二乘法是基本的线性求解问题之一&#xff0c;本文介绍最小二乘法的原理&#xff0c;和最小二法求解公式…

【Vue】Vue3基础

VUE3基础 1、简介2、创建工程2.1 基于vue-cli创建&#xff08;脚手架webpack&#xff09;2.2 基于vite创建&#xff08;推荐&#xff09;2.3 目录结构2.4 vscode插件推荐 3、核心语法3.1 选项式&#xff08;options API&#xff09;和组合式&#xff08;composition API&#x…

Arduino - LED 矩阵

Arduino - LED 矩阵 Arduino - LED Matrix LED matrix display, also known as LED display, or dot matrix display, are wide-used. In this tutorial, we are going to learn: LED矩阵显示器&#xff0c;也称为LED显示器&#xff0c;或点阵显示器&#xff0c;应用广泛。在…

scatterlist的相关概念与实例分析

概念 scatterlist scatterlist用来描述一块内存&#xff0c;sg_table一般用于将物理不同大小的物理内存链接起来&#xff0c;一次性送给DMA控制器搬运 struct scatterlist {unsigned long page_link; //指示该内存块所在的页面unsigned int offset; //指示该内存块在页面中的…

纯硬件FOC驱动BLDC

1. 硬件FOC 图 1 为采用 FOC 的方式控制 BLDC 电机的过程&#xff0c;经由 FOC 变换( Clark 与 Park 变换) &#xff0c;将三相电流转换为空间平 行电流 ID 与空间垂直电流 IQ。经过 FOC 逆变化逆( Clark 变换与逆 Park 变换) &#xff0c;将两相电流转换为三相电流用于控 制电…

喜茶新品被迫更名,内容营销专家刘鑫炜谈品牌定位敏锐度和适应性

喜茶&#xff0c;作为茶饮界的知名品牌&#xff0c;一直以其独特的创意和优质的产品受到消费者的喜爱。然而&#xff0c;近期喜茶推出的一款新品“小奶栀”却因其名称发音问题引发了不小的争议。 事件回顾 “小奶栀”这款新品在上市之初&#xff0c;以其独特的口感和创新的命名…

气膜结构的年度维护费用解析—轻空间

气膜结构作为一种新型建筑形式&#xff0c;广泛应用于体育场馆、仓储、展览馆等场所。由于其独特的结构特点&#xff0c;气膜建筑的维护工作显得尤为重要。轻空间将详细探讨气膜结构的年度维护费用构成及影响因素&#xff0c;帮助大家全面了解气膜建筑的运营成本。 气膜结构年度…

android studio 添加aar包

按着以前旧的导包方式栽了大跟头&#xff0c;后面在留老板的的博客下找到了解决办法&#xff0c;记录一下。 Andriod Studio 导入aar最新的方式_gradle 8 引入arr-CSDN博客 最新导包方式 1.在新建libs目录&#xff0c;在app/libs目录下导入aar包&#xff08;其实就是拷贝过去…

揭秘品牌推广的制胜之道:步骤、流程、方法与技巧全攻略!

品牌推广是现代营销战略中的核心环节&#xff0c;对于提升品牌知名度、塑造品牌形象以及扩大市场份额具有举足轻重的作用。 作为一名手工酸奶品牌的创始人&#xff0c;目前全国复制了100多家门店&#xff0c;我来为大家分享品牌推广的制胜之道&#xff0c;包括具体步骤、流程、…

STM32的EXTI简介

一&#xff0c;EXTI&#xff08;External Interrupt&#xff09;外部中断事件控制器 什么是EXTI&#xff1f; 1.监测指定的GPIO口的电平信号变化&#xff0c;并检测到指定条件时&#xff0c;向内核的中断控制器NVIC发出中断申请。NVIC在裁决后&#xff0c;如果满足条件&#xf…

pytest-自动执行固件

目前为止&#xff0c;所有固件的使用都是手动指定&#xff0c;或者作为参数&#xff0c;或者使用 usefixtures。 如果我们想让固件自动执行&#xff0c;可以在定义时指定 autouse 参数。 下面是两个自动计时固件&#xff0c;一个用于统计每个函数运行时间&#xff08;functio…

【自然语言处理】司法阅读理解

司法阅读理解 1 任务目标 1.1 任务说明 裁判文书中包含了丰富的案件信息&#xff0c;比如时间、地点、人物关系等等&#xff0c;通过机器智能化地阅读理解裁判文书&#xff0c;可以更快速、便捷地辅助法官、律师以及普通大众获取所需信息。 本次任务覆盖多种法律文书类型&am…