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

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) {...}
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...
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;...
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,...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
JAVA PROGRAMMING project 1: Businesses want phone numbers that are easy to remember, and one that...
JAVA PROGRAMMING project 1: Businesses want phone numbers that are easy to remember, and one that can be tied to a phone number are even better. Given the “standard international keypad”, write anaysis and design to determine the phone number based on a user-supplied 7-letter phrase. 1 2 ABC 3 DEF 4 GHI 5 JKL 6 MNO 7 PQRS 8 TUV 9 WXYZ * 0 # Test cases: ● BadDogs ● GoodCat ● Glasses ● EatGood ------------------------------------------------------------------------------------------------------------------------ I ONLY NEED...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1 to 1000. Then remove every even number. Then remove every multiple of 3 remaining Then remove every multiple of 5 Then remove every multiple of 7 Then sum the array, and print.
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT