This applet I'm supposed to write needs to have a class that checks the range from the low number entered to the highest and gets and prints all the prime numbers found, as well as the whole number square roots, as well as the sum of the whole square roots found. These need to be separate methods in a class and I'm pretty sure it needs to use ActionListeners/event handling. I haven't tried to write the code for the last two methods yet or make the layout look nice, I'm too busy trying to figure out why it compiles but refuses to start the getPrime method. It compiles fine. Could anybody please help me? If you could help me over AIM my aim is peperonynchease. Thank you in advance!
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class hw1 extends JApplet
{
private JTextField low, high;
public int highNo, lowNo;
JTextArea checkRangeArea, doAllArea;
// Initializes applet
public void init()
{
Container content = getContentPane();
content.setLayout(new FlowLayout());
JLabel lowLabel = new JLabel("Low: ");
content.add(lowLabel);
low = new JTextField(10);
content.add(low);
JLabel highLabel = new JLabel("High: ");
content.add(highLabel);
high = new JTextField(10);
content.add(high);
checkRangeArea = new JTextArea(4, 4);
content.add(checkRangeArea);
// The button for checking the ranges is added
JButton checkRange = new JButton("Check Range");
checkRange.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
final String lowString = low.getText();
int lowInt = Integer.parseInt(lowString);
lowNo = lowInt;
final String highString = high.getText();
int highInt = Integer.parseInt(highString);
highNo = highInt;
if (lowInt > highInt)
{
checkRangeArea.setText(null);
JOptionPane.showMessageDialog(null, "Sorry, the numbers you "
+ " have entered are not valid! Please ensure that the number in the LOW "
+ " box is lower than the number in the HIGH box.", "OOPS!", JOptionPane.ERROR_MESSAGE);
}
else
checkRangeArea.append("Ok!");
} // end actionPerformed
} // end actionListener
); // end addActionListener
content.add(checkRange);
checkRange.setActionCommand("cr");
// Button for performing multiple computations is created and added
JButton doAll = new JButton("Do All");
content.add(doAll);
checkRangeArea = new JTextArea(14, 14);
content.add(checkRangeArea);
doAllArea = new JTextArea(24, 24);
content.add(doAllArea);
ActionListener myListener = new myListenerClass(doAllArea, highNo, lowNo);
doAll.addActionListener(myListener);
}
}
// New class for computing methods
class myListenerClass extends JApplet implements ActionListener
{
JTextArea myText;
int highNum, lowNum;
public myListenerClass(JTextArea tempText, int noHigh, int noLow)
{
myText = tempText;
highNum = noHigh;
lowNum = noLow;
}
public void actionPerformed(ActionEvent evt)
{
getPrimes(myText, highNum, lowNum);
}
// Gets the prime numbers from the range
public void getPrimes(JTextArea primeText, int lowPrime, int highPrime)
{
// Checks to make sure low is lower than high
if (lowPrime > highPrime)
JOptionPane.showMessageDialog(null, "Sorry, the numbers you "
+ " have entered are not valid! Please ensure that the number in the LOW "
+ " box is lower than the number in the HIGH box.", "OOPS!",
JOptionPane.ERROR_MESSAGE);
// Gets the range for the two numbers entered
int range = highPrime-lowPrime;
int noPrimes = 0;
int l = 0;
boolean primeNumber = true;
int [] primeArray = new int[10];
// Creates an array with appropriate amounts of numbers inside
int [] rangeArray = new int[range];
for (int i = 0; i <= range; i++)
{
rangeArray[i] = lowPrime;
lowPrime++;
}
// Checks for prime numbers
for (int j = lowPrime; j < highPrime; j++)
{
// If divisible by two, it's not a prime number
if (rangeArray[j]%2==0)
primeNumber = false;
// Checks if number is divisible by any odd number
else
for (int k = 3; k <= ((rangeArray[j])/2); k+=2)
{
if (rangeArray[j]%2 == 0)
primeNumber = false;
}
// If a prime number is found, add it to the array
if (primeNumber)
{
primeArray[l] = j;
l++;
primeText.append("Primes found!");
}
else
primeText.append("No primes found!");
}
}
}