Program in Java that Display the Largest & Smallest Number

In this Java program, we will take 5 input numbers by the user and display the Largest and Smallest numbers in Graphical User interface (GUI).

This is the same solution of Deitel & Deitel exercises 2.2. However we enhance the output and display in a GUI instead of command line:
  • Write an application that reads five integers and determines and prints the largest and the smallest integers in the group.

Use you're favotie Java Editor and type in paste the code below:

import javax.swing.*;

/**
* Exercise 02 This program will outpt the largest and the smallest number
* Created by Airgildgreat on 3/26/2017.
*/

public class LargerSmallerGUI {

public static void main(String args[]){

//Declaring the variables
String fNumber, seNumber, thNumber, foNumber, fiNumber;
int num1, num2, num3, num4, num5, largest, smallest;

//Reads the input values
fNumber = JOptionPane.showInputDialog("Enter first number: ");
seNumber = JOptionPane.showInputDialog("Enter second number: ");
thNumber = JOptionPane.showInputDialog("Enter third number: ");
foNumber = JOptionPane.showInputDialog("Enter fourth number: ");
fiNumber = JOptionPane.showInputDialog("Enter fifth number: ");

//Converts string nteger values

num1 = Integer.parseInt(fNumber);
num2 = Integer.parseInt(seNumber);
num3 = Integer.parseInt(thNumber);
num4 = Integer.parseInt(foNumber);
num5 = Integer.parseInt(fiNumber);

//If condtions are meet

largest = num1; //Assumes num1 this is the largest;

if (num2 > largest)
largest = num2;

if (num3 > largest)
largest = num3;

if (num4 > largest)
largest = num4;

if (num5 > largest)
largest = num5;

smallest = num1; // Assumes num1 this the smallest

if (num2 < smallest)
smallest = num2;

if (num3 < smallest)
smallest = num3;

if (num4 < smallest)
smallest = num4;

if (num5 < smallest)
smallest = num5;

//Prints the result in Graphical User Interface
JOptionPane.showMessageDialog(null, "Largest is: " + largest + "\nSmallest is: " + smallest
,"Largest and Smallest Program in GUI", JOptionPane.INFORMATION_MESSAGE);
  }
}

Here is the output. In this case assumes user input the number "5", "6", "7", "8",
"9".

















Below is the version of the Java program which display the output in the Command Line

import java.util.Scanner;

/**
* Exercise no.05 This will determine the largest and the smallest number inut by the user
* Created by Airgildgreat on 3/27/2017.
*/
public class LargerSmallestCMD {

public static void main(String args[]){

//Call Scanner class
Scanner value = new Scanner(System.in);

//Declaring the variables
int num1, num2, num3, num4, num5, largest, smallest;

//Read the inpit value by the user
System.out.printf("Enter First Number: ");
num1 = value.nextInt();

System.out.printf("Enter Second Number: ");
num2 = value.nextInt();

System.out.printf("Enter Third Number: ");
num3 = value.nextInt();

System.out.printf("Enter Fourth Number: ");
num4 = value.nextInt();

System.out.printf("Enter Fifth Number: ");
num5 = value.nextInt();

//If condtions are meet
largest = num1; //Assumes num1 this is the largest;

if (num2 > largest)
largest = num2;

if (num3 > largest)
largest = num3;

if (num4 > largest)
largest = num4;

if (num5 > largest)
largest = num5;

smallest = num1; // Assumes num1 this the smallest

if (num2 < smallest)
smallest = num2;

if (num3 < smallest)
smallest = num3;

if (num4 < smallest)
smallest = num4;

if (num5 < smallest)
smallest = num5;

//Prints the result in Graphical User Interface
System.out.printf("\nThe Largest number is: %d\n The Smallest is: %d", largest, smallest);
   }
}





Here is the output. In this case assumes user input the number "5", "6", "7", "8",
"9".


Comments

Popular posts from this blog

Calculate the Sum, Product, Difference & Quotient in Java

Calculates the Squares and Cubes in Java

Determine Two Numbers which is the largest or Both Numbers are Equal in Java