Question

* Please write the answer without the explanation. * Please star with Public class. Write a...

* Please write the answer without the explanation.

* Please star with Public class.

Write a program that asks the user to enter two positive integers (input validation), then the program should pass the numbers to the method to display all even numbers between those two integers inclusive in the following format.

If enter 11 and 18, the output should be: (12, 14, 16, 18)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation::

  • Code in JAVA is given below
  • Please read comments for better understanding of the code
  • Screenshots of the OUTPUT are given at the end of the code


Code in JAVA::

import java.util.Scanner;

public class EvenNumbers {

   public static void main(String[] args)throws Exception {
       /*
       * We are using Scanner class to take input from the user
       * **/
       Scanner sc=new Scanner(System.in);
      
       /**
       * Two integer variables are declared to store lower and higher value
       * */
       int lower,higher;
      
       /**
       * Prompting user to enter positive values
       * If user enters negative value for lower then
       * We ask again to enter the value of lower
       * */
       while(true) {
           System.out.print("Enter value of lower for the range : ");
           lower = sc.nextInt();
           if(lower>=0) {
               break;
           }
           System.out.println("Please enter positive value for lower.");
       }
       /**
       * Prompting user to enter value that is greater then lower
       * */
       while(true) {
           System.out.print("Enter value of higher for the range : ");
           higher = sc.nextInt();
           if(higher>lower) {
               break;
           }
           System.out.println("Please enter value greater then lower.");
       }
      
       /**
       * Now that we have both valid inputs let's call the function to
       * print only positive value
       * */
       System.out.println(" The outpur is:");
       displayEven(lower,higher);

   }

   private static void displayEven(int lower, int higher) {
       /**
       * First we print the open bracket (
       * */
       System.out.print("(");
      
       /**
       * Then we run the for loop that start from
       * lower to hogher both inclusive
       * */
       for(int i=lower;i<=higher;i++) {
           /**
           * To check if current value of i is even
           * we must see if remainder is 0 when i is divided by 2
           * */
           if(i%2==0) {
               /**
               * Here in if we check if current i is equal to higher
               * or higher-1, if so then which means we are at last number.
               * So we must print the number and closing bracket )
               * */
               if(higher==i || higher-1==i) {
                   System.out.print(i+")");
               }else {
                   /**
                   * Else we just print the number along with comma
                   * */
                   System.out.print(i+", ");
               }
           }
       }/*For loop ends here*/
       System.out.println();
      
   }/*displayEven function ends here*/
  
}/*class ends here*/

OUTPUT::

TEST CASE 1:

TEST CASE 2:

Please provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
* Please write the answer without the explanation. * Please star with Public class. Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help and please satisfy the directions. The directions are under program 3. port JavaScore. public...

    Please help and please satisfy the directions. The directions are under program 3. port JavaScore. public class Example 4 [ public static int getint String strum - JOptionPane.showinput Dialog("Enter an integer"); return integer.parseInt(strum): public static void main(String args Scanner input = new Scanner (System.in): System.out.println("Enter 2 integers"); intx input.nextInt(); inty input.nextInt(): 1/ these are simple is if b ) System.out.println("The first number is positive): if (0 y0) System.out.println("Both numbers are positive"); ( 11 y) System.out.println("At least one number is positive");...

  • Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...

    Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of Numbers/C++ program Write a C ++ program that asks the user for a positive integer value by prompting "Enter a positive integer number: ", read in that number, then use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2,3, 4…50....

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Please answer this question in C++. Also, please write it with 'using namespace std' because the other ways some of you do it is confusing. Class Exercise Write a program that does the follow...

    Please answer this question in C++. Also, please write it with 'using namespace std' because the other ways some of you do it is confusing. Class Exercise Write a program that does the following: 1. Defines a class named Data that has the following members a. A vector of integers entitled numbers (Private) b. Methods (Functions) (Public): i. Display_Menu Prompts the user to choose one of the following 0. Quit 1. Input Numbers 2. Display Numbers 3. Search Numbers 4....

  • PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from...

    PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from tony gaddis starting out with java book. I have included the screenshot of the problems for reference. I need HELP implementing these two problems TOGETHER. There should be TWO class files. One which has TWO methods (one to implement problem 8, and one to implement problem 9). The second class should consist of the MAIN program with the MAIN method which calls those methods....

  • c++ class, coding using code blocker MinilabLoopLogic The program: You are to write a program called...

    c++ class, coding using code blocker MinilabLoopLogic The program: You are to write a program called MinilabLoopLogic which does the following Asks the user to enter 2 integers (can be one prompt or two) Gets and stores the ints Explains that the program will generate all integers that are between the numbers and are divisible by a third integer . e Asks the user for the number they should be divisible by Gets and stores that int Since 0 will...

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • this is for c++ please and thank you! 19. Check Writer Write a program that displays...

    this is for c++ please and thank you! 19. Check Writer Write a program that displays a simulated paycheck. The program should ask the user to enter the date, the payee's name, and the amount of the check (up to $10,000). It should then display a simulated check with the dollar amount spelled out, as shown here: Date: 11/24/2014 Pay to the Order of: John Phillips $1920.85 One thousand nine hundred twenty and 85 cents Be sure to format the...

  • Please help me with these, thank you! 3. **25.3 (Implement inorder traversal without using recursion) Implement...

    Please help me with these, thank you! 3. **25.3 (Implement inorder traversal without using recursion) Implement the inorder method in BST using a stack instead of recursion. Write a test program that prompts the user to enter 10 integers, stores them in a BST, and invokes the inorder method to display the elements. 4. **25.4 (Implement preorder traversal without using recursion) Implement the preorder method in BST using a stack instead of recursion. Write a test program that prompts the...

  • Write a class named DartSector. The constructor should accept a sector number and position {Single, Double,...

    Write a class named DartSector. The constructor should accept a sector number and position {Single, Double, Treble, Outer & Inner} represented with the integers 1 – 5 respectively. The class should have 3 methods: • getSectorColor – method should return the pocket’s color (as a String) • singleThrow – method should generate 2 random numbers; one in the range (1..20) and another (1..5); and return the score for the throw. • throwThree – method should generate 3 sets of random...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT