Question

PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning...

PLEASE INCLUDE COMMENTS

In java

Create a Java Program

  • Add the following comments at the beginning of the file:
    • Your name.
    • The name of the class(es) used in the program.
    • The core concept (found below) for this lesson.
    • The date the program was written.
  • Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then the recursive method will return 2 + 4 + 6 + 8 + 10 = 30. If the user enters 5 then the recursive method will return 2 + 4 = 6.
  • In the main method:
    • Ask the user for an integer. Call the recursive method, passing it the user input.
    • Include error handling for the input (the input must be an integer).
    • Display the original value and the result of the recursive method calculation. Include an appropriate output message.
    • Repeat the program until the user chooses to quit.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

*   Java program to add even numbers recurssively

*/

import java.util.Scanner;

import java.util.*;

// Declaration of class

class recSum{

    // main method execution starts from here

    public static void main(String[] args){

        // Declaration of variable

        int choice = 0;

        // Iterates while user input quit condition

        do{

            // promot message for user

            System.out.print("\nEnter number: ");

            int number = getInteger();              // call function

            // it checks for possitive integer no

            if(number > 0){

                recEvenSum(number, 0);

            }

            else{

                System.out.println("Enter Possitive no only");

            }

            // promot message for user

            System.out.println("\nPress 0 to continue, 1 to Quit");

            choice = getInteger();                  // call function

        }while(choice != 1);                        // checks for terminating condition

    }

    // this function return integer value input from user

    public static int getInteger(){

        // create object for scanner class

        Scanner i = new Scanner(System.in);

        int number = 0;

        

        // iterate while valid input

        for(boolean test = false; test == false;){

            try{

                number = i.nextInt();           // get input from console

                test = true;

                return number;                  // return to calling function

            }

            // throws error if input not integer

            catch(InputMismatchException e){

                System.out.println("Invalid input");

            }

            i.nextLine();                       // nextInt() function tripping on subsequent calls so its required

        }

        return number;

    }

    // recursive function for finding sum of even no's takes two parameters

    public static void recEvenSum(int number, int sum){

        // iterate while greater than 0

        if(number >= 0){

            // checks for even no

            if(number % 2 == 0){

                sum += number;                      // add number to sum

                recEvenSum(number - 2, sum);        // recursive call

            }else

                recEvenSum(number - 1, sum);

        }else{                                      // print final sum

            System.out.println("The recursive Even no's Sum: " + sum );

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning...
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 give the java code for the below and add comments for different areas for a...

    ***Please give the java code for the below and add comments for different areas for a dummy to understand*** This java program will combine the techniques of handling arrays, decision control statements, and loops. Your program should accomplish the following: Build an array that holds characters (size 35) Load the array with random capital letters (use random generator) Your program should present a menu to the user allowing for the following “actions”. To “search” for a target letter of the...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • Be sure to include a message to the user explaining the purpose of the program before...

    Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Be sure to include information about which document codes are valid. Within the main method, please complete the following tasks in java program: Create a loop that allows the user to continue to enter two-character document designations until a sentinel value is entered (ex. “XX”). If the user enters a valid document code, please echo that value back...

  • Write a small program using C++, that does the following using the Conditional Operator only. Prompts...

    Write a small program using C++, that does the following using the Conditional Operator only. Prompts user to input a score between 1-6 (assume the user will honor the request) Stores the user input in an appropriate variable (appropriate in type and name) Checks if the score is a passing grade. Assume 70% or more is needed Displays a message if the score is passing or if the score is not a passing score Include a test table in the...

  • in JAVA please please include a main() and show output!! Create a GUI program shown below....

    in JAVA please please include a main() and show output!! Create a GUI program shown below. User enters information through the first window. When “Close” button is clicked, your program stops; when “Show Info” button is clicked, the input information is displayed on the TextArea; while when “Modify” button is clicked, the second window pops up and the information user enters to the first window automatically fills in the second window. User can change the information on the second window....

  • Create the following programs in Java {Java1 difficulty} [Please create as simple as possible] a. Ask...

    Create the following programs in Java {Java1 difficulty} [Please create as simple as possible] a. Ask the user to enter their favorite number and favorite word. Pass both of these values to a method that will print the favorite word vertically the favorite number of times. b. Ask the user to enter 2 integers. Pass the integers to 2 different methods: one method to add the integers and print the sum (from the method); the second method to multiply the...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

    Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function: Problem Specification: The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.) table2 = [] for line in infile: row=line.split() intRow = []...

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