Question: How can i fix the code? Copy, paste and rename the program from Exercise 2, above, into the same project. Copy and paste copies of
How can i fix the code?
Copy, paste and rename the program from Exercise 2, above, into the same project. Copy and paste copies of the current methods (loopSum and formulaSum) into the same program, after the current methods. Now you will have 4 methods, 2 named loopSum and 2 named formulaSum. Leave one loopSum and one formulaSum method unchanged. You will change the other two into double methods (each using the double type instead of int throughout). They will have the same names as the above (loopSum, formulaSum) but will have different types of parameters and return values. Actually, we only want them to have different return types but in order to overload a method, they must have different parameter types to change their signatures. Simply changing all ints to doubles will do it.
Copy and paste the following main() method, replacing the original main().
public class sumOverload {
public static void main(String[] args) {
int n = 100;
double nDouble = n;
System.out.println("Sums using integer types:");
System.out.println("Sum (using a loop) = " + loopSum(n));
System.out.println("Sum (using the formula) = " + formulaSum(n));
System.out.println("Sums using double types:");
System.out.println("Sum (using a loop) = " + loopSum(nDouble));
System.out.println("Sum (using the formula) = " + formulaSum(nDouble));
}
// Your 4 methods go here. }
For the new double methods, the parameter and the variables in the method will all be type double. In other words, change all int to double. Then the operators will generate double values and the results will be more correct for large values of nDouble. Run the program with values 100 and 1,000,000. The output for 1,000,000 will be wrong in the int methods due to integer overflow. Integer overflow either has been or will be discussed in class.
Below is my code.
public class sumOverload {
public static void main(String[] args) {
int n = 100;
double nDouble = n;
System.out.println("Sums using integer types:");
System.out.println("Sum (using a loop) = " + sum(n));
System.out.println("Sum (using the formula) = " + formulaSum(n));
System.out.println("Sums using double types:");
System.out.println("Sum (using a loop) = " + sum(nDouble));
System.out.println("Sum (using the formula) = " + formulaSum(nDouble));
}
public static int calculateLoopSum(int n) {
int sum = 0;
for (int i = 1; i < n+1 ; i++ ) {
sum = sum + i;
}
return sum;
}
public static int calculateFormulaSum(int n) {
int formulaSum = n*(n+1)/2;
return formulaSum;
}
public static double calculateLoopSum(double nDouble) {
double sum = 0;
for (double i = 1; i < nDouble+1 ; i++ ) {
sum = sum + i;
}
return sum;
}
public static double calculateFormulaSum(double nDouble) {
double formulaSum = nDouble*(nDouble+1)/2;
return formulaSum;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
