Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the java programs provided in pAssignment07_template.zip and add necessar

ID: 3701004 • Letter: M

Question

Modify the java programs provided in pAssignment07_template.zip and add necessary code components so as to allow a user to input integer values into a 6-element array and search the array. The program should allow the user to retrieve values from the array by index and by specifying a value to locate (see the examples below and explanations in the class by the instructor). The program should handle any exceptions that might arise when inputting values or accessing array elements. The program should throw a NumberNotFoundException (refer to the template code given above) if a particular value cannot be found in the array during a search. If an attempt is made to access an element outside the array bounds, catch the ArrayIndexOutOfBoundsException and display an appropriate error message. Also, the program should throw an ArrayIndexOutOfBoundsException if an attempt is made to access an element for which the user has not yet input a value (the inputs and outputs should be though basic Java GUIs as shown below).

// ArrayAccess.java
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ArrayAccess extends JFrame
{
   private JTextField inputField;
   private JTextField retrieveField1;
   private JTextField retrieveField2;
   private JTextField outputField;
   private JPanel inputArea;
   private JPanel retrieveArea;
   private JPanel outputArea;

   private int num;
   private int index = 0;
   private int array[] = new int[ 10 ];
   private String result;
  
   // set up GUI
   public ArrayAccess()
   {
      super( "Accessing Array values" );
      setLayout( new FlowLayout() );
     
      // set up input Panel
      inputArea = new JPanel();
      inputArea.add( new JLabel( "Enter array elements here" ) );
      inputField = new JTextField( 10 );
      inputArea.add( inputField );
      inputField.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent e )
            {
               /* Create a try block in which the application reads the number
                  entered in the inputField and assigns it to the next index
                  in the array, then increments instance variable index. */
                 
               /* Write catch handlers that catch the two types of exceptions
                  that the previous try block might throw (NumberFormatException
                  and ArrayIndexOutOfBoundsException), and display appropriate
                  messages in error message dialogs. */
              
               inputField.setText( "" );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener
     
      // set up retrieve Panel
      retrieveArea = new JPanel( new GridLayout( 2, 2 ) );
      retrieveArea.add( new JLabel( "Enter number to retrieve" ) );
      retrieveField1 = new JTextField( 10 );
      retrieveArea.add( retrieveField1 );
      retrieveField1.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent event )
            {
               /* Create a try block in which the application reads from
                  retrieveField1 the number the user wants to find in the
                  array, then searches the current array contents for the number.
                  If the number is found, the outputField should display all the
                  indices in which the number was found. If the number is not
                  found, a NumberNotFoundException should be thrown. */
              
               /* Write catch handlers that catch the two types of exceptions that
                  the try block might throw (NumberFormatException and
                  NumberNotFoundException), and display appropriate messages
                  in error message dialogs. */
               
               retrieveField1.setText( "" );
            } // end method actionPerformed
         } // end anonymous inner class
      ); // end call to addActionListener
           
      retrieveArea.add( new JLabel( "Enter index to retrieve" ) );
      retrieveField2 = new JTextField( 10 );
      retrieveArea.add( retrieveField2 );
      retrieveField2.addActionListener(
         new ActionListener()
         {
            public void actionPerformed( ActionEvent event )
            {
               /* Create a try block in which the application reads from
                  retrieveField2 the index of a value in the array, then
                  displays the value at that index in the outputField. If the index
                  input by the user is not a number a NumberFormatException should
                  be thrown. If the number input by the user is outside the array
                  bounds or represents an element in which the application has not
                  stored a value, an ArrayIndexOutOfBoundsException should
                  be thrown. */
              
               /* Write catch handlers that catch the two types of exceptions
                  the try block might throw (NumberFormatException and
                  ArrayIndexOutOfBoundsException), and display appropriate
                  messages in error message dialogs. */
              
               retrieveField2.setText( "" );
            } // end anonymous inner class
         } // end new ActionListener
      ); // end call to addActionListener
     
      // set up output Panel
      outputArea = new JPanel();
      outputArea.add( new JLabel( "Result" ) );
      outputField = new JTextField( 30 );
      outputField.setEditable( false );
      outputArea.add( outputField );

      add( inputArea );
      add( retrieveArea );
      add( outputArea );
   } // end constructor
} // end class ArrayAccess

// ArrayAccessTest.java
import javax.swing.JFrame;

public class ArrayAccessTest
{  
   // execute application
   public static void main( String args[] )
   {
      ArrayAccess application = new ArrayAccess();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      application.setSize( 400, 200 );
      application.setVisible( true );
   }
} // end class ArrayAccessTest

