An int greater than 1 is said to be prime if it is divisi- ble by only 1 and itself. For example, 2, 3, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not. Write an application that takes two numbers (representing a lower bound and an upper bound) and determines all of the prime numbers within the specified bounds, inclusive. Your application should appear as in Fig. 12.32.



a) Copying the template to your working directory. Copy the C:Examples Tutorial12ExercisesPrimeNumbers directory to your C:SimplyJava directory.

b) Opening the template file. Open the PrimeNumbers.java file in your text editor.

c) Adding a method to determine if a number is prime. On line 154, add a comment indicating that the method will determine if a number is a prime number. On line 155, add the method header for this method. The method will be called isPrime. This method returns a value of type boolean and takes an argument of type int. Name the parameter number. On line 156, add a left brace to begin the body of the method. On line 158, add the right brace to end the body of the method. Follow the brace with a comment indicating the end of this method. You will add functionality to this method in the next step.

d) Dealing with the number 1. The number 1 is not a prime number. On line 157, add a comment stating that 1 is not a valid prime number. On line 158, add an if statement t


```
1 // PrimeNumbers.java
2 // Determines all prime numbers between a user-specified range.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7
8 public class PrimeNumbers extends JFrame
9 {
10 // JLabel and JTextField for lower bound
11 private JLabel lowerBoundJLabel;
12 private JTextField lowerBoundJTextField;
13
14 // JLabel and JTextField for upper bound
15 private JLabel upperBoundJLabel;
16 private JTextField upperBoundJTextField;
17
18 // JLabel, JTextArea and JScrollPane for displaying prime numbers
19 private JLabel primeNumbersJLabel;
20 private JTextArea primeNumbersJTextArea;
21 private JScrollPane primeNumbersJScrollPane;
22
23 // JButton initiates calculation and display of prime numbers
24 private JButton calculatePrimesJButton;
25
26 // no-argument constructor
27 public PrimeNumbers()
28 {
29 createUserInterface();
30 }
31
32 // create and position GUI components; register event handlers
33 private void createUserInterface()
34 {
35 // get content pane for attaching GUI components
36 Container contentPane = getContentPane();
37
38 // enable explicit positioning of GUI components
39 contentPane.setLayout( null );
40
41 // set up lowerBoundJLabel
42 lowerBoundJLabel = new JLabel();
43 lowerBoundJLabel.setBounds( 16, 16, 80, 21 );
44 lowerBoundJLabel.setText( "Lower bound:" );
45 contentPane.add( lowerBoundJLabel );
46
47 // set up lowerBoundJTextField
48 lowerBoundJTextField = new JTextField();
49 lowerBoundJTextField.setBounds( 104, 16, 56, 21 );
50 lowerBoundJTextField.setHorizontalAlignment(
51 JTextField.RIGHT );
52 contentPane.add( lowerBoundJTextField );
53
54 // set up upperBoundJLabel
55 upperBoundJLabel = new JLabel();
56 upperBoundJLabel.setBounds( 16, 56, 80, 21 );
57 upperBoundJLabel.setText( "Upper bound:" );
58 contentPane.add( upperBoundJLabel );
59
60 // set up upperBoundJTextField
61 upperBoundJTextField = new JTextField();
62 upperBoundJTextField.setBounds( 104, 56, 56, 21 );
63 upperBoundJTextField.setHorizontalAlignment(
64 JTextField.RIGHT );
65 contentPane.add( upperBoundJTextField );
66
67 // set up primeNumbersJLabel
68 primeNumbersJLabel = new JLabel();
69 primeNumbersJLabel.setBounds( 16, 100, 96, 21 );
70 primeNumbersJLabel.setText( "Prime numbers:" );
71 contentPane.add( primeNumbersJLabel );
72
73 // set up primeNumbersJTextArea
74 primeNumbersJTextArea = new JTextArea();
75 primeNumbersJTextArea.setEditable( false );
76
77 // set up primeNumbersJScrollPane
78 primeNumbersJScrollPane = new JScrollPane(
79 primeNumbersJTextArea );
80 primeNumbersJScrollPane.setBounds( 16, 120, 144, 96 );
81 contentPane.add( primeNumbersJScrollPane );
82
83 // set up calculatePrimesJButton
84 calculatePrimesJButton = new JButton();
85 calculatePrimesJButton.setBounds( 16, 232, 144, 23 );
86 calculatePrimesJButton.setText( "Calculate Primes" );
87 contentPane.add( calculatePrimesJButton );
88 calculatePrimesJButton.addActionListener(
89
90 new ActionListener() // anonymous inner class
91 {
92 // event handler called when
93 // calculatePrimesJButton is clicked
94 public void actionPerformed( ActionEvent event )
95 {
96 calculatePrimesJButtonActionPerformed( event );
97 }
98
99 } // end anonymous inner class
100
101 ); // end call to addActionListener
102
103 // set properties of application's window
104 setTitle( "Prime Numbers" ); // set title bar string
105 setSize( 184, 296 ); // set window size
106 setVisible( true ); // display window
107
108 } // end method createUserInterface
109
110 // calculate and display primes using method prime
111 private void calculatePrimesJButtonActionPerformed(
112 ActionEvent event )
113 {
114 // declare variables and get values
115 int lowerBound = Integer.parseInt(
116 lowerBoundJTextField.getText() );
117 int upperBound = Integer.parseInt(
118 upperBoundJTextField.getText() );
119 int counter;
120 primeNumbersJTextArea.setText( "" );
121
122 if ( ( lowerBound <= 0 ) || ( upperBound <= 0 ) )
123 {
124 JOptionPane.showMessageDialog( null,
125 "Bounds must be greater than 0", "Invalid Bounds",
126 JOptionPane.ERROR_MESSAGE );
127 }
128
129 else if ( upperBound < lowerBound )
130 {
131 JOptionPane.showMessageDialog( null,
132 "Upper bound cannot be less than lower bound",
133 "Invalid Bounds", JOptionPane.ERROR_MESSAGE );
134 }
135
136 else
137 {
138 // loop from lower bound to upper bound
139 for ( counter = lowerBound; counter <= upperBound;
140 counter++ )
141 {
142 // if prime number, display in primeNumbersJTextArea
143 if ( isPrime( counter ) )
144 {
145 primeNumbersJTextArea.append( counter + "\n" );
146 }
147
148 } // end for loop
149
150 } // end else
151
152 } // end method calculatePrimesJButtonActionPerformed
153
154 // determine if number is prime
155 private boolean isPrime( int number )
156 {
157 // 1 is not a valid prime number
158 if ( number == 1 )
159 {
160 return false;
161 }
162
163 // set square root of number as limit
164 int limit = ( int ) Math.sqrt( number );
165
166 // loop until count reaches square root of number
167 for ( int count = 2; count <= limit; count++ )
168 {
169 if ( ( number % count ) == 0 )
170 {
171 return false; // number is not prime
172 }
173 } // end for loop
174
175 return true; // number is prime
176
177 } // end method isPrime
180 // main method
181 public static void main( String[] args )
182 {
183 PrimeNumbers application = new PrimeNumbers();
184 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
185
186 } // end method main
187
188 } // end class PrimeNumbers
```

Computer Science & Information Technology

You might also like to view...

What protection is needed for a banking organization’s internal 802.3 network?

a. WEP b. WPA c. WPA-2 d. None

Computer Science & Information Technology

When you create a class and do not provide a(n) ____, Java automatically supplies you with a default one.

A. constructor B. argument C. header D. name

Computer Science & Information Technology

Infusion is the union of difference biometric modalities resulting in an integrated system.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Following are the ages of students in a math class at a local community college. 18, 18, 19, 19, 19, 20, 20, 22, 22, 22, 23, 23, 25, 27, 42, 43, 57 Construct a graph that is appropriate for these data. Discuss the "spread" of this graph as it pertains to the average age of a student in the class.

What will be an ideal response?

Computer Science & Information Technology