Question

In: Computer Science

Write a Java program which reads a positive integer from the command line, then displays the...

Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided.

  • validate that the command line argument is an integer greater than 0
  • to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException
  • use one or more for loops to perform the even and odd sum calculations

Expected console output follows. Command line argument shown in red. Output was generated by multiple runs of the program to test input validation.

D:\COP2800C> java Sums 0
Value entered is 0
0 is out of range.

D:\COP2800C> java Sums -1
Value entered is -1
-1 is out of range.

D:\COP2800C> java Sums 5.5
Value entered is 5.5
5.5 is not an integer.

D:\COP2800C> java Sums hello
Value entered is hello
hello is not an integer.

D:\COP2800C> java Sums 5
Value entered is 5
The sum of the even numbers up to 5 is 6
The sum of the odd numbers up to 5 is 9

Solutions

Expert Solution

Hi,
Please find the answer below:
----------------------------------------------------

Java code:


import java.util.Scanner;

//Sums.java
public class Sums {

   public static void main(String[] args) {
       //variables to hold evens and odd sums
       int sumOfOdds=0;
       int sumOfEvens=0;
       try {
           //parse the command line argument
           int number = Integer.parseInt(args[0]);
           System.out.println("Value entered is "+number);
           if(number > 0) {
               //for loop to calculate
               for(int i=1;i<=number;i++) {
                   if(i%2 ==0)
                       sumOfEvens=sumOfEvens+i;
                   else
                       sumOfOdds=sumOfOdds+i;
               }
               //print the sums
               System.out.println("The sum of the even numbers up to " + number + " is " + sumOfEvens);
               System.out.println("The sum of the odd numbers up to " + number + " is " + sumOfOdds);

           }else {
               System.out.println(number + " is out of range.");  
           }
           // catch exception
       }catch(NumberFormatException nfe) {
           System.out.println(args[0] + " is not an integer.");
       }
   }
}


Sample output:
Value entered is 5
The sum of the even numbers up to 5 is 6
The sum of the odd numbers up to 5 is 9

Screenshot:

-----------------------------------------------------
Hope this helps.
Kindly like the solution if it is useful to you.
Let me know if you need more help.
Thanks.


Related Solutions

Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
JAVA PROGRAMMING RecallNoIF! Write a program named RecallNoIF that reads an integer representing a year. (In...
JAVA PROGRAMMING RecallNoIF! Write a program named RecallNoIF that reads an integer representing a year. (In this case there is no prompt from the program.) The program prints out RECALL if the year was 2004, 2010 or 2015 and NO RECALL otherwise. CONSTRAINT: Nowhere in the program is an if statement used. REMINDER: the program's output is shown here in bold; the user's data entry is shown here in italics. Sample Interactive Run 1: 2010 RECALL Sample Interactive Run 2:...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT