湖北民院OJ 构造方法(Java)

题目描述

编写Java程序模拟简单的计算器。定义名为Number的类,其中有两个整型数据成员x和y,声明为私有。编写构造方法赋予x和y初始值,再为该类定义加add、减sub、乘mul、除div等公有成员方法分别对两个成员变量执行加、减、乘、除的运算。 在main方法中创建Number类的对象调用各个方法并显示计算结果。

// 你的代码将嵌入这里
public class Main{
public static void main(String[] args){
Number num=new Number(4,4);
num.add();
num.sub();
num.mul();
num.div();
}
}

输入描述

输出描述

the result is:8
the result is:0
the result is:16
the result is:1

 class Number {
	public int x;
	public int y;
	public  Number(int x, int y) {
		this.x = x;
		this.y = y;
	}
   public void add()
   {
	   System.out.println("the result is:"+(this.x+this.y));
   }
   public void sub()
   {
	   System.out.println("the result is:"+(this.x-this.y));
	   
   }
   public void mul()
   {
	   System.out.println("the result is:"+(this.x*this.y));
	   
   }
   public void div()
   {
	   System.out.println("the result is:"+(this.x/this.y));
	   
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值