Passing Parameters


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Passing Parameters

  1. #1
    Join Date
    Feb 2004
    Posts
    4

    Passing Parameters

    Can anyone help me... I can't get my head round this!

    I have two seperate java files... javafile1.java and javafile2.java

    I want to pass three seperate integer values from the second java file to the first java file. Then once I have them in the first java file I can use them as variables to plot graphs.

    Can anyone post some idiot proof sample code of how to do this??

    Thanks!

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    well the class that holds the data needs to have a get method for the data, defined something like this
    Code:
    public int getInt()
    {
        return theInt;
    }
    and in the graph plotting file you'll make an instance of the class with the data in it, and call the get method from it to get the data. You can either have a seperate get method for each integer, or you could store the integers in an array and just have a get method for the array.

  3. #3
    Join Date
    Feb 2004
    Posts
    4
    if i include a bit of code, would be possible could someone show me how to implement my problem??

    myOpengl.java
    ---------------

    private static Receiver midi_receiver = new MidiReceiver(System.out, true);


    .............


    //midi file chooser
    public static void openFile() {

    String strFilename = null;

    { //show file chooser dialog
    JFileChooser chooser = new JFileChooser();
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
    { //construct reader and read products
    File selectedFile = chooser.getSelectedFile();
    strFilename = selectedFile.getName();
    midi(strFilename);
    }
    }
    }


    public static void midi(String strFilename) {
    //MIDI file data retreiver...
    String strMidiFilename = strFilename;
    File midiFile = new File(strMidiFilename);

    Sequence sequence = null;
    try {
    sequence = MidiSystem.getSequence(midiFile);
    }
    catch (InvalidMidiDataException e) {
    e.printStackTrace();
    System.exit(1);
    }
    catch (IOException e) {
    e.printStackTrace();
    System.exit(1);
    }


    if (sequence == null) {
    System.out.println("Cannot retrieve sequence");
    }

    else
    {
    Track[] tracks = sequence.getTracks();
    for (int nTrack = 0; nTrack < tracks.length; nTrack++)
    {
    //System.out.println("Track " + nTrack + ":");
    Track track = tracks[nTrack];
    for (int nEvent = 0; nEvent < track.size(); nEvent++)
    {
    MidiEvent event = track.get(nEvent);
    output(event);
    }
    }
    }
    }

    public static void output(MidiEvent event)
    {
    MidiMessage message = event.getMessage();
    long lTicks = event.getTick();
    midi_receiver.send(message, lTicks);
    }
    }

    =========================================

    MidiReceiver.java
    --------------------

    public class MidiReceiver implements Receiver {

    private PrintStream m_printStream;
    public String strMessage = null;

    public MidiReceiver(PrintStream printStream) {

    this(printStream, false);
    }


    public MidiReceiver(PrintStream printStream, boolean bPrintTimeStampAsTicks) {

    m_printStream = printStream;
    }


    public void close() {
    }


    public void send(MidiMessage message, long lTimeStamp)
    {
    if (message instanceof ShortMessage)
    {
    strMessage = decodeMessage((ShortMessage) message);
    }

    m_printStream.println(strMessage);

    }

    public String decodeMessage(ShortMessage message)
    {
    String strMessage = null;
    switch (message.getCommand())
    {
    case 0x90:
    if (message.getData2() != 0) {
    strMessage = "" + message.getData1();
    }
    break;
    }
    return strMessage;
    }
    }



    ------------
    i want to take the extracted int value from 'decodeMessage' and then pass it into the first java file, myopengl.java
    from there i can then make the values a global variable and use it within the myopengl.java file to draw shapes with the values.

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    decodeMessage() isn't returning an int, it's returning a string. I don't know what message.getData1(); returns, but if you say it's an int, why do you then store it in a string? Store it in whatever data type it is and return that.
    If you want to send the int to an instance of myOpengl then you're either going to have to create an instance of myOpengl in the instance of MidiReciever and have a public set method to pass the int values to, or you're going to have to write a static set method in MidiReciever so that you can set the values without having to create an instance of the class. Since I don't know what exactly it is you're doing or what your program structure is i don't know which is best.

  5. #5
    Join Date
    Feb 2004
    Posts
    4
    The string that I am taking from message.getData1() is simply being used to do a console print out to make sure that my code was getting the correct data that i wanted...

    sorry for the confusion there, i forgot to add in under that line another line of code...

    int MidiValue = message.getData1();

    there then I have the int that I want to pass back into the original myOpengl.java file

    could you explain how i can do that within the code that i posted above, im having serious difficulty in trying to understand what is happening. The more that I try to understand the more I get confused!!

    within my 'public String decodeMessage' this there any way that i can get the int MidiValue back into the first java file??

    im afraid im going to need an idiot proof guide!!

    thanks alot for the help,
    shane

  6. #6
    Join Date
    Feb 2004
    Posts
    4
    sorry, forgot to add, that when i have the int back in the myOpengl.java file i am then going to use it to draw shapes...

    so i really just want it as a variable in the myOpengl.java file so that i can play about with it in there.

  7. #7
    Join Date
    Feb 2004
    Posts
    541
    in the MyOpengl class declare some variables for the ints (i assume that there will always be 3 right?), then write a method something like
    Code:
    public void getIntValues(int val1, int val2, int val3)
    {
        this.val1 = val1; //this.val1 refers to the instance variable as opposed to the paramter val1
        this.val2 = val2;
        this.val3 = val3;
    }
    Then, when you have got the int values from the file you can call the method above to pass them across, and do whatever you want with them.

  8. #8
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by mikeBarr81
    Code:
    //this.val1 refers to the instance variable as opposed to the paramter val1
    hey.. punk!

    nooo.. dont teach them things like this! if the guy is having trouble passing values between objects, then that's gonna be way over their heads... do this instead:

    Code:
    public Constructor(int theValue1, int theValue2){
      value1 = theValue1;
      value2 = theValue2;
    }


    shane: you need to stop thinking in terms of "files".. you dont pass vales "between java files" - a java file is used to make a class file, and a class file is a blueprint, or a plan for, an object in memory.

    you make a new object with the NEW keyword:

    MyClass1 mc = new MyClass1();

    MyClass1 = the TYPE of the object
    mc = the NAME of the object

    if you want to pass data between 2 objects, you either make one aware of the other, or use a third class to handle the passing (best way):

    Code:
    //all this code is in a method in UberClass1, okay?
    
    MyClass1 mc1 = new MyClass1();
    MyClass2 mc2 = new MyClass2();
    
    int tempInt;
    
    //get the value out of mc1
    tempInt = mc1.giveMeYourValue();
    
    //pass it to mc2
    mc2.hereHaveThisValue(tempInt);
    
    
    //you can also (NOT encouraged)
    mc1.hey_GiveYourValueToThisGuy(mc2);
    
    //or
    mc2.hey_getYourValueFromThisGuy(mc1);
    in the second cases, you make one class aware of the other. that aware class then has to call "giveMeYourValue()" or "hereHaveThisValue()" - depends which way round you chose to do things.

    It is NOT encouraged because:

    - a class represents an object, and as far as possible, should be disconnected from other objects. if you make it so that mc1 knows how to get his value from mc2, it means that mc1 must be told about mc2's existence.

    it is better to keep them separate; mc1 knows how to give a value, mc2 knows what to do when it gets a value

    MC1 SHOULDNT CARE WHO HE GIVES HIS VALUE TO
    MC2 SHOULDNT CARE WHO HE GETS HIS VALUE FROM

    this is why using a third class to manage the 2 of them, and pass the values is better.. that way neither MyClass1 nor MyCLass2 needs to know of the existence of the other class, nor do they have to interact directly. this makes it easier to use just one of them in another project.. see?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  9. #9
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by mikeBarr81
    write a method something like
    Code:
    public void getIntValues(int val1, int val2, int val3)
    looks more like a "set" to me...
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  10. #10
    Join Date
    Feb 2004
    Posts
    541
    oh dear, what a terrible typo for me to make hehe. Anyway, about the use of the "this" keyword; I see your point but it was just the way I was taught it. I imagine if we sparked a debate it could end up like the "goto" debate Sorry if it caused any confusion

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center