// NumberNotFoundException.java
public class NumberNotFoundException extends Exception
{
   // no-argument constructor specifies default error message
   public NumberNotFoundException()
   {
      super( "Number not found in array" );
   }
  
   // constructor to allow customized error message
   public NumberNotFoundException( String message )
   {
      super( message );
   }
} // end class NumberNotFoundException

Explanation / Answer

Below is the program with modified code with added try catch blocks as required in ArrayAccess.java

There is only change in the ArrayAccess.java class.

// ArrayAccess.java

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class ArrayAccess extends JFrame

{

   private JTextField inputField;

   private JTextField retrieveField1;

   private JTextField retrieveField2;

   private JTextField outputField;

   private JPanel inputArea;

   private JPanel retrieveArea;

   private JPanel outputArea;

   private int num;

   private int index = 0;

   private int array[] = new int[ 6 ];

   private String result;

   // set up GUI

   public ArrayAccess()

   {

     

      super( "Accessing Array values" );

      setLayout( new FlowLayout() );

    

      // set up input Panel

      inputArea = new JPanel();

      inputArea.add( new JLabel( "Enter array elements here" ) );

      inputField = new JTextField( 10 );

      inputArea.add( inputField );

      inputField.addActionListener(

         new ActionListener()

         {

            public void actionPerformed( ActionEvent e )

            {

               

                try {

                    // reads the number entered in the inputField

                    int element = Integer.parseInt(inputField.getText());

                    // Assign element to the next index in the array

                    array[index] = element;

                    // Increments instance variable index

                    index++;     

                } catch (NumberFormatException ex) {

                    // Throw NumberFormatException

                    throw new NumberFormatException("Please enter elements in correct format");  

                } catch (ArrayIndexOutOfBoundsException ex) {

                    // Throw ArrayIndexOutOfBoundsException

                    throw new ArrayIndexOutOfBoundsException("Please enter elements in correct format");  

                }

               inputField.setText( "" );

            } // end method actionPerformed

         } // end anonymous inner class

     ); // end call to addActionListener

    

      // set up retrieve Panel

      retrieveArea = new JPanel( new GridLayout( 2, 2 ) );

      retrieveArea.add( new JLabel( "Enter number to retrieve" ) );

      retrieveField1 = new JTextField( 10 );

      retrieveArea.add( retrieveField1 );

      retrieveField1.addActionListener(

         new ActionListener()

         {

            public void actionPerformed( ActionEvent event )

            {

               

                try {

                     // reads the number entered in the inputField

                    int element = Integer.parseInt(retrieveField1.getText());

                    String text = "The index's are :";

// Search the element in array

                    for(int i =0 ; i<6; i++){

// If element is found

                        if(array[i]==element){

// Print the index where element is found

                        outputField.setText(text+i+",");

                        }

                        else{

// If number is not found throw exception

                        throw new NumberNotFoundException("Number Not found in array");

                        }

                    }

                } catch (NumberFormatException ex) {

                        // Throw NumberFormatException

                    throw new NumberFormatException("Please enter elements in correct format");  

                } catch (NumberNotFoundException ex) {

                }

              

               retrieveField1.setText( "" );

            } // end method actionPerformed

         } // end anonymous inner class

      ); // end call to addActionListener

         

      retrieveArea.add( new JLabel( "Enter index to retrieve" ) );

      retrieveField2 = new JTextField( 10 );

      retrieveArea.add( retrieveField2 );

      retrieveField2.addActionListener(

         new ActionListener()

         {

           public void actionPerformed( ActionEvent event )

            {

                 try {

                     // reads the number entered in the inputField

                    int index = Integer.parseInt(retrieveField2.getText());

                    // Store the value at index in number variable

                    int value = array[index];

// Output the value found at index

                    outputField.setText(Integer.toString(value));

                   

                } catch (NumberFormatException ex) {

                        // Throw NumberFormatException

                    throw new NumberFormatException("Please enter elements in correct format");  

                }catch (ArrayIndexOutOfBoundsException ex) {

                        // Throw ArrayIndexOutOfBoundsException if index not found

                    throw new ArrayIndexOutOfBoundsException("Please enter correct index ");  

                }

             

               retrieveField2.setText( "" );

            } // end anonymous inner class

         } // end new ActionListener

      ); // end call to addActionListener

    

      // set up output Panel

      outputArea = new JPanel();

      outputArea.add( new JLabel( "Result" ) );

      outputField = new JTextField( 30 );

      outputField.setEditable( false );

      outputArea.add( outputField );

      add( inputArea );

      add( retrieveArea );

      add( outputArea );

   } // end constructor

  

} // end class ArrayAccess

Note: Comments are added to each line to better understand the code

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote