Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Noob that needs some clearing up with Java style programming

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Noob that needs some clearing up with Java style programming

    Hello everybody. I have experience with c++ and c programming. I wasn't expecting Java to give me such a difficult time, but I am having a hard time grasping in.

    In c/c++, you have a main function and other functions that come up as you call them from your main function. I see this isn't the case in Java. For example, I am studying the following code from Sam's Teach yourself java in 24 hours:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    class PrimeFinder extends JFrame implements Runnable, ActionListener {
        Thread go;
        JLabel howManyLabel = new JLabel("Quantity: ");
        JTextField howMany = new JTextField("400", 10);
        JButton display = new JButton("Display primes");
        JTextArea primes = new JTextArea(8, 40);
     
        PrimeFinder() {
            super("Find Prime Numbers");
            setSize(400, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BorderLayout bord = new BorderLayout();
            setLayout(bord);
            display.addActionListener(this);
     
            JPanel topPanel = new JPanel();
            topPanel.add(howManyLabel);
            topPanel.add(howMany);
            topPanel.add(display);
            add(topPanel, BorderLayout.NORTH);
     
            primes.setLineWrap(true);
            JScrollPane textPane = new JScrollPane(primes);
            add(textPane, BorderLayout.CENTER);
     
            setVisible(true);
        }
     
        public void actionPerformed(ActionEvent event) {
            display.setEnabled(false);
            if (go == null) {
                go = new Thread(this);
                go.start();
            }
        }
     
        public void run() {
            int quantity = Integer.parseInt(howMany.getText());
            int numPrimes = 0;
            // candidate: the number that might be prime
            int candidate = 2;
            primes.append("First " + quantity + " primes:");
            while (numPrimes < quantity) {
                if (isPrime(candidate)) {
                    primes.append(candidate + " ");
                    numPrimes++;
                }
                candidate++;
            }
        }
     
        public static boolean isPrime(int checkNumber) {
            double root = Math.sqrt(checkNumber);
            for (int i = 2; i <= root; i++) {
                if (checkNumber % i == 0) {
                    return false;
                }
            }
            return true;
        }
     
        public static void main(String[] arguments) {
            PrimeFinder fp = new PrimeFinder();
        }
    }

    I see that main calls PrimeFinder(), but from there the other functions aren't called. Why does this work? does run() start up on its own because Runnable is implemented at the top? In c/c++ I would call the run() from withing my main(). actionPerformed isn't even called from what I can see. What makes this work? Sorry for the newb question. I just really want to learn this stuff. Thanks in advance
    Last edited by looneylu; January 23rd, 2011 at 08:06 PM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Noob that needs some clearing up with Java style programming

    What makes it work is the Swing/AWT event dispatch thread (EDT). Once a Swing container is realized in the main method the EDT runs until the program exits. Much of what you need is handled for you (eg events on components are dispatched to registered listeners, paint methods are called as needed, etc...)

  3. The Following User Says Thank You to copeg For This Useful Post:

    looneylu (January 23rd, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noob that needs some clearing up with Java style programming

    Quote Originally Posted by copeg View Post
    What makes it work is the Swing/AWT event dispatch thread (EDT). Once a Swing container is realized in the main method the EDT runs until the program exits. Much of what you need is handled for you (eg events on components are dispatched to registered listeners, paint methods are called as needed, etc...)
    So I guess the c/c++ way of thinking isn't necessary... am I really just over thinking things?

  5. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Noob that needs some clearing up with Java style programming

    Quote Originally Posted by looneylu View Post
    So I guess the c/c++ way of thinking isn't necessary... am I really just over thinking things?
    Wouldn't save over-thinking things...the basics between the languages are very similar, but java has a lot of API behind it (particularly when it comes to the GUI - as the above demonstrates) that is hard to swallow in one gulp. Take it one step at a time, and with a background in c/c++ you are already one step ahead.

  6. The Following User Says Thank You to copeg For This Useful Post:

    looneylu (January 24th, 2011)

Similar Threads

  1. Trying to achieve Movie Style Rolling Credits
    By javanoob123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 10th, 2011, 07:02 PM
  2. Programming noob here, need help!
    By artenuga54 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2010, 04:04 PM
  3. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM
  4. Need Help with my hmwk! Java noob!
    By ravij in forum Loops & Control Statements
    Replies: 4
    Last Post: October 7th, 2009, 01:02 AM