编程创建一个Cale计算类,在其中定义2个变量(属性)表示两个操作数,定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)``并创建两个对象,分别测试 。
public class Test{ public static void main(String[] args){ Cale test1 = new Cale(); test1.operand1 = 10; test1.operand2 = 5; double add1 = test1.add(); System.out.println("operand1 + operand2="+add1); double sub1 = test1.sub(); System.out.println("operand1 - operand2="+sub1); double muliti1 = test1.muliti(); System.out.println("operand1 * operand2="+muliti1); double div1 = test1.div(); if (test1.flag){ System.out.println("operand1 / operand2="+div1); }else{ System.out.println("分母不能为0"); } Cale test2 = new Cale(); test2.operand1 = -10; test2.operand2 = 0; double add2 = test2.add(); System.out.println("operand1 + operand2="+add2); double sub2 = test2.sub(); System.out.println("operand1 - operand2="+sub2); double muliti2 = test2.muliti(); System.out.println("operand1 * operand2="+muliti2); double div2 = test2.div(); if (test2.flag){ System.out.println("operand1 / operand2="+div2); }else{ System.out.println("分母不能为0"); } } } class Cale{ double operand1; double operand2; boolean flag = true; public double add(){ return operand1 + operand2; } public double sub(){ return operand1 - operand2; } public double muliti(){ return operand1 * operand2; } public double div(){ if (operand2 == 0){ flag = false; return -1; }else{ return operand1 / operand2; } } }