[3].设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用computer方法完成运算 2.输出运算的结果

时间: 2024-09-28 11:10:28 浏览: 70
在Java中,你可以设计一个名为`UseCompute`的类,这个类的作用是为了通用地利用一个实现了特定运算接口(如`Compute`)的对象来进行计算,并显示结果。假设`Compute`接口定义了一个`compute(int a, int b)`方法用于执行某种运算。下面是一个简单的实现: ```java // 定义Compute接口 interface Compute { int compute(int one, int two); } // 实现UseCompute类 class UseCompute { // 使用Compute对象并调用compute方法 public void useCom(Compute com, int one, int two) { int result = com.compute(one, two); System.out.println("运算结果是: " + result); } } ``` 在这个`useCom`方法中,我们接受一个`Compute`类型的参数`com`,然后通过`com.compute()`来调用具体的计算方法。无论`com`实际是什么类的实例,只要它遵守了`Compute`接口的规范,就能保证正确的运行。
相关问题

利用接口做参数,写个计算器,能完成加减乘除运算。 1.定义一个接口Compute含有一个方法int computer(int n, int m)。 2.设计四个类分别实现此接口,完成加减乘除运算。 3.设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。 4.设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。

好的,以下是代码实现: 1. 定义Compute接口 ```java public interface Compute { int computer(int n, int m); } ``` 2. 实现加减乘除四个类 ```java public class Add implements Compute { @Override public int computer(int n, int m) { return n + m; } } public class Sub implements Compute { @Override public int computer(int n, int m) { return n - m; } } public class Mul implements Compute { @Override public int computer(int n, int m) { return n * m; } } public class Div implements Compute { @Override public int computer(int n, int m) { if (m == 0) { throw new IllegalArgumentException("除数不能为0"); } return n / m; } } ``` 3. 设计UseCompute类 ```java public class UseCompute { public void useCom(Compute com, int one, int two) { int result = com.computer(one, two); System.out.println("运算结果为:" + result); } } ``` 4. 设计Test主类 ```java public class Test { public static void main(String[] args) { UseCompute uc = new UseCompute(); int a = 10; int b = 5; Compute add = new Add(); Compute sub = new Sub(); Compute mul = new Mul(); Compute div = new Div(); uc.useCom(add, a, b); uc.useCom(sub, a, b); uc.useCom(mul, a, b); uc.useCom(div, a, b); } } ``` 这样就可以通过传递不同的Compute对象来完成不同的加减乘除运算了。

java利用接口做参数,写个计算器,能完成加减乘除运算。 (1)定义一个接口Compute含有一个方法int computer(int n, int m)。 (2)设计四个类分别实现此接口,完成加减乘除运算。 (3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。 (4)设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。

代码如下: 接口Compute: ``` public interface Compute { int computer(int n, int m); } ``` 实现加法的类Add: ``` public class Add implements Compute { public int computer(int n, int m) { return n + m; } } ``` 实现减法的类Sub: ``` public class Sub implements Compute { public int computer(int n, int m) { return n - m; } } ``` 实现乘法的类Mul: ``` public class Mul implements Compute { public int computer(int n, int m) { return n * m; } } ``` 实现除法的类Div: ``` public class Div implements Compute { public int computer(int n, int m) { if (m == 0) { throw new IllegalArgumentException("除数不能为0"); } return n / m; } } ``` 使用接口Compute的类UseCompute: ``` public class UseCompute { public void useCom(Compute com, int one, int two) { int result = com.computer(one, two); System.out.println("运算结果为:" + result); } } ``` 主类Test: ``` public class Test { public static void main(String[] args) { UseCompute useCompute = new UseCompute(); Compute add = new Add(); Compute sub = new Sub(); Compute mul = new Mul(); Compute div = new Div(); useCompute.useCom(add, 10, 5); useCompute.useCom(sub, 10, 5); useCompute.useCom(mul, 10, 5); useCompute.useCom(div, 10, 5); } } ``` 输出结果为: ``` 运算结果为:15 运算结果为:5 运算结果为:50 运算结果为:2 ```
阅读全文

相关推荐

Write a C++ program that defines a class DateV2 that (1) Contains all the members in the class DateV1; Programming for Engineers C++ (2) Has two constructors as follows: One takes three parameters, int y, int m, int n; The other is the default constructor that takes no parameter (3) Has additional public member functions as follows: string getWeekDay(); // return the week day, for example, Sunday if day is 0, etc bool Leap(); // return if the year is leap int differFrom(DateV2& oneDate); // return the difference in days between the calling object // and the oneDate object void printDate(); // print the year, the month in English, the day, and the week day Test class DateV2 in the main function as follows: (1) Declare and set the objects today and tomorrow as in Problem 2. (2) Declare and initialize (by a constructor) an object to represent your OWN birthday. (3) Use the member function printDate to print today, tomorrow, and your birthday. (4) Output the weekday of today, tomorrow, and your own birthday. (5) Output how many days has passed since your birth (the difference between your birthday and today). Hint: i) We can use another string array to store the English name for week days (Sunday, Monday, through Saturday) ii) We know that it is Monday on Year 1, Month 1, and Day 1 iii) A good idea is to first design a function to compute the number of days that has passed since Year 1, Month 1, and Day 1, and then to use this function to compute the week day for a give date and to compute the difference between two dates. You can store the number of days for each of the 12 months in an integer array, which helps in counting the days.

Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks: (a) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d)Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers . Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal. 用c++5.11寫出,且使用using namespace std;

Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks:(a) (5%) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) (5%) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) (5%) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d) (5%) Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers (5%). Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal. 用c++寫,並用using namespace std;

