Results 1 to 12 of 12
Thread: Prime Number - true , false
- 04-28-2010, 12:42 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Prime Number - true , false
hey guyz , im stuck at some point of this program , ive tried to write a applet that identifies prime numbers by using boolean . however as i try to test the program there's no result showed in textfield as i click on button.
could u take a look at it n help me to solve this out ?
Java Code:import java.awt.*; import java.applet.*; import java.awt.event.*; public class Prime1 extends Applet implements ActionListener { private Label enterno1,prime1; private TextField enterno2,prime2; private Button calculate,clear; public void init() { enterno1=new Label(" Enter your number : "); prime1=new Label(" Prime Form"); calculate=new Button("Calculate"); enterno2=new TextField(10); prime2=new TextField(10); add(enterno1); add(enterno2); add(prime1); add(prime2); add(calculate); calculate.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource()==enterno1){ int n=Integer.parseInt(enterno1.getText()); int num=0; boolean isPrime = true; for ( int i = 1; i <= num; i++ ){ isPrime = true; for(int y=2; y <= i/2; y++) { if ((i%y == 0)) isPrime = false; } enterno2.setText(Integer.toString(i)); } } } }
- 04-28-2010, 01:01 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
if (e.getSource()==enterno1)
will never be true, since you're only adding the listener to "calculate"
replace
if (e.getSource()==enterno1)
with
if (e.getSource()==calculate)
- 04-28-2010, 01:12 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
i did the change , still no result though .
- 04-28-2010, 01:14 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Java Code:public void actionPerformed(ActionEvent e){ if (e.getSource()==calculate){ int n=Integer.parseInt(enterno1.getText()); int num=0; boolean isPrime = true; for ( int i = 1; i <= num; i++ ){ isPrime = true; for(int y=2; y <= i/2; y++) { if ((i%y == 0)) isPrime = false; } enterno2.setText(Integer.toString(i)); }
- 04-28-2010, 02:19 PM #5
On top of everline of code or above meth you write as a comment what you want to do and it will be easy to debug.
In the below code u are trying to retreive from Label instead of Textfield.(enterno1)
Just check once and correct it.Ramya:cool:
- 04-28-2010, 02:43 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
ok , and how am i suppose to show if number is prime or not ? still doesnt show anythin in the textfield...
- 04-28-2010, 03:08 PM #7
Do it as a sequence like this below.
1.Create a Label with text "Enter a number"
2.Create a textfield named "InputNo".
3.Create a Label with text " Is it Prime"
4.Create a textfield named "output".
5.Create Calculate Button.
6.Add the components in the above order.
7.Finally addActionListener to Calculator
8. In actionPerformed get the input value as number from component no 2.
Do logic ..
9. Finally output is prime or not in component no 4.Ramya:cool:
- 04-29-2010, 06:59 AM #8
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
based on ur instructions i did below code , however still no result !! please try it urself n tell my whats the problem :
Java Code:import java.awt.*; import java.applet.*; import java.awt.event.*; public class Primee extends Applet implements ActionListener { private Label enternumber,Prime; private TextField inputno,output; private Button calculate,clear; public void init() { enternumber=new Label(" Enter a number : "); inputno=new TextField(10); Prime=new Label(" is the prime "); output=new TextField(10); calculate=new Button("Calculate"); add(enternumber); add(inputno); add(Prime); add(output); add(calculate); calculate.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getSource()==calculate){ int n=Integer.parseInt(inputno.getText()); int num=0; boolean isPrime = true; for ( int i = 1; i <= num; i++ ){ isPrime = true; for(int y=2; y <= i/2; y++) { if ((i%y == 0)) isPrime = false; } output.setText(Integer.toString(i)); } } } }
- 04-29-2010, 10:25 AM #9
u have added the listener and components correctly.
Develop the debugging skills.
check the logic you are performing.
You are getting the input from text and storing in "n".
Afterwards for loop is there,you have initialized num to 0.But in for loop u want iterate <=num.....How it will enter into the for loop?
Try Try and correct...each line youput comment and fix.print debugging statements.Ramya:cool:
- 05-01-2010, 10:48 PM #10
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
yo, working on learning swing, decided to work this out for you, take a gander. If you just wanted a prime solving tool -- this is it; if you're trying to learn to program i defiantly recommend looking at this closely and figuring out how it works. Hope it helps.
Java Code:import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SpringLayout; public class test extends JApplet implements ActionListener { private JPanel panel1, panel2; private JButton isPrimeButton; private JTextField primeInput; private JTextArea outputField; private ArrayList<Integer> multiples; public void init() { setLayout(new FlowLayout()); setSize(800,50); panel1 = new JPanel(true); panel1.setLayout(new FlowLayout()); panel2 = new JPanel(true); panel2.setLayout(new FlowLayout()); primeInput = new JTextField("Number"); outputField = new JTextArea("chose a number to find multiples, they will be displayed in this box."); isPrimeButton = new JButton("Find Mulitples"); isPrimeButton.addActionListener(this); isPrimeButton.setActionCommand("getPrime"); panel1.add(primeInput); panel1.add(isPrimeButton); panel2.add(outputField); add(panel1); add(panel2); } public void actionPerformed(ActionEvent e) { if ("getPrime".equals(e.getActionCommand())){ multiples = findMultiples(Integer.parseInt(primeInput.getText())); setLabels(multiples); } } private void setLabels(ArrayList<Integer> multi) { if (multi.isEmpty()){ outputField.setText("is a prime"); outputField.setVisible(true); repaint(); } else { outputField.setText("The string is divisable:" + multiples.toString()); outputField.setVisible(true); repaint(); } } private ArrayList<Integer> findMultiples(int prime) { ArrayList<Integer> multi = new ArrayList<Integer>(); for (int index = 2; index < prime / 2; index++) { if (prime % index == 0) { multi.add(index); } } return multi; } }
- 05-02-2010, 11:38 PM #11
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
a little debugging to the findMultiples method...Java Code:private ArrayList<Integer> findMultiples(int prime) { ArrayList<Integer> multi = new ArrayList<Integer>(); for (int index = 2; index < (prime / 2)+1; index++) { if (prime % index == 0) { multi.add(index); } } return multi; }
- 05-04-2010, 02:49 PM #12
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Similar Threads
-
Finding nth prime number
By dextr in forum New To JavaReplies: 2Last Post: 04-12-2010, 11:42 PM -
How to Determine prime number and making shape using loop?
By cyzash in forum New To JavaReplies: 10Last Post: 02-20-2010, 08:25 PM -
swirling becomes false,why?
By arefeh in forum New To JavaReplies: 2Last Post: 01-18-2010, 06:12 PM -
If statement executing when false...
By ribbs2521 in forum New To JavaReplies: 6Last Post: 10-26-2009, 05:19 PM -
Prime Number - System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04-26-2009, 01:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks