Question

In: Computer Science

1) Question with methods use scanner: 1) Create one method that will add four numbers (return...

1) Question with methods use scanner:


1) Create one method that will add four numbers (return method or regular public static void )
2) Create another method that will subtract four numbers (return method or regular public static void )
3) Create another method that will multiplay four numbers (return method or regular public static void )
4) Create another method that will divide four numbers (return method or regular public static void )
5) Create another method that will find average of four numbers (return method or regular public static void )
6) In main method (Two parts):
1) Call four System prompt to enter numbers and four variables that will relate to scanner (they should be consistent with
variables declared in above methods).
2) Display results even with system.out.print assigned in main method or in your methods above?

Solutions

Expert Solution

Refer to code snippet below. Ask any doubts or clarifications in comments section.

import java.util.Scanner;

public class Answer {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter first number: "); //get first number
                int a = sc.nextInt();
                System.out.println("Enter second number: "); //get second
                int b = sc.nextInt();
                System.out.println("Enter third number: "); //get third
                int c = sc.nextInt();
                System.out.println("Enter fourth number: ");//get fourth
                int d = sc.nextInt();
                addNums(a, b, c, d); //call addNums method
                subtractNums(a, b, c, d);//call subtractNums method
                multiplyNums(a, b, c, d); //call multiplyNums method
                divideNums(a, b, c, d); //call divideNums method
                averageNums(a, b, c, d);// call averageNums method
        }
        public static void addNums(int a, int b, int c, int d)
        {
                int result = a+b+c+d; //add
                System.out.println("Sum of 4 numbers is : "+result); //print value
        }
        public static void subtractNums(int a, int b, int c, int d)
        {
                int result = a-b-c-d; //subtract
                System.out.println("Difference of 4 numbers is : "+result);//print value
        }
        public static void multiplyNums(int a, int b, int c, int d)
        {
                int result = a*b*c*d; //multiply
                System.out.println("Product of 4 numbers is : "+result);//display 
        }
        public static void divideNums(int a, int b, int c, int d)
        {
                double result = (double) a/b/c/d; //divide
                System.out.printf("Division of 4 numbers is : %.2f\n", result); //display
        }
        public static void averageNums(int a, int b, int c, int d)
        {
                double result = (a+b+c+d)/4; //find average
                System.out.println("Average of 4 numbers is : "+result); //display
        }
}

Output:

Refer to output above. Thanks.


Related Solutions

Add your own method (or use one or more of the existing methods) to insert the...
Add your own method (or use one or more of the existing methods) to insert the following set of numbers (1, 5, 19, 7, 23, 17, 2) in a linked list (use one function-call per number, and preserve the order of insertion). Once inserted print the linked list such that the output displays the numbers in reverse order. (2, 17, 23, 7, 19, 5, 1) JAVA CODE BELOW class aNode { char data; aNode next; aNode(char mydata) { // Constructor...
Create a circuit that allows the client to add numbers one by one into a register...
Create a circuit that allows the client to add numbers one by one into a register file (8 four-bit registers) by inputting: 1) the number itsself 2) the memory location The circuit can be logic gates OR verilog code. whichever you prefer! Thank you and I will rate!
Create a circuit that allows the client to add numbers one by one into a register...
Create a circuit that allows the client to add numbers one by one into a register file (8 four-bit registers) by inputting: 1) the number itsself 2) the memory location The circuit can be logic gates OR verilog code. whichever you prefer! Thank you and I will rate!
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
WITH using methods create an array with numbers
IN JAVA  Prompt 3:WITH using methods create an array with numbersint [] number = { 35, 68, 80, 34, 45, 79, 80};Display the numbers in the array using for loopDisplay the sumFind biggest element in the arrayDisplay the numers in the array with index numbersDisplay the numers using for loop in ascending order (sort)
code the following methods: one returning the average weekly hours and a method to add a...
code the following methods: one returning the average weekly hours and a method to add a weeks hours to the array of hours (this means creating a larger array).
Create a method evenOdd() that sorts an array into even numbers on one side and the...
Create a method evenOdd() that sorts an array into even numbers on one side and the odd numbers on the other side public class ourArray { int[] Ar; int size; public int getSize() {    return size; } public ourArray() {    Ar = new int[100];    size = 0; } public void add(int e) {    Ar[size]= e;    size++; } public int remove() {    //save the last item           int save = Ar[size -1];   ...
Objectives • Use the static methods and static fields. •Use the method-call/return mechanism. • Use the...
Objectives • Use the static methods and static fields. •Use the method-call/return mechanism. • Use the random-number generation. • Overload methods. Problem Specification Write a program that lets a user play "Rock, Paper, Scissors" against the computer or computer against computer. In user-computer game, the program should ask the user to choose one of the three choices, and then the computer randomly picks one (without knowing what the user has chosen). For this problem, the user should be asked to...
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.
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT