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

C++ program that reads a positive integer number from a user and displays all binary numbers...
C++ program that reads a positive integer number from a user and displays all binary numbers from 0 to the number. For the program, you can assume that the input number is a number between 0 and 100. Sample Run 1: Assume that the user typed the following number. 5 This is the correct output of your program. 000 001 010 011 100 101 Sample Run 2: Assume that the user typed the following number. 0 This is the correct...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values,...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the sentinel loop). The program should ignore any negative input and should continue to run. Hint: to remember/save entered (good) values, you can concatenate them into a string (separated by spaces) that you can output later on. Sample runs showing input prompts and...
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
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.
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers...
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.
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
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
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT