Question

In: Computer Science

How to use a Java program that calls for two functions. The first function is asking...

How to use a Java program that calls for two functions. The first function is asking the user for a two integer numbers, then display the sum of the numbers. The second method is asking the user for two floating point numbers, then display the product of the numbers. Call these methods addNumbers and multiplyNumbers respectively. Call the program AddAndMultiply

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// AddAndMultiply.java

import java.util.Scanner;

public class AddAndMultiply {

      // a global Scanner variable used to read input from keyboard

      private static Scanner keyboard = new Scanner(System.in);

      // method to read two integers and print their sum

      public static void addNumbers() {

            System.out.print("Enter two integers: ");

            // reading two integers

            int a = keyboard.nextInt();

            int b = keyboard.nextInt();

            //finding sum

            int sum = a + b;

            System.out.println("The sum of " + a + " and " + b + " is " + sum);

      }

      // method to read two floats and print their product

      public static void multiplyNumbers() {

            System.out.print("Enter two floating point numbers: ");

            // reading two floating point values (could enter integers too)

            double a = keyboard.nextDouble();

            double b = keyboard.nextDouble();

            //finding product

            double product = a * b;

            System.out.println("The product of " + a + " and " + b + " is "

                        + product);

      }

      public static void main(String[] args) {

            // reading integers, displaying sum

            addNumbers();

            // reading floating point numbers, displaying product

            multiplyNumbers();

      }

}

/*OUTPUT*/

Enter two integers: 15 17

The sum of 15 and 17 is 32

Enter two floating point numbers: 5.5 1.2

The product of 5.5 and 1.2 is 6.6


Related Solutions

java In one program, write 3 separate functions, and use them. 1) Write the function body...
java In one program, write 3 separate functions, and use them. 1) Write the function body for each: int squareInteger s( int x) double squareDouble( double d) float squareFloat ( float f) They basically do the same thing, square a number, but use different argument datatypes. 2) Test Each function with: Using int 4, double 4.9, float 9.4 What happened when you use the correct numerical data type as input ? What happened when you use the incorrect numerical data...
Please use Java language to write two efficient functions: Write an efficient function that compute the...
Please use Java language to write two efficient functions: Write an efficient function that compute the intersections of two sorted arrays of integers Write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
Can you provide java code for a program that prompts the user by asking "How many...
Can you provide java code for a program that prompts the user by asking "How many one mile races have you ran?". After the user inputs how many one mile races have they run it then prompts the user to input how many seconds it took for them to finish each race. So for example, if the user ran 6 races then the user will have to input 6 race times. Is there a way when you prompt the user...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Write a program that calls a/few function(s) to determine the smaller of two arguments. Test the...
Write a program that calls a/few function(s) to determine the smaller of two arguments. Test the program using integer, character and floating-point number arguments. Produce 3 separate programs solving the problem above. The three approaches you should be using are: a) 3 different functions (findMinimum1(), findMinimum2(), findMinimum3()), each to solve different data types parameters. b) Function overloading with only one function named findMinimum( ) c) Function template with only one function named findMinimum( )
This program must have 9 functions: •main() Controls the flow of the program (calls the other...
This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers, returns the modulo of the first...
1. Write a Java program and Submit only "one .java file" calledNumbers that calls the...
1. Write a Java program and Submit only "one .java file" called Numbers that calls the following methods and displays the returned value:o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive.o Write a method called larger that accepts two double parameters and...
Requirements: In this assignment, you are going to create two switch functions. The first function is...
Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need...
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT