Results 1 to 18 of 18
  1. #1
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default run another class within a class

    I have a class doing the system.out.println stuff in class A

    but I have to run class B, during that, also show out the result of class A

    how to do it? I tried to import but seems not working???

    thank you very much

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Your post is confusing and lacks information. A guess, you create an instance of one class in the other and call its methods. Perhaps post some code as an example of what you are trying to do. Don't forget the code tags.

  3. #3
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    below is part of the code for class A, all I want to do in class A is to print out sth
    public void show() {

    System.out.println("aaaaaaaaaa");
    }
    after that, in class B
    while running class B, I would also want to run class A

    just like it can be done in one file, but I must do it in a separate way....

  4. #4
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    and I dont mean call a method? but really load all the stuff from class A in class B.........

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    I'm still not sure what you want...
    Java Code:
    class A {
        public void show() {
            system.out.println("hello");
        }
    }
    
    class B {
        public void someMethod() {
            A foo = new A();
            foo.show();
        }
    }
    Or maybe this is what you want
    Java Code:
    class A {
        public static void main(String[] args) {
            A foo = new A();
            a.run();
        }
    
        public void run() {
            B bar = new B(this);
            bar.someMethod();
        }
    
        public void show() {
            system.out.println("hello");
        }
    }
    
    class B {
        A foo;
    
        Bar (A a) {
            foo = a;
        }
    
        public void someMethod() {
            foo.show();
        }
    }

  6. #6
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    lol......such that simple
    I used the first way but it turns out cannot find symbol symbol : constructor?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    You are calling a constructor that does not exist. Either add that constructor to the class or call one of the other constructors that do exist.

  8. #8
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    how to add? ...

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Huh?

    You place your cursor inside the class and start typing.

  10. #10
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    I dont understand =_________=

  11. #11
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    maybe I put my code up.........
    class WordList
    import java.util.Vector;
    import java.util.Random;

    public class WordList
    {
    protected Vector words;
    protected Random random;

    /**
    * Construct a word list capable of holding "capacity" words
    */
    public WordList(int capacity) {

    words = new Vector(capacity);
    random = new Random();
    }

    /**
    * Return true if the given word is already in the list
    */
    public boolean find(String word)
    {
    boolean found = false;
    for (int i=0;i<words.size();i++)
    if (words.elementAt(i).equals(word))
    {
    found = true;
    break;
    }
    return found;
    }

    /**
    * Add a word to the word list if it is not already there
    */
    public void add(String word) {
    if (find(word) == false)
    words.addElement(word);
    }

    /**
    * Return the size of the current word list
    */
    public int size() {

    return words.size();
    }

    /**
    * Return word at postion i
    */
    public String select(int i) {

    return (String)words.elementAt(i);
    }

    /**
    * Returns a random word from the word list
    */
    public String selectAny()
    {
    int i = (int)(random.nextFloat()*words.size());
    return select(i);
    }

    /**
    * Print the list of unique words
    */
    public void show() {
    /*for (int i=0;i<words.size();i++){*/
    System.out.println("aaaaaaaaaa");
    /*}*/
    }
    }
    class WordCount
    import java.io.*;
    import java.util.*;

    public class WordCount
    {

    public void xyz() {
    WordList method = new WordList();
    method.show();
    }

    }
    thank you........

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    public WordList(int capacity) { // your constructor
    
    WordList method = new WordList(); // creating a WordList object
    Notice how the parameters do not match. Your options already provided above.

  13. #13
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    so what should I do?

    i tried to change WordList() into WordList(int capacity) but it does not work?

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    System.out.println(String "hello");
    Is that correct? Think about what you do have to pass as a parameter. Don't just guess.

  15. #15
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    !!!!! replace it by Sting right? D:D
    but then Exception in thread "main" java.lang.NoSuchMethodError: main
    ..........................

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    What did I say about guessing?

  17. #17
    crunchichichi is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    What do you mean :S.........

  18. #18
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Programmers need to have good problem solving skills. You have a problem, Junky gave you a hint, and you need to try to solve it yourself.

    "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."

Similar Threads

  1. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  2. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  3. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 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
  •