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 7 of 7

Thread: Passing Variable between programs

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing Variable between programs

    Hi, I am a beginner in Java. Can anyone help me write a program where
    Program 1: Takes input from keyboard from user and check whether it is a number
    If yes then passes it to Program2 and Program3 esle return message to user saying entered is not a number.
    Program 2: Check number for even or odd and return value to Program 1 which inturn display to user.
    Program 3: Check number for prime and returns confirmation to program 1 which inturn display to user.

    ( if programs checks for 0 to 9 is fine, program1, 2 & 3 should be in different files, I mean three '.java' files.
    I know that all can be written in one program but I want to learn and understand how to write into multiple codes/projects which can interact)

    Case1: if user enters 7

    Output should be

    User entered is a number
    Number entered by user: odd
    Number entered by user: Prime


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Passing Variable between programs

    Different .java files are not different programs. A program usually consists of a multitude of classes, where each class is usually defined in its own .java file.
    If you actually need communication across programs this could be very very difficult. If you are simply supposed to have several classes it will be quite easy. But you have to elaborate your question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing Variable between programs

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Assuming your terminology doesn't accurately reflect the concept you have in mind, sharing info or data among different objects in the same project can be accomplished multiple ways:

    1. Pass the data via the Class’ constructor:

    // in the sending class
    receivingClass = new ReceivingClass( dataToPass );

    2. If an instance of the receiving class already exists, use a mutator (or setter) method to communicate the data from the sending class to the receiving class:

    // in the sending class:
    receivingClass.setDataToPass( dataToPass );

    3. Or, if the ReceivingClass object is being used to obtain the data from the SendingClass, use an accessor (or getter) method:

    // in the receiving class
    dataToPass = sendingClass.getDataToPass();

    In the program you've described, I think you'll have an opportunity to use all 3 approaches which may be the assignment's purpose.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Location
    Mumbai
    Posts
    10
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Passing Variable between programs

    Create main method in Program 1 check here whether number or not now create objects of Program 2 and program 3 now call methods of program 2 and 3 by passing the value from program 1 as a parameter.

  5. #5
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Passing Variable between programs

    What did you come up with? Doing homework for nothing is not me. It seems to me you got the problem set. All you need is an algorithm or steps to follow. But in program form. Write it in english first, then in Java codes. It will look clearer. I would like to see it too.

  6. #6
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Passing Variable between programs

    Hi again. This is an example of a simple program on the weather.

    It shows you a local variable called tep, reference data type String, and various other public classes in the same package.

    //temperature class
    public  class UseTemperatureNice {
     
      public static void main(String args[])  {
     TemperatureNice temp = new TemperatureNice();
    temp.setNumber(70.0);
    temp.setScale(TempScale.FAHRENHEIT);
    temp.display();
     
    temp.newTemperature(32.0);
    temp.display();
    temp = new Temperature(TempScale.CELSIUS);
    temp.display();
     
    temp = new TemperatureNice(2.73,TempScale.KELVIN);
    temp.display();
      }
    } //end class


    --- Update ---

    Hi again. This is an example of a simple program on the weather.

    It shows you a local variable called temp, reference data type String, and various other public classes in the same package.

    //temperature class
    public  class UseTemperatureNice {
     
    public static void main(String args[])  {
     TemperatureNice temp = new TemperatureNice();
    temp.setNumber(70.0);
    temp.setScale(TempScale.FAHRENHEIT);
    temp.display();
     
    temp.newTemperature(32.0);
    temp.display();
    temp = new Temperature(TempScale.CELSIUS);
    temp.display();
     
    temp = new TemperatureNice(2.73,TempScale.KELVIN);
    temp.display();
      }
    } //end class

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Passing Variable between programs

    This thread is three years old. (And the TemperatureNice code is out of a book.)

Similar Threads

  1. Passing variable from one class to another
    By kingsta in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 16th, 2013, 09:57 AM
  2. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  3. Passing as a parameter, an object + another variable
    By goochasauras in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2012, 11:13 PM
  4. Need help with passing user defined variable in SQLJ
    By redkryptonite in forum JDBC & Databases
    Replies: 1
    Last Post: March 7th, 2012, 05:47 AM
  5. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM