Maximum of Decimal Numbers

Saturday, 31 March 2018

Maximum of Decimal Numbers


Write an applet that allows user to input three decimal numbers and it determines and return the largest of them.

The program
//Finding the maximum of three floating point numbers

import java.awt.Container;

import javax.swing.*;

public class MaximumTest extends JApplet{
 
  public void init()
  {
    //allows the user to obtain input as a string.

    String s1 = JOptionPane.showInputDialog("Enter first floating-point value");
    String s2 = JOptionPane.showInputDialog("Enter second floating-point value");
    String s3 = JOptionPane.showInputDialog("Enter third floating-point value");
   
    //method Double.parseDouble is used to convert the strings input by the user to double values
    double number1 = Double.parseDouble( s1 );
    double number2 = Double.parseDouble( s2 );
    double number3 = Double.parseDouble( s3 );
   
    //method maximum return returns the result to init, using a return statement
    //this program assigns the result to variable max
    double max = maximum(number1, number2, number3);
   
    //create JTextArea to display results
    JTextArea outputArea = new JTextArea();
   
    //display numbers and maximum value
    outputArea.setText("number1: " + number1 + "\nnumber2: " + number2 + "\nnumber3: " + number3 + "\nMaximum is: " + max );
   
    //get applet's GUI component to display area
    Container container = getContentPane();
   
    //attach outputArea to container
    container.add(outputArea);
  }
 
  public double maximum(double x, double y, double z )
  {
    return Math.max(x, Math.max(y, z));
  }
}

0 comments :

Post a Comment