Results 1 to 12 of 12
  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question 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));
    			
    			
    		}
    		
    	
    		
    		
    		
    	}
    		
    	}
    }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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)

  3. #3
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    i did the change , still no result though .

  4. #4
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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));
    			
    			
    		}

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    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:

  6. #6
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    ok , and how am i suppose to show if number is prime or not ? still doesnt show anythin in the textfield...

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    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:

  8. #8
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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));
    			
    			
    		}
    		
    	
    		
    		
    		
    	}
    		
    	}
    }

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    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:

  10. #10
    gbtimmon is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    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;
    	}
    
    }

  11. #11
    gbtimmon is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    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;
    	}
    a little debugging to the findMultiples method...

  12. #12
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

Similar Threads

  1. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 11:42 PM
  2. Replies: 10
    Last Post: 02-20-2010, 08:25 PM
  3. swirling becomes false,why?
    By arefeh in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 06:12 PM
  4. If statement executing when false...
    By ribbs2521 in forum New To Java
    Replies: 6
    Last Post: 10-26-2009, 05:19 PM
  5. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •