老师要求张浩使用面向对象的思想编写一个计算器类(Calculator),可以实现两个整数的加减乘除的运算.java
展开全部
Java程序:
public class Main002 {
public static void main(String[] args) {
Calculator c1 = new Calculator(2);
Calculator c2 = new Calculator(3);
Calculator c3;
c3 = c1.add(c2);
System.out.println("c1 + c2 = " + c3.toString());
c3 = c1.subtract(c2);
System.out.println("c1 - c2 = " + c3.toString());
c3 = c1.product(c2);
System.out.println("c1 * c2 = " + c3.toString());
c3 = c1.divide(c2);
System.out.println("c1 / c2 = " + c3.toString());
}
}
//计算器类:Calculator,可以实现两个整数的加减乘除的运算
class Calculator {
protected int num;
//构造方法
public Calculator() {
num = 0;
}
//构造方法
public Calculator(int num) {
this.num = num;
}
//两个整数的相加运算
public Calculator add(Calculator c) {
return new Calculator(this.num + c.num);
}
//两个整数的相减运算
public Calculator subtract(Calculator c) {
return new Calculator(this.num - c.num);
}
//两个整数的相乘运算
public Calculator product(Calculator c) {
return new Calculator(this.num * c.num);
}
//两个整数的相除运算
public Calculator divide(Calculator c) {
return new Calculator(this.num / c.num);
}
public String toString() {
return String.valueOf(num);
}
}
运行测试:
c1 + c2 = 5
c1 - c2 = -1
c1 * c2 = 6
c1 / c2 = 0
展开全部
public class Calculator {
/**
* 加法
*/
public static int jia(int a , int b){
return a+b;
}
/**
* 减法
*/
public static int jian(int a , int b){
return a-b;
}
/**
* 乘法
*/
public static int cheng(int a , int b){
return a*b;
}
/**
* 除法
*/
public static int chu(int a , int b){
return a/b;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
System.out.println(Calculator.jia(5, 6)); // 打印 5 + 6
System.out.println(Calculator.cheng(5, 6)); // 打印 5 * 6
}
}
/**
* 加法
*/
public static int jia(int a , int b){
return a+b;
}
/**
* 减法
*/
public static int jian(int a , int b){
return a-b;
}
/**
* 乘法
*/
public static int cheng(int a , int b){
return a*b;
}
/**
* 除法
*/
public static int chu(int a , int b){
return a/b;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
System.out.println(Calculator.jia(5, 6)); // 打印 5 + 6
System.out.println(Calculator.cheng(5, 6)); // 打印 5 * 6
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Calculator {
public final int PLUS = 0;
public final int MINUS = 1;
public final int MULTIPLY = 2;
public final int DIVIDE = 3;
int num_1;
int num_2;
int type;
public int calculate(int num_1, int num_2, int type) {
this.num_1 = num_1;
this.num_2 = num_2;
this.type = type;
int result;
switch (type) {
case PLUS: result = plus();
case MINUS: result = minus();
case MUTIPLY: result = multiply();
case DIVIDE: result = divide();
}
System.out.println(result);
return result;
}
public int plus() {
return num_1 + num_2;
}
public int minus() {
return num_1 - num_2;
}
public int mutiply() {
return num_1 * num_2;
}
public int divide() {
return num_1 / num_2;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class Calculator{
public int jia(int a ,int b)
{
return a+b;}
public int jia(int a ,int b)
{
return a-b;}
public int jia(int a ,int b)
{
return a*b;}
public double jia(int a ,int b)
{
return a/b;}
}
public int jia(int a ,int b)
{
return a+b;}
public int jia(int a ,int b)
{
return a-b;}
public int jia(int a ,int b)
{
return a*b;}
public double jia(int a ,int b)
{
return a/b;}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要求直接能运行的么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询