Arithmetic, Smallest and Largest

We will be writing an application that  inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers using a java application GUI (Graphics User Interface)

So the first step of the program will be to import javax.swing.JOptionPane which is part of the java packages and which has the capability of displaying and inputting text in dialog box.
usually when inputting digits in java applications, it is entered as strings which can then be converted to corresponding data types like integers and floating point numbers.
The program code below illustrates how to do a simple arithmetic, smallest and largest of three numbers.


import javax.swing.JOptionPane;

public class Arithmetic{
  public static void main(String args [])
  {
    String firstNumber;
    String secondNumber;
    String thirdNumber;
    String compare;
 
    int number1;
    int number2;
    int number3;

    int sum;
    int product;
    int average;
 
    firstNumber = JOptionPane.showInputDialog("Enter first integer" );
    secondNumber = JOptionPane.showInputDialog("Enter second integer");
    thirdNumber = JOptionPane.showInputDialog("Enter third integer");
 
   
    number1 = Integer.parseInt(firstNumber);
    number2 = Integer.parseInt(secondNumber);
    number3 = Integer.parseInt(thirdNumber);
 
    
    sum = number1 + number2 + number3;
    product = number1 * number2 * number3;
    average = sum /3;
 
    compare = " ";
 
    if(number1>number2 && number1>number3)
      compare = compare + number1 +" "+ "is greater than" + number2 +" "+ "and" +" "+ number3;
    if (number1<number2 && number1<number3)
      compare = compare +"\n" + number1 +" "+ "is less than" + number2 +" "+ "and" + number3;
    if (number2>number1 && number2>number3)
      compare = compare +"\n" + number2 +" "+ "is greater than" + number1 +" "+ "and" + number3;
    if (number2<number1 && number2<number3)
      compare = compare +"\n" + number2 +" "+ "is less than" + number1 +" "+ "and" + number3;
    if (number3>number1 && number3>number2)
      compare = compare +"\n" + number3 +" "+ "is greater than" + number1 +" "+"and" + number2;
    if (number3<number1 && number3<number2)
      compare = compare +"\n" + number3 +" "+ "is less than" + number1 +" "+ "and" + number2;
 
    JOptionPane.showMessageDialog(null,"The sum is: "+" " + sum +"\n"+ "The Product is "+" " + product+"\n"+ "The average is "+" " + average +"\n"+ compare, "Results", JOptionPane.INFORMATION_MESSAGE);
 
    System.exit(0);
 
 
  }
}

Comments

Popular posts from this blog

Body Mass Index Calculator In C

Adding two Integers in Java Application

Calculating Product, sum, difference, and quotient of Five Integers