最新推荐

recommend-type

无人机.zip

航天模拟器文件、蓝图、代码
recommend-type

ASP.NET MVC 程序设计.zip(毕设&课设&实训&大作业&竞赛&项目)

项目工程资源经过严格测试运行并且功能上ok,可实现复现复刻,拿到资料包后可实现复现出一样的项目,本人系统开发经验充足(全栈全领域),有任何使用问题欢迎随时与我联系,我会抽时间努力为您解惑,提供帮助 【资源内容】:包含源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可实现复现;设计报告也可借鉴此项目;该资源内项目代码都经过测试运行;功能ok 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 【提供帮助】:有任何使用上的问题欢迎随时与我联系,抽时间努力解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 下载后请首先打开说明文件(如有);整理时不同项目所包含资源内容不同;项目工程可实现复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用
recommend-type

全国国土利用现状、耕地分布、园地分布、林地分布等三调专题图PDF PNG分享

主要包括中国国土利用现状、耕地分布、园地分布、林地分布、草地分布、湿地分布、城镇村及工矿用地分布、交通运输用地分布、水域及水利设施用地分布。 全国国土利用现状、耕地分布、园地分布、林地分布等三调专题图,是基于2018年9月启动的第三次全国国土调查(简称“三调”)成果制作的。这些专题图采用了优于1米分辨率的卫星遥感影像,并广泛应用了移动互联网、云计算等新技术,确保了数据的准确性和现势性。专题图集涵盖了全国的国土利用现状,包括耕地、园地、林地等各类土地资源的分布情况,为科学决策、资源保护和合理利用提供了有力支持。PDF和PNG格式的专题图。
recommend-type

交通警务-Android-基于安卓的交通警务系统设计与实现

1. 用户管理 用户注册与登录:支持通过手机号码或身份证进行注册和登录,确保用户身份的真实性。 权限管理:根据不同角色(如交警、管理员、公众等)设置相应的权限,保障系统安全。 2. 交通违法信息管理 违法记录查询:用户可以查询个人的交通违法记录,包括违法时间、地点、类型和罚款金额。 违法行为投诉:用户可对交通违章行为进行举报,并上传相关证据(如照片视频)。 3. 交通事故处理 事故报告提交:用户可以在线填写事故报告,上传相关照片和证据,以便快速处理。 现场处理指导:交警可通过系统为事故现场提供处理步骤和注意事项,提升事故处理效率。 4. 车辆管理 车辆信息登记:支持用户登记机动车的信息,包括型号、车牌号、注册日期等。 年检提醒:系统定期提醒用户进行车辆年检和保险续费。 5. 交通数据统计与分析 交通流量监测:通过实时数据分析,提供交通流量、拥堵情况的监测与预测。 违法行为统计:生成各类交通违法行为的统计报告,帮助交警部门制定改善措施。 6. 路况信息发布 实时路况更新:提供最新的交通路况信息,包括拥堵、施工、事故等情况。 交通公告推送:系统可以向用户推送重要的交通公告,如道路封闭、临时
recommend-type

仿京细菜谱微信小程序源码云开发菜谱微信小程序源码.zip

京细菜谱是一个美食分享网站,提供优质的家常菜谱大全,仿京细菜谱小程序源码为喜欢美食的朋友提供了很多的美食烹饪教程 让您轻松学会做美食。对不同食材和地域的饮食做了不同的分类和详细的做菜方法 分类十分详细,八大菜系、特色食品、特殊场合、热门功效、人群细分、烘焙甜品、口味和食材分类的十分详细。 本菜谱小程序源码为云开发版本,不需要域名和服务器即可搭建小程序,直接导入开发者工具即可上传审核。
recommend-type

PHP集成Autoprefixer让CSS自动添加供应商前缀

标题和描述中提到的知识点主要包括:Autoprefixer、CSS预处理器、Node.js 应用程序、PHP 集成以及开源。 首先,让我们来详细解析 Autoprefixer。 Autoprefixer 是一个流行的 CSS 预处理器工具,它能够自动将 CSS3 属性添加浏览器特定的前缀。开发者在编写样式表时,不再需要手动添加如 -webkit-, -moz-, -ms- 等前缀,因为 Autoprefixer 能够根据各种浏览器的使用情况以及官方的浏览器版本兼容性数据来添加相应的前缀。这样可以大大减少开发和维护的工作量,并保证样式在不同浏览器中的一致性。 Autoprefixer 的核心功能是读取 CSS 并分析 CSS 规则,找到需要添加前缀的属性。它依赖于浏览器的兼容性数据,这一数据通常来源于 Can I Use 网站。开发者可以通过配置文件来指定哪些浏览器版本需要支持,Autoprefixer 就会自动添加这些浏览器的前缀。 接下来,我们看看 PHP 与 Node.js 应用程序的集成。 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时环境,它使得 JavaScript 可以在服务器端运行。Node.js 的主要特点是高性能、异步事件驱动的架构,这使得它非常适合处理高并发的网络应用,比如实时通讯应用和 Web 应用。 而 PHP 是一种广泛用于服务器端编程的脚本语言,它的优势在于简单易学,且与 HTML 集成度高,非常适合快速开发动态网站和网页应用。 在一些项目中,开发者可能会根据需求,希望把 Node.js 和 PHP 集成在一起使用。比如,可能使用 Node.js 处理某些实时或者异步任务,同时又依赖 PHP 来处理后端的业务逻辑。要实现这种集成,通常需要借助一些工具或者中间件来桥接两者之间的通信。 在这个标题中提到的 "autoprefixer-php",可能是一个 PHP 库或工具,它的作用是把 Autoprefixer 功能集成到 PHP 环境中,从而使得在使用 PHP 开发的 Node.js 应用程序时,能够利用 Autoprefixer 自动处理 CSS 前缀的功能。 关于开源,它指的是一个项目或软件的源代码是开放的,允许任何个人或组织查看、修改和分发原始代码。开源项目的好处在于社区可以一起参与项目的改进和维护,这样可以加速创新和解决问题的速度,也有助于提高软件的可靠性和安全性。开源项目通常遵循特定的开源许可证,比如 MIT 许可证、GNU 通用公共许可证等。 最后,我们看到提到的文件名称 "autoprefixer-php-master"。这个文件名表明,该压缩包可能包含一个 PHP 项目或库的主分支的源代码。"master" 通常是源代码管理系统(如 Git)中默认的主要分支名称,它代表项目的稳定版本或开发的主线。 综上所述,我们可以得知,这个 "autoprefixer-php" 工具允许开发者在 PHP 环境中使用 Node.js 的 Autoprefixer 功能,自动为 CSS 规则添加浏览器特定的前缀,从而使得开发者可以更专注于内容的编写而不必担心浏览器兼容性问题。
recommend-type

揭秘数字音频编码的奥秘:非均匀量化A律13折线的全面解析

# 摘要 数字音频编码技术是现代音频处理和传输的基础,本文首先介绍数字音频编码的基础知识,然后深入探讨非均匀量化技术,特别是A律压缩技术的原理与实现。通过A律13折线模型的理论分析和实际应用,本文阐述了其在保证音频信号质量的同时,如何有效地降低数据传输和存储需求。此外,本文还对A律13折线的优化策略和未来发展趋势进行了展望,包括误差控制、算法健壮性的提升,以及与新兴音频技术融合的可能性。 # 关键字 数字音频编码;非均匀量化;A律压缩;13折线模型;编码与解码;音频信号质量优化 参考资源链接:[模拟信号数字化:A律13折线非均匀量化解析](https://wenku.csdn.net/do
recommend-type

arduino PAJ7620U2

### Arduino PAJ7620U2 手势传感器 教程 #### 示例代码与连接方法 对于Arduino开发PAJ7620U2手势识别传感器而言,在Arduino IDE中的项目—加载库—库管理里找到Paj7620并下载安装,完成后能在示例里找到“Gesture PAJ7620”,其中含有两个示例脚本分别用于9种和15种手势检测[^1]。 关于连线部分,仅需连接四根线至Arduino UNO开发板上的对应位置即可实现基本功能。具体来说,这四条线路分别为电源正极(VCC),接地(GND),串行时钟(SCL)以及串行数据(SDA)[^1]。 以下是基于上述描述的一个简单实例程序展示如
recommend-type

网站啄木鸟:深入分析SQL注入工具的效率与限制

网站啄木鸟是一个指的是一类可以自动扫描网站漏洞的软件工具。在这个文件提供的描述中,提到了网站啄木鸟在发现注入漏洞方面的功能,特别是在SQL注入方面。SQL注入是一种常见的攻击技术,攻击者通过在Web表单输入或直接在URL中输入恶意的SQL语句,来欺骗服务器执行非法的SQL命令。其主要目的是绕过认证,获取未授权的数据库访问权限,或者操纵数据库中的数据。 在这个文件中,所描述的网站啄木鸟工具在进行SQL注入攻击时,构造的攻击载荷是十分基础的,例如 "and 1=1--" 和 "and 1>1--" 等。这说明它的攻击能力可能相对有限。"and 1=1--" 是一个典型的SQL注入载荷示例,通过在查询语句的末尾添加这个表达式,如果服务器没有对SQL注入攻击进行适当的防护,这个表达式将导致查询返回真值,从而使得原本条件为假的查询条件变为真,攻击者便可以绕过安全检查。类似地,"and 1>1--" 则会检查其后的语句是否为假,如果查询条件为假,则后面的SQL代码执行时会被忽略,从而达到注入的目的。 描述中还提到网站啄木鸟在发现漏洞后,利用查询MS-sql和Oracle的user table来获取用户表名的能力不强。这表明该工具可能无法有效地探测数据库的结构信息或敏感数据,从而对数据库进行进一步的攻击。 关于实际测试结果的描述中,列出了8个不同的URL,它们是针对几个不同的Web应用漏洞扫描工具(Sqlmap、网站啄木鸟、SqliX)进行测试的结果。这些结果表明,针对提供的URL,Sqlmap和SqliX能够发现注入漏洞,而网站啄木鸟在多数情况下无法识别漏洞,这可能意味着它在漏洞检测的准确性和深度上不如其他工具。例如,Sqlmap在针对 "http://www.2cto.com/news.php?id=92" 和 "http://www.2cto.com/article.asp?ID=102&title=Fast food marketing for children is on the rise" 的URL上均能发现SQL注入漏洞,而网站啄木鸟则没有成功。这可能意味着网站啄木鸟的检测逻辑较为简单,对复杂或隐蔽的注入漏洞识别能力不足。 从这个描述中,我们也可以了解到,在Web安全测试中,工具的多样性选择是十分重要的。不同的安全工具可能对不同的漏洞和环境有不同的探测能力,因此在实际的漏洞扫描过程中,安全测试人员需要选择合适的工具组合,以尽可能地全面地检测出应用中存在的漏洞。 在标签中指明了这是关于“sql注入”的知识,这表明了文件主题的核心所在。SQL注入是一种常见的网络攻击方式,安全测试人员、开发人员和网络管理员都需要对此有所了解,以便进行有效的防御和检测。 最后,提到了压缩包子文件的文件名称列表,其中包含了三个文件:setup.exe、MD5.exe、说明_Readme.html。这里提供的信息有限,但可以推断setup.exe可能是一个安装程序,MD5.exe可能是一个计算文件MD5散列值的工具,而说明_Readme.html通常包含的是软件的使用说明或者版本信息等。这些文件名暗示了在进行网站安全测试时,可能涉及到安装相关的软件工具,以及进行文件的校验和阅读相应的使用说明。然而,这些内容与文件主要描述的web安全漏洞检测主题不是直接相关的。
recommend-type

【GPStoolbox使用技巧大全】:20个实用技巧助你精通GPS数据处理

# 摘要 GPStoolbox是一个广泛应用于GPS数据处理的软件工具箱,它提供了从数据导入、预处理、基本分析到高级应用和自动化脚本编写的全套功能。本文介绍了GPStoolbox的基本概况、安装流程以及核心功能,探讨了如何