作业帮 > 综合 > 作业

实验二:编写一个MyMath类,具有low、high两个整数属性,添加两个构造方法,一个带一个整数参数,用于设置high

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/30 12:33:23
实验二:编写一个MyMath类,具有low、high两个整数属性,添加两个构造方法,一个带一个整数参数,用于设置high的值,并将low设为1;另一个带两个整数参数,用于设置low和high值.为该类添加两个sum()和product()两个方法,分别用于求low,low+1,…high-1,high的和与积,并写好main方法测试该类.
class MyMath {
int low,high;
MyMath(int h) { this(1,h); }
MyMath(int l,int h) { low = l; high = h; }
int sum() { return (low + high) * (high - low + 1) / 2; }
int product() {
int p = 1;
for (int x = low; x