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

Python 3.4 3.6 code: Create a menu-driven program that will calculate and displa

ID: 3854715 • Letter: P

Question

Python 3.4 3.6 code:

Create a menu-driven program that will calculate and display all the prime numbers between 2 and a user-entered limit.
Your simple menu should have only 3 options:
1. Create a list of primes from 2 to n using the Sieve of Eratosthenes algorithm.

2. Display the prime numbers

3. Quit

Program particulars:
You must use standard list-traversal coding structures to perform your calculations.
Get a limit (type int) from the user to represent the upper limit of the list.  

There must be error checking on the input integer.  If it not greater than 2, the program will print an error message and re-prompt. This process will continue until an integer greater than 2 is entered.

You must use a try-except structure to trap both types of input errors (like letters where numbers should go) and range errors (like < 2).
There must be error checking on the menu choice entered. If the user enters a choice not on the menu, the program will print an error message, re-display the menu and re-prompt. This process will continue until a valid option value is entered.
Your solution must be modular.

The design of your functions is up to you, but the rules of “highly cohesive” and “loosely coupled” must be followed.
Your program should be well-documented. Explain what you’re doing in your code. Be sure to include the usual name and assignment notes

Explanation / Answer

code in java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import static java.lang.Math.*;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.Dimension;

public class eratosthenesJFrame extends JFrame

{

    private JLabel inputL;

                       

    private JTextField inputTF;

     

    private static JTextArea outputTA;

     

    private JScrollPane scrollText;//creates the scroll feature for JTextArea

                                                          

    private JButton calculateB, exitB, clearB;

    private CalculateButtonHandler cbHandler;

    private ExitButtonHandler ebHandler;

    private ClearButtonHandler clbHandler;

     

    private static int WIDTH = 1024;

     private static int HEIGHT = 750;

         

   public eratosthenesJFrame()

     

    {

     Font font = new Font("Courier New", Font.BOLD, 20);

    Font font2 = new Font("Verdana Bold", Font.PLAIN, 32);

    Font font3 = new Font("Times New Roman", Font.BOLD, 20);

         

    JLabel inputL = new JLabel("Please enter an integer to find"+

                          " all the prime numbers up to that number:");

    inputL.setFont(font3);

                           

    inputTF = new JTextField(3);

    inputTF.setFont(font2);

     

    outputTA = new JTextArea("",10,10);

    outputTA.setFont(font);

    outputTA.setBorder(BorderFactory.createEmptyBorder(50, 50, 20, 40));

    outputTA.setLineWrap(true);

   outputTA.setWrapStyleWord(true);

       

       // the code below is for adding an image to the background of the window

        

           final ImageIcon imageIcon = new ImageIcon("eratosthenes_sieve.png");

          outputTA = new JTextArea("",10,10) {

         Image image = imageIcon.getImage();

      Image grayImage = GrayFilter.createDisabledImage(image);

      {

        setOpaque(false);

      }

      public void paint(Graphics g) {

        g.drawImage(grayImage, 0, 0, this);

        super.paint(g);

      }

    };

    outputTA.setFont(font);

    outputTA.setBorder(BorderFactory.createEmptyBorder(50, 50, 20, 40));

    outputTA.setLineWrap(true);

     outputTA.setWrapStyleWord(true);

         

     

     scrollText = new JScrollPane(outputTA);//adds the scroll feature to the outputTA

     scrollText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

  

         

     calculateB = new JButton("Calculate");

      cbHandler = new CalculateButtonHandler();

      calculateB.addActionListener(cbHandler);

     calculateB.setFont(font2);

      

     exitB = new JButton("EXIT");

      ebHandler = new ExitButtonHandler();

      exitB.addActionListener(ebHandler);

     exitB.setFont(font2);

      

     clearB = new JButton("Clear All");

      clbHandler = new ClearButtonHandler();

      clearB.addActionListener(clbHandler);

     clearB.setFont(font2);

      

     setTitle("Sieve of Erastosthenes");       

             

      Container myWindow = getContentPane();

      myWindow.setLayout(null);

      

           calculateB.setLocation(794, 5);

          exitB.setLocation(525, 665);

          clearB.setLocation(250, 665);

          inputL.setLocation(6,5);

          inputTF.setLocation(608,5);

          scrollText.setLocation(5,50);     

             

          

          calculateB.setSize(210,40);

            exitB.setSize(225, 40);

          clearB.setSize(225, 40);

          inputL.setSize(660,40);

          inputTF.setSize(185,40);

          scrollText.setSize(1000,610);

           

          myWindow.add(calculateB);         

            myWindow.add(exitB);

          myWindow.add(clearB);

          myWindow.add(inputL);

          myWindow.add(inputTF);

          myWindow.add(scrollText);

          

           

          setSize(WIDTH, HEIGHT);

        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);      

      }

       

private class CalculateButtonHandler implements ActionListener

{

     public void actionPerformed(ActionEvent e)

     {

          String arraySizeStr;

          int arraySize;       

     

            arraySize = Integer.parseInt(inputTF.getText());

            int y = (int) sqrt(arraySize);

      boolean[] notPrime = new boolean[arraySize + 1];// Must add 1 to the

                                            // array since the array starts out

                                            // at 0, otherwise the arraySize is not

                                            // big enough and will give an

                                            // ArrayIndexOutOfBoundsException        

      for (int num = 2; num <= y; num++)// will run through the entire

                                                     // for loop as long as num is

                                                     // less than or equal to the

                                                     // user supplied arraySize.

                                                     // Must start with 2 for it

                                                     // to work, though.        

        {

            if (!notPrime[num]) //if the array's expression (num) is

                                            //NOT not prime then it will prlong out

                                            //the prime number. So these numbers

                                            //will be prime and then numbers that

                                            //are divisible by this number will

                                            //be put in the notPrime array      

                                            //Without this if statement, the

                                            //program will prlong every number

                                            //in the array starting with 2           

            {

                  outputTA.append(num + " ");// Printss each Prime Number

                                                            // one at a time. The number

                                                            //   two prints right away.

                         

                  for (int k = num*num ; k <= arraySize; k += num)

                           //sets k equal to 2 times 2 initially.

                           //So k = 4 and 4 will be discarded longo the notPrime

                            //array. k then updates to k + num (which is 4 + 2).

                           //So now 6 is discarded..and so on.Until k is greater

                           //than the users input for the arraySize.

                            //This FOR LOOP gets rid of all of the multiples of

                            //the next prime number                            

                      

                 

                     notPrime[k] = true;//numbers not primes go here

               }

         }

                     

                     for (int n = y; n <= arraySize;n++)

                     if (!notPrime[n])

                  outputTA.append(n + " ");                  

     }

             

                     

                                     

}  

         

     private class ExitButtonHandler implements ActionListener

       {

       public void actionPerformed(ActionEvent e)

       {

           System.exit(0);//allows the exit button to close the JFrame window      

       }

    }// End of EXIT BUTTON actionPerformed  

      

         private class ClearButtonHandler implements ActionListener

    {

       public void actionPerformed(ActionEvent e)

       {

                 inputTF.setText("");

              outputTA.setText("");

              inputTF.requestFocusInWindow();

       }

    }

   public static void main(String[] args)

   {    

       eratosthenesJFrame sieveObject = new eratosthenesJFrame();

       sieveObject.setLocationRelativeTo(null);

   }   

}