Question

Write a method using JAVA called "maximum4" that returns the maximum of 4 decimal numbers. Use...

Write a method using JAVA called "maximum4" that returns the maximum of 4 decimal numbers. Use the "math.max" method to implement "maximum4". The user should enter the 4 values and the program determines the maximum and then display the result.

The steps for the Project could be the following:

  • Create the Maximum4 method that includes the math.max
  • Define and enter the variables
  • Apply the Maximum4 method you created to find the maximum of the 4 decimal numbers you entered and then display it.

Homework Answers

Answer #1

Code for Above Problem:

import java.util.Scanner;

public class MaximumDemo {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                // Asking user to enter decimal number1
                System.out.print("Enter decimal number 1: ");
                double number1 = sc.nextDouble();
                // Asking user to enter decimal number2
                System.out.print("Enter decimal number 2: ");
                double number2 = sc.nextDouble();
                // Asking user to enter decimal number3
                System.out.print("Enter decimal number 3: ");
                double number3 = sc.nextDouble();
                // Asking user to enter decimal number4
                System.out.print("Enter decimal number 4: ");
                double number4 = sc.nextDouble();

                // getting maximum of four decimal numbers by calling maximum4 method
                double max = maximum4(number1, number2, number3, number4);

                // Displaying maximum of four decimal numbers
                System.out.println("Maximum of four decimal numbers is: " + max);
                sc.close();
        }

        // method to return maximum of four decimal numbers
        public static double maximum4(double number1, double number2, double number3, double number4) {
                /*
                 * Single line answer Math.max(Math.max(Math.max(number1,
                 * number2),number3),number4)
                 */

                // get maximum of number1 and number2 and store in max
                double max = Math.max(number1, number2);
                // get maximum of max and number3 and store in max
                max = Math.max(max, number3);
                // get maximum of max and number4 and store in max
                max = Math.max(max, number4);
                // return max which is maximum of four decimal values
                return max;
        }
}

Sample Run Input/Output:

Enter decimal number 1: 15.3
Enter decimal number 2: 27.5
Enter decimal number 3: 82.2
Enter decimal number 4: 82.29
Maximum of four decimal numbers is: 82.29

Image Of Code:

Image Of Sample Run Input/Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to get 4 integers from the user and store them in variables. The program should calculate the average of the 6 numbers as a floating point. Output all of the original input and the calculated average in the Command window. The code is expected to be commented and user-friendly.
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
(Center of a triangle) In Java, write the following method that returns the center of a...
(Center of a triangle) In Java, write the following method that returns the center of a triangle: (MAKE SURE TO USE THIS METHOD) public static Point getCenterPoint(Point p1, Point p2, Point p3); Write a test program that prompts the user to enter three points and displays the center point. Here is a sample run: (Hint: Use what you created for the previous problem). Enter x1, y1, x2, y2, x3, y3: 2.5 2 5 -1.0 4.0 2.0 The center point is...
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
JAVA IDE NETBEANS Create a program that will prompt the user to enter 20 numbers using...
JAVA IDE NETBEANS Create a program that will prompt the user to enter 20 numbers using any looping statement. Store it in a 2-dimensional array. Display all the numbers in a table format. Determine which numbers are odd and which numbers are even (include their array index) need some help with codes
A ________ is accurately represents large or very precise decimal numbers.    2. When a value...
A ________ is accurately represents large or very precise decimal numbers.    2. When a value is converted from one data type to another data type that can store either large numbers or numbers with greater precision it is called ______________ 3.  A procedure that returns a value is called a __________________ 4. A procedure that performs its task but does not return a value is called ______________ 5.  Local variables have a certain ____________ in the program whereas Global variables can...
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns...
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns a 2D array of integers of size rows x cols where the value of element [row][col] is row + col. Write a short program to test your method.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them.         [0.5 mark] (BY USING C PROGRAM)
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT