Question

In: Computer Science

IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...

IN JAVA

Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive statement.

// Question 3

/*

public static int sum(int num1, int num2){

int both;

both = num1 + num2;

return both;

}

*/

Solutions

Expert Solution

Below is your code:

import java.util.Scanner;

public class Numbers {

        // main method
        public static void main(String[] args) {
                // declaring two variables
                int positiveNumber = -1;
                int negativeNumber = 0;

                // declaring Scanner to get user input
                Scanner keyboard = new Scanner(System.in);

                // getting input for first variable
                while (positiveNumber < 0) {
                        // prompt user to input a positive number
                        System.out.print("Enter a positive number: ");
                        // getting input from user
                        positiveNumber = keyboard.nextInt();
                        // if number entered is negative, print error and get input again
                        if (positiveNumber < 0) {
                                System.out.println("Please enter a valid number.");
                        }
                }

                // getting input for second variable
                while (negativeNumber >= 0) {
                        // prompt user to input a negative number
                        System.out.print("Enter a negative number: ");
                        // getting input from user
                        negativeNumber = keyboard.nextInt();
                        // if number entered is positive or 0, print error and get input
                        // again
                        if (negativeNumber >= 0) {
                                System.out.println("Please enter a valid number.");
                        }
                }
                // calling method to get sum
                int sum = sum(positiveNumber, negativeNumber);

                // printing result
                System.out.println("Sum of two numbers i.e. " + positiveNumber
                                + " and " + negativeNumber + " is: " + sum);

                // closing scanner
                keyboard.close();

        }

        // method from Question 3
        public static int sum(int num1, int num2) {
                int both;
                both = num1 + num2;
                return both;
        }
}

Output

Enter a positive number: -8
Please enter a valid number.
Enter a positive number: -6
Please enter a valid number.
Enter a positive number: 8
Enter a negative number: 6
Please enter a valid number.
Enter a negative number: 7
Please enter a valid number.
Enter a negative number: -4
Sum of two numbers i.e. 8 and -4 is: 4


Related Solutions

1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive). 2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked. 3. Take a year input from the user (should be between 1500 and 2019) 4. If the input year is a leap year, print that year is a leap year; otherwise, print that the...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
In Java:Implement a program that repeatedly asks the user to input apositive integer and...
In Java:Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT