{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "7896b88027e34a4ea6de980d35e10418", "keywords": "算法初阶,基础知识,分治策略,用主方法求解递归式", "title": "模拟计算器", "desc": [ { "content": "\n模拟简单的计算器。\n要求:\n(1)定义名为Number的类,在其中定义两个私有的整型数据成员n1和n2;\n(2)在Number类中编写构造方法,赋予n1和n2初始值;\n(3)再为Number类定义加(addition)、减(subtration)、乘(multiplication)、除(division)四个公有成员方法,分别对两个成员变量执行加、减、乘、除的运算。注意:除法运算时要抛出除数为0的异常。\n(4)在主方法中创建Number类的对象,调用上述各个方法,测试并输出计算结果,注意进行必要的异常捕获与处理。", "language": "markdown" } ], "answer": [ { "content": "", "language": "java" } ], "prepared": [ [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ] ], "template": { "content": "public class Number {\n\tprivate int n1;\n\tprivate int n2;\n\tpublic Number(int n1, int n2) {\n\t\tthis.n1 = n1;\n\t\tthis.n2 = n2;\n\t}\n\tpublic int addition() {\n\t\treturn n1 + n2;\n\t}\n\tpublic int subtration() {\n\t\treturn n1 - n2;\n\t}\n\tpublic int multiplication() {\n\t\treturn n1 * n2;\n\t}\n\tprivate int division() {\n\t\tif (n2 == 0) {\n\t\t\tthrow new IllegalArgumentException(\"除数参数不合法\");\n\t\t}\n\t\treturn n1 / n2;\n\t}\n\tpublic static void main(String[] args) {\n\t\tNumber number = new Number(6, 0);\n\t\tSystem.out.println(number.addition());\n\t\tSystem.out.println(number.subtration());\n\t\tSystem.out.println(number.multiplication());\n\t\ttry {\n\t\t\tSystem.out.println(number.division());\n\t\t} catch (Exception e) {\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t}\n}", "language": "java" }, "node_id": "dailycode-a97f6434fd15477d982f9ef33297caff", "license": "csdn.net", "created_at": 1637894161, "topic_link": "https://bbs.csdn.net/topics/600469981" }