Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java question

Options
  • 02-12-2012 3:11pm
    #1
    Registered Users Posts: 300 ✭✭


    Hi, have a quick question in Java:

    if I have 2 classes in the same program with main method each, how can I control which one to execute? Could I create an extra class to control them 2 methods?

    Thanks


Comments

  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    Is there a need for 2 main methods? Why can't the code from one be put into a method?


  • Registered Users Posts: 300 ✭✭Tomas_S


    Kavrocks wrote: »
    Is there a need for 2 main methods? Why can't the code from one be put into a method?

    I'm new to this so sorry for being stupid :D

    I would have 2 classes, each creates a different object. So user should be able to choose which one.

    So I have control method for each class.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    You should post your code, it's not very clear what you mean. Be sure to use the [code] tags!


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    Would you not just use an if statement?

    Object o;

    if (userChoice == choice1)
    {
    o = new class1();
    }

    else
    {
    o = new class2();
    }


  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    Tomas_S wrote: »
    Hi, have a quick question in Java:

    if I have 2 classes in the same program with main method each, how can I control which one to execute? Could I create an extra class to control them 2 methods?

    Thanks
    You should only have one main method for the program. What I would do is create a 3rd class which contains the main method and whatever control methods you need to create an object from either of the two classes you're using.


  • Advertisement
  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    OP a main method should really be considered as not being part of any Object.
    As Zero says, I would always have a standalone class that contains the main method and creates any other objects as required.

    IMO there is a danger while you are learning OOP to think in a procedural fashion, I think you might be falling into this trap if you are creating main methods in all of your classes. If your classes just contain main methods that do all the work in a step-by-step fashion then you are missing the point.

    If you post your class(es) we can take a look and advise you, as the sooner you get your head around OO concepts the easier it will be in the long run.


  • Registered Users Posts: 300 ✭✭Tomas_S


    Thank's for all replies however can't really post any code here.

    What I was asked to do in college was to create an interface, 2 different classes without inheritance, 2 control programs one for each class with a main method in each control program.

    Classes have different inputs ( scanner and swing ).

    Now I wasnt asked to create a control program which would let user to choose which class he wants to pick and which main method to execute. Just I thought it's possible to have an extra class with another main method which would control 2 main methods.

    Hope you know what I mean.


  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    Tomas_S wrote: »
    Thank's for all replies however can't really post any code here.

    What I was asked to do in college was to create an interface, 2 different classes without inheritance, 2 control programs one for each class with a main method in each control program.

    Classes have different inputs ( scanner and swing ).

    Now I wasnt asked to create a control program which would let user to choose which class he wants to pick and which main method to execute. Just I thought it's possible to have an extra class with another main method which would control 2 main methods.

    Hope you know what I mean.
    It's still not clear why you would need more than one main method in your program, it doesn't make any sense from a design point of view. For your interface I presume you are using a Swing GUI? The class that creates that will have a main method. From that class, and using the GUI interface, you should be able to create new objects from the two classes and call methods on those objects to execute the code from the class you wish to use. Those methods can just be regular methods, and you can have the control program for that particular class inside that method and it will be executed when the method is called.

    But if you really do need to call the main methods you can do it using something like this:

    public class ClassA {
    public static void main (String [] args) {
    if (ClassB) {
    ClassB.main(args);
    } else {
    ClassC.main(args);
    }
    }
    }


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Tomas_S wrote: »
    Thank's for all replies however can't really post any code here.

    What I was asked to do in college was to create an interface, 2 different classes without inheritance, 2 control programs one for each class with a main method in each control program.

    Classes have different inputs ( scanner and swing ).

    Now I wasnt asked to create a control program which would let user to choose which class he wants to pick and which main method to execute. Just I thought it's possible to have an extra class with another main method which would control 2 main methods.

    Hope you know what I mean.
    I don't think what you want exists. Either you have 2 separate programmes with 2 main methods, or you have one main method which chooses which class to launch. Second is preferable. But sounds like you were asked for the first.


  • Registered Users Posts: 300 ✭✭Tomas_S


    I know what you mean by all that.

    Just not clear really what am I exactly asked to do.Interface only specifies methods for both classes that's all. So I have to use same methods in 2 classes but 1 is swing and other is non swing.

    Now both classes have only defined methods that's all and i'm asked to create control programs for both of them.

    So in every control program an object has to be created so I can use methods?

    Sorry for being stupid aswell only new to Java.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Your interface will provide a bare bones structure of your methods (just empty methods with signatures etc), your classes will implement this interface and have to code the methods declared in the interface - in any way that you want. The idea is that you can know the rough format of the class, but implement it in different ways. An interface leaves the implementation method to you.

    Sounds like you do not need to have one main method launching both, you just need two separate programs with two separate main controlling methods. The idea of the exercise is to teach you about interfaces and their purpose, so I wouldn't worry too much about the main methods. In each main method you make an object 'instance' of the class you want to use yes.


  • Registered Users Posts: 300 ✭✭Tomas_S


    Your interface will provide a bare bones structure of your methods (just empty methods with signatures etc), your classes will implement this interface and have to code the methods declared in the interface - in any way that you want. The idea is that you can know the rough format of the class, but implement it in different ways. An interface leaves the implementation method to you.

    Sounds like you do not need to have one main method launching both, you just need two separate programs with two separate main controlling methods. The idea of the exercise is to teach you about interfaces and their purpose, so I wouldn't worry too much about the main methods. In each main method you make an object 'instance' of the class you want to use yes.

    Yes I think that's exactly what I have to do so I will leave it as it is.

    Thanks!

    Oh and would anyone recommend some small project in Java? I learn better in practice then just reading a book.


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    Classes have different inputs ( scanner and swing )
    At a guess, I would think what the lecturer wants is two programs to manage the two UI but with some basic logic implemented via the interface ... maybe to demostrate the concept of Contracts or perhaps as a precursor to MVC?

    ps. Tar.Aldarion got there before me...


  • Registered Users Posts: 891 ✭✭✭Mmmm_Lemony


    If you are using netbeans, adding swing JFrame forms automatically generates a main method within it.

    By right clicking on the project, selecting properties, then from the panel on the left select 'run', you can assign which file is ran first when the project is ran. By default, this will be the main class created when netbeans initially creates a new project.


  • Closed Accounts Posts: 799 ✭✭✭Logical_Bear


    Tomas_S wrote: »

    Oh and would anyone recommend some small project in Java? I learn better in practice then just reading a book.

    Here's a few sites with projects for people to do

    http://codekata.pragprog.com/2007/01/code_kata_backg.html#more

    http://www.codechef.com/

    http://codingbat.com/


Advertisement