Write a program that computes and prints the sum of integers from A to B . Creat
ID: 667206 • Letter: W
Question
Write a program that computes and prints the sum of integers from A to B. Create an appropriate class Summer that has a sum method that takes the range of numbers (A and B) from the user and returns the sum. Create a second class (SummerTester) with a main method to construct two objects of the Summer class and call the sum method to print out the result. For example: assume A = 1 and B = 100, the Sum will be: 1 + 2 + 3 + 4 + . . . + 100
Can you write the code which is easily understandle for NetBeans software and it uses javaDoc style. Thanks
Explanation / Answer
public class SummerTester{
public static void main(String[] args){
Summer s1=new Summer();
System.out.println("Sum of numbers between 1 and 100:"+s1.sum(1,100));
Summer s2=new Summer();
System.out.println("Sum of numbers between 1 and 10:"+s2.sum(1,10));
}
}
package mani;
public class Summer {
public int sum=0;
public int sum(int i, int j) {
for(int x=i;x<=j;x++){
sum=sum+x;
}
return sum;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.