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

In Java. Write a program with a Graphical User Interface (GUI) that accepts an i

ID: 3767383 • Letter: I

Question

In Java.

Write a program with a Graphical User Interface (GUI) that accepts an integer number and displays if it is or is not a prime number. The program may be a JFrame or a JApplet. You may format the GUI in any way that works effectively. You may include a button in the GUI if you wish or you can rely on the user pressing enter in the JTextField. A prime number is one that is only evenly divisible by the number one and itself. A simple not necessarily efficient) way to determine if a number is prime is to try to divide it by all numbers from 2 up to the square root of the number. If X % Y is equal to zero, then Y evenly divides X. (% is the modulus operator.) Loop from 2 to the square root of the number and check if the number % loop index is equal to zero. If it is, the number is not prime. If no index evenly divides the number, then it is prime.

Explanation / Answer

Program :

import java.awt.*;
import javax.swing.*;


@SuppressWarnings("serial")
public class PrimeTest extends JApplet
{
   boolean       isPrime = true;
   long       testedValue = 2;
   long       divisor = 1;
   JTextField   theTextField;
  
   // Standard Applet procedures
  
   public void init()
   {
       setLayout(new FlowLayout(FlowLayout.LEFT));
      
       theTextField = new JTextField("2", 19);
       add(new Label("Number to test:"));
       add(theTextField);
       add(new Button("Check for primality"));
   }
  
   public boolean action(Event evt, Object arg)
   {
       if ( evt.target instanceof Button )
           DoTest();
      
       return true;
   }
  
  
public void paint(Graphics g)
{
   g.clearRect(0, 50, 200, 80);
  
if ( isPrime )
        g.drawString("" + testedValue + " is prime", 10, 60);
    else
        g.drawString("" + testedValue + " is not prime " , 10, 60);
}

   // Procedures specific to Primes
  
   public void DoTest()
   {
       String   numString;
       long   testPrime;
      
       numString = theTextField.getText();
       testPrime = Long.parseLong(numString);
      
       if ( testPrime < 2 )
           return /* nothing */ ;
          
       divisor = IsPrime(testPrime);
   if ( divisor == testPrime )
           isPrime = true;
   else
       isPrime = false;

       testedValue = testPrime;
      
       repaint();
}
  
public long IsPrime(long testPrime)
{
   long   test;
  
   // should at least use square root
   test = 2;
   while ( test < testPrime )
   {
       if ( testPrime % test == 0 )
           return test;
      
       if ( test == 2 )
           test++;
       else
           test += 2;
   }
  
return testPrime;
}
}

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