In: Computer Science
The program Nadha Skolar wrote is supposed to read an integer value supplied by the user that should be between 1-30. Using the value supplied (let's call it n), three different summation formulas should be calculated using three different methods. Nadha Skolar was supposed to write a program to read the user's input, ensure that it is a value between 1-30 and then call the appropriate methods to calculate the summations and print the results:
Sample Runs:
Run #1 (bad value for n):
Enter a number between 1-30 for the value of n: 0 <--- user enters the number for n Program cannot continue. n must be a value 1-30. <--- use a println
Run #2 (bad value for n):
Enter a number between 1-30 for the value of n: 31 <--- user enters the number for n Program cannot continue. n must be a value 1-30. <--- use a println
Run #3:
Enter a number between 1-30 for the value of n: 3 <--- user enters the value for n The summation of the first n numbers is: 6 The summation of the n odd numbers is: 9 The summation of n numbers squared is: 14 <--- use a println
Tips:
import java.util.*;
/* Name: NadhaSkolar Solution
public class NadhaStrikesAgain { //my best work yet! I have just
learned about methods!
public static void main(String[] args) {
//get the value for n
Scanner scnr = new Scanner(System.in);
int n = getN();
//ensure n is usable
if ((n <= 0) && (n > 30)){
System.out.println("Program cannot continue. n must be a value
1-30.");
}
int sum = summationN(n);
int sumodd = sumofOddN(sum);
int sumSquares = sumOfSquaresN(n);
displayResults(sumodd, sum, sumSquares); //print them out
}
}
// ************************************************
// DESCRIPTION - getN() - prompts for n and returns value to
main
// ************************************************
public static void getN(Scanner scnr) {
//prompt for a number,
System.out.print("Enter a number between 1-30 for the value of n:
");
int n = scnr.nextInt();
return n;
}
// ************************************************
// DESCRIPTION - addOdd() computes summation of first "num" odd
numbers using the formula n^2
// ************************************************
public static double sumofOddN(int num) {
return(num^2);
}
// ************************************************
// DESCRIPTION - summation() - computes and returns the summation
of i from to N
// ************************************************
public static int summationN(int num) {
return(num * num + 1 / 2);
}
// ************************************************
// DESCRIPTION - sumOfSquares() - computes and returns the sum of
squares of first "num" numbers
// ************************************************
public static int sumOfSquaresN(int n) {
return((num * (num + 1) * (2 * num + 1) / 6));
}
// ************************************************
// DESCRIPTION - displayResults() - displays the various
summations
// ************************************************
public static void displayResults(int sum, int oddSum, int
sumSquares) {
System.out.println();
System.out.println("The summation of the first n numbers is: " +
sum);
System.out.println("The summation of the n odd numbers is: " +
oddSum);
System.out.println("The summation of n numbers squared is: " +
sumSquares);
}
}
}
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new java program with name "NadhaStrikesAgain.java" is created, which contains following code.
NadhaStrikesAgain.java :
import java.util.*;//import package
/* Name: NadhaSkolar Solution
*/
//java class
public class NadhaStrikesAgain {
//my best work yet! I have just learned about methods!
//entry point , main method
public static void main(String[] args) {
//get the value for n, object of scanner class
Scanner scnr = new
Scanner(System.in);
int n = getN(scnr);
//ensure n is usable
if ((n <= 0) || (n > 30))
{
System.out.println("Program cannot continue. n must be a value
1-30.");
}
else
{
int sum = summationN(n);//call
method to get the sum
int sumodd = sumofOddN(n);//call
method to sum the odd numbers
int sumSquares =
sumOfSquaresN(n);//call the method to square the sum
displayResults(sum,sumodd,
sumSquares); // print them out
}
}
// ************************************************
// DESCRIPTION - getN() - prompts for n and returns value to
main
// ************************************************
public static int getN(Scanner scnr) {
//prompt for a number,
System.out.print("Enter a number
between 1-30 for the value of n: ");
int n = scnr.nextInt();//read
input
return n;//return n
}
// ************************************************
// DESCRIPTION - addOdd() computes summation of first "num" odd
numbers using the formula n^2
// ************************************************
public static int sumofOddN(int num) {
return (num*num);
}
// ************************************************
// DESCRIPTION - summation() - computes and returns the summation
of i from to N
// ************************************************
public static int summationN(int num) {
return num * (num + 1)/ 2;//return
sum
}
// ************************************************
// DESCRIPTION - sumOfSquares() - computes and returns the sum of
squares of first "num" numbers
// ************************************************
public static int sumOfSquaresN(int num) {
return ((num * (num + 1) * (2 * num
+ 1) / 6));//return sum of squares
}
// ************************************************
// DESCRIPTION - displayResults() - displays the various
summations
// ************************************************
public static void displayResults(int sum, int oddSum,
int sumSquares) {
System.out.println();
System.out.println("The summation
of the first n numbers is: " + sum);
System.out.println("The summation
of the n odd numbers is: " + oddSum);
System.out.println("The summation
of n numbers squared is: " + sumSquares);
}
}
======================================================
Output : Compile and Run NadhaStrikesAgain.java to get the screen as shown below
Screen 1 :NadhaStrikesAgain.java ,Run #1 (bad value for n)
Screen 2 :NadhaStrikesAgain.java ,Run #2 (bad value for n)
Screen 3:Run #3
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.