Question

In: Computer Science

9) Create a java programming where you will enter two numbers and create only one method,...

9) Create a java programming where you will enter two numbers and create only one method, which will return or display
addition, substraction, multiplication, division, average and check which number is greater than the other.
Everything in one method and call it in main method.

Solutions

Expert Solution

Above program is to do calculations in one function and call it in the main method

Firstly , in main method, 2 integers are entered in console

and a function calculation is called with two arguments

As the function is called, control is moved inside the function

Inside the function, addition ,subtraction, multiplication , division, average manipulations are performed and printed in the console.

After that, number is compared which one is greater using If-else blocks and printed in the console.

_________________________

Below is the code in JAVA:


import java.util.*;
public class Main
{
static void calculation(int num1,int num2)
{
int sum,sub,mul,div,avg;
sum=num1+num2;
sub=num1-num2;
mul=num1*num2;
div=num1/num2;
avg=(num1+num2)/2;
System.out.println("Sum of 2 numbers: "+sum);
System.out.println("Subtraction of 2 numbers: "+sub);
System.out.println("Multiplication of 2 numbers: "+mul);
System.out.println("Division of 2 numbers: "+div);
System.out.println("Average of 2 numbers: "+avg);
if(num1>num2){
System.out.println(num1+" is greater");
}
else if(num1<num2){
System.out.println(num2+" is greater");
}
else{
System.out.println("Both numbers are equal");
}
}
   public static void main(String[] args) {
   Scanner scr=new Scanner(System.in);
       System.out.println("Enter 2 numbers");
       int num1,num2;
       num1=scr.nextInt();
       num2=scr.nextInt();
       calculation(num1,num2);
   }
}

Below is the code screenshot:

Below is the output screenshot:


Related Solutions

Create a Java to find the GCD of two numbers. You will need to create a...
Create a Java to find the GCD of two numbers. You will need to create a new variable to reflect the algorithm (numberOne, numberTwo, GCD = 0). Requirements: 1) read in numberOne 2) read in numberTwo. 3) run the loop, If numberTwo is 0 then print out numberOne, otherwise, temp =numberOne % numberTwo, numberOne = numberTwo, numberTwo = temp.
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once The program should output the average, largest, and smallest of 5 numbers. You must implement the methods listed below in your program. static float getAverage(int[] data) {...} static int getLargest(int[] data) {...} static int getSmallest(int[] data) {...}
Java programming Create a application with a method void (after main() ) that creates an array...
Java programming Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers. This array have infinite elements until the user decide that it is enough and input a command to stop. Display this array's elements data in another method void. Thanks for your help!
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
only JAVA code /** Create a method as instructed below and then call it appropriately. */...
only JAVA code /** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class MoreBankCharges { //Constant declarations for base fee and per check fees //Class scope so constants are accessible by all methods static final double BASE_FEE = 10.0; static final double LESS_THAN_20_FEE = 0.10; static final double TWENTY_TO_THIRTYNINE_FEE = 0.08; static final double FORTY_TO_FIFTYNINE_FEE = 0.06; static final double SIXTY_OR_MORE_FEE = 0.04; public static void main(String[] args) { //Variable declarations int numChecks;...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator. B) Write methods to implement the multiply, subtract and divide operations for integers. The results of all of these are integers. Use only the add operator.
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT