Question

In: Computer Science

The program Nadha Skolar wrote is supposed to read an integer value supplied by the user...

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:

  • summationN() - Calculates and returns the summation of the first n integers. This should be calculated using n(n+1)/2
  • sumofOddN() - Calculates and returns the summation of the first n odd integers. This should be calculated using n2
  • sumOfSquaresN() - Calculates and returns the sum of squares of the first n integers ∑i2 from 1 to n. This should be calculated using n(n+1)(2n+1)/6

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:

  1. Test your program in intelliJ before copying it back to zyBooks.
  2. When the program is working correctly, copy the code from intelliJ back to zyBooks and submit for grading.
  3. Important: You may NOT erase and re-write the entire program or an entire method. Your job is to fix Nadha's thinking and programming.

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);
}
}
}

Solutions

Expert Solution

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.


Related Solutions

A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Playing with encryption: Write a program that will read a four-digit integer entered by the user...
Playing with encryption: Write a program that will read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the second digit with the fourth. Finally, print the original number and its encrypted one. Now reverse the process. Read an encrypted integer and decrypt it by reversing the algorithm to obtain the original...
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
Write a program that uses a loop to read 10 integers from the user. Each integer...
Write a program that uses a loop to read 10 integers from the user. Each integer will be in the range from -100 to 100. After all 10 integers have been read, output the largest and smallest values that were entered, each on its own line in that order. Avoiding using the max or min functions from Python, and definitely use a loop to read the integers instead of 10 input statements.
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
Write a C++ program that prompts the user (or “Player 1”) to input an integer value...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value between 1 and 3 (where 1=paper, 2=scissor, and 3=rock). This input should be passed into a string function called player_RPS(), and returns a string value indicating the selection of paper, scissors, or rock (as mentioned above). Next, the returned string value, along with a generated input from the computer, should be passed into a void function called RPS_comparison(), and determines whether the user’s input...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT