Results 1 to 3 of 3
  1. #1
    Izuba is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Exclamation Program to display both the largest and smallest of a series of 5 integers

    Hi dear programmers
    here is a simple program on applets that is giving me a bit of headache since it cannot always display the two numbers that it is supposed to: the largest and smallest of the five input integers.....I cannot see where i went wrong, can somebody help me see? you can always create separately the HTML file to load the applet in an appletviewer of your won size. kind regards.


    *
    23.6 (Largest and Smallest) Write an applet that reads five integers, determines which are the
    largest and smallest integers in the group and prints them. Draw the results on the applet.
    */
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;

    public class Exercise23_6 extends JApplet
    {

    // class attributes
    private int number1 ;
    private int number2 ;
    private int number3 ;
    private int number4 ;
    private int number5 ;
    // initialise applets elements
    public void init()
    {
    String firstN = JOptionPane.showInputDialog( "Enter first integer : " );
    String secondN = JOptionPane.showInputDialog( "Enter second integer : " );
    String thirdN = JOptionPane.showInputDialog( "Enter third integer : " );
    String fourthN = JOptionPane.showInputDialog( "Enter fourth integer : " );
    String fifthN = JOptionPane.showInputDialog( "Enter fifth integer : " );

    // conversion of the String elements to integer
    number1 = Integer.parseInt( firstN );
    number2 = Integer.parseInt( secondN);
    number3 = Integer.parseInt( thirdN );
    number4 = Integer.parseInt( fourthN );
    number5 = Integer.parseInt( fifthN );
    } // end method init

    // method paint
    public void paint( Graphics g )
    {
    // calling superclass version of method paint
    super.paint( g );

    // draw a rectangle
    g.drawRect( 10, 15, 500, 450 );

    // displaying the input numbers
    g.drawString( "The input numbers are : ",50, 50 );
    g.drawString(" "+ number1, 50, 70);
    g.drawString( " "+ number2,60, 70 );
    g.drawString( " "+ number3, 70, 70 );
    g.drawString( " "+ number4, 80, 70 );
    g.drawString( " "+ number5, 90, 70 );
    // LARGEST
    if( number1 > number2 )
    {
    if( number1 > number3 )
    {
    if( number1 > number4 )
    {
    if( number1 > number5 )
    {
    g.drawString( number1 +" is the greatest", 50,90 );
    } // end if
    }// end if
    } // end if

    } // end if
    else if( number2 > number1 )
    {
    if( number2 > number3)
    {
    if( number2 > number4 )
    {
    if( number2 > number5)
    {
    g.drawString( number2 +" is the greatest.", 50, 90 );
    } // end if
    } // end if
    } // end if

    } // end else if
    else if( number3 > number1 )
    {
    if( number3 > number2 )
    {
    if( number3 > number4 )
    {
    if( number3 > number5 )
    {
    g.drawString( number3 +" is the greatest.", 50, 90 );
    } // end if
    } // end if
    } // end if
    } // end if else
    else if( number4 > number1 )
    {
    if( number4 > number2 )
    {
    if( number4 > number3 )
    {
    if( number4 > number5 )
    {
    g.drawString( number4 +" is the greatest.", 50, 90 );
    } // end if
    } // end if
    } // end if

    } // end if else
    else if( number5 > number1 )
    {
    if( number5 > number2 )
    {
    if( number5 > number3 )
    {
    if( number5 > number4 )
    {
    g.drawString( number5 +" is the greatest.", 50, 90 );
    } // end if
    } // end if
    } // end if
    } // end else if



    // SMALLEST
    if( number1 < number2 )
    {
    if( number1 < number3 )
    {
    if( number1 < number4 )
    {
    if( number1 < number5 )
    {
    g.drawString( number1 +" is the smallest", 50,100 );
    } // end if
    }// end if
    } // end if

    } // end if
    else if( number2 < number1 )
    {
    if( number2 < number3)
    {
    if( number2 < number4 )
    {
    if( number2 < number5)
    {
    g.drawString( number2 +" is the smallest.", 50, 100 );
    } // end if
    } // end if
    } // end if

    } // end else if
    else if( number3 < number1 )
    {
    if( number3 < number2 )
    {
    if( number3 < number4 )
    {
    if( number3 < number5 )
    {
    g.drawString( number3 +" is the smallest.", 50, 100 );
    } // end if
    } // end if
    } // end if
    } // end if else
    else if( number4 < number1 )
    {
    if( number4 < number2 )
    {
    if( number4 < number3 )
    {
    if( number4 < number5 )
    {
    g.drawString( number4 +" is the smallest.", 50, 100 );
    } // end if
    } // end if
    } // end if

    } // end if else
    else if( number5 < number1 )
    {
    if( number5 < number2 )
    {
    if( number5 < number3 )
    {
    if( number5 < number4 )
    {
    g.drawString( number5 +" is the smallest.", 50, 100 );
    } // end if
    } // end if
    } // end if
    } // end else if
    } // end method paint
    } // end class Exercise23_6

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Program to display both the largest and smallest of a series of 5 integers

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Can you post what the code does? Show the 5 numbers that were input and what numbers it chose.

    Do you know about arrays? Using an array would make it easier.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Program to display both the largest and smallest of a series of 5 integers

    Hi,

    Your code is really hard to read. You could optimise it with 2 additional variables min and Max. Then you set these to the first number. Then you just need to compare these variables to the remaining numbers. If required, you set the new min or Max value.

    Just keep in mind: the more complex your code is, the harder it is to find an error. In such a case you have to work on your code to make it easier.

    Konrad

Similar Threads

  1. Largest and smallest numbers program
    By gquach in forum New To Java
    Replies: 1
    Last Post: 08-17-2013, 03:43 PM
  2. Replies: 2
    Last Post: 10-06-2012, 05:29 AM
  3. Replies: 12
    Last Post: 09-08-2011, 06:03 PM
  4. Array[] get smallest/largest value
    By bobocheez in forum New To Java
    Replies: 13
    Last Post: 09-10-2010, 12:16 AM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 11:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •