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

问题描述:

实验二:编写一个MyMath类,具有low、high两个整数属性,添加两个构造方法,一个带一个整数参数,用于设置high的值,并将low设为1;另一个带两个整数参数,用于设置low和high值.为该类添加两个sum()和product()两个方法,分别用于求low,low+1,…high-1,high的和与积,并写好main方法测试该类.
1个回答 分类:综合 2014-10-04

问题解答:

我来补答
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
 
 
展开全文阅读
剩余:2000