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

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 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...
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
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...
1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it...
1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it is a positive integer >= 0 3.Allocate (dynamically) an array big enough for the data. 4.Load the array with random numbers ranging in value from1 to 100 5.Display the elements of the array (unsorted) 6.Display the elements of the array (sorted) 7. Display the average 8.Display the median 9.Display the mode, if none, display appropriate message #include <iostream> #include <stdlib.h> /* srand, rand */...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT