We help IT Professionals succeed at work.

UML / OCL (yes it's homework)

dovholuk
dovholuk asked
on
let me start that i know this is a homework question. i'm not looking for anything that i feel is out of control. i just want to know how to set up a class in java, that is defined by UML/OCL as:

methodName(in att1: Class1, out att2: Class2, out att3: Class3)

that's it.

so does this translate into:

public class MyNewClass
{
     public methodName(Class1 att1, Class2 att2, Class3 att3)
     {
          //code here
     }
}

??????

i'm making this worth 300 points cause i want to UNDERSTAND this notation. all the UML books i have don't help me much. my professor has explained to me that the "in" and "out" refer to whether or not the parameter is "used" by the caller or "set" by the caller.

can someone shed some light on this for me?  i'm no java expert, but i'm pretty confident in my coding.

my homework involves implementing MANY classes declared as above, but i don't have the slightest clue about how to get the "frame" setup properly.

speedy responses are very welcomed!

if anybody can give me a really slam-bang answer quickly, i'll even kick in another 200 points because i REALLY need this answer speedily.

if any other experts have an objection to my question, please let me know.  i've finally made my way to the top15 in MSAccess (please don't laugh *smile*) so i understand that people who ask homework questions (covertly) are the most dreaded of all experts.  i am not looking for an answer to my homework... i'm just looking to get going in the right direction!

thanks

dovholuk
Comment
Watch Question

hi...
Before i give u the method ..in Java let me explain u the UML notation
methodName(in att1: Class1, out att2: Class2, out att3: Class3)

Here 'in'-specifies that the parameter is an input parameter into the method.
and 'out'- specifies that this parameter is returned by the method after execution.
if ex: u want a method to add two numbers and get the result from a method then UML notation go like this
add(in a:integer,in b:integer,out sum:integer)

so now u'r method returns "att2 of type Class2" and "att3 of type Class3" and as java can return only one object(and it supports call-by-value) i'am enclosing these two output objects in another object Vector and returning a vector...
 
 public class MyNewClass
{
    public Vector methodName(Class1 att1)
    {
        Vector v=new Vector();
        Class2 att2;
        Class3 att3
         //code here
         v.addElement(att2);
         v.addElement(att3);
       return v;
    }
}

cheers....

CERTIFIED EXPERT

Author

Commented:
first, thanks for responding.

second, i'm going to reject your answer because you posted it as an answer. i would have perferred you use a comment instead. i see you're new to EE. read the policy below for tips on EE ettiquite.

trust me, if your answer turns out to be the best one, i WILL give you credit. however, i'm leaning toward not agreeing with you.

the issue is that the "out" parameters aren't returned. also, i can't return a vector. there's got to be a better way.

for example, (i tried to NOT use a homework example, but i think i might need to) here are two OCL specifications for 2 separate methods:

gethotelDetails(in match: String): HotelDetails[]
getRoomInfo(in res: ReservationDetails, out availability: Boolean, out price: Currency)
(see http://www.umlcomponents.com/UMLComponentsCaseStudy.pdf for the ACTUAL case study, refer to page 11)

for the LIFE of me, i can't figure this out.

thanks again for your comment.

dovholuk
CERTIFIED EXPERT

Author

Commented:
addendum:
gethotelDetails(in match: String): HotelDetails[]

clearly returns an array of "HotelDetails"

while the second example:
getRoomInfo(in res: ReservationDetails, out availability: Boolean, out price: Currency)

seemingly has NO return type (void maybe)

dovholuk
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
My understanding is that an in parameter is passed to the method and is not altered by the method, while an out parameter has it's value set by the method.
As far as how to map your definition to a Java class definition goes, the problem is that Java passes object references by value so a method cannot set an object to return via a parameter. But it could of course modify the state of a class passed to it. This means that as far as the method definition goes there is really no difference between in and out parameters.
CERTIFIED EXPERT

Author

Commented:
to all:
keep offering info. 200 points to whoever can give me more good info on this subject! (awarded in a separate "points for" question)

objects:
i think i'm tending to agree with you. where i ran into problems is with passing Strings from one class to the next, modifying the String and getting the modified value from the calling class, know what i mean?? i could do it with my own classes but not with String. i gotta work that out somehow! (i would imagine String is like custom classes, but i can't get the sytax correct...)

i'm going to award you the correct answer, but i'm going to leave this question open for another day in hopes that someone else either agrees with you or can give any more info.  that person will get 200 points as a "reward" as long as they give some good information. thanks for your help.



dovholuk
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Your problem with Strings is that they are 'immutable' objects, ie. you cannot change their contents (no available methods).
If you need to modify the internals of a string then instead use a StringBuffer object.

Commented:
Here is one _free_ comment:
[;-)]

---

In method With single out value - always use a function return.

---

There are two acceptable ways to make multiple out values in Java:

- creating a wrapper class which wraps around the data and has required access methods (setValue, getValue, ...)
Classes Boolean, Integer, Float, ... are created for these purposes (apart from some additional functionality), to wrap around primitive Java datatypes. For String you can create a StringWrapper class or use StringBuffer as objects said, although I use StringBuffer in situations where I have frequent modifications on String content, so I don't want to involve garbage collector too much.

- passing out parameter to a method as a single element array (which is not too nice, but you still have some parameter type control)

---

I hope that this first (prefered) option is clear.
For the second one here's a short example.

You can implement a method like this:

    void modify( String[] outputString )
    {
        if (outputString.length!=1)
        {
            throw new ClassCastException( "Parameter outputString must be a String[1] instance." );
        }
       
        if (outputString[0]==null)
        {
            outputString[0]="String was null";
        }
        else
        {
            if (outputString[0].equals( "When?" ))
            {
                outputString[0]="Now!";
            }
            else
            {
                outputString[0]=outputString[0]+" "+outputString[0];
            }
        }
    }

Now you can use it like this:
...
    String[] outputParameter = new String[1];
   
    modify( outputParameter );
    System.out.println( outputParameter[0] );
   
    outputParameter[0]="When?";
    modify( outputParameter );
    System.out.println( outputParameter[0] );
   
    modify( outputParameter );
    System.out.println( outputParameter[0] );
...

---

Best wishes,
    </ntr> :)
CERTIFIED EXPERT

Author

Commented:
alright time to put this puppy to bed!

i don't know if anything was resolved. all the options listed i'd thought of, but they helped me confirm that i was NOT crazy!

objects: knew about the immutability of strings, didn't know if there was a workaround with String. i didn't think there was, there isn't as you've confirmed. using the buffer isn't as it was diagrammed to me, so it wasn't an option. not to sound smug, but i'd thought of that too. i wasn't really sure though. you just put the nail in the coffin for me.

neutron:
thanks for the input. i liked your answer although it's not "acceptable". i can't create my own return objects.. that's part of my problem "understanding" the whole hulla-ballu... in any event, i liked your answer enough to post a points for question worth 200. NOTHING's free in this world! :)

thanks for the input.

dovholuk
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Thanks for the points :)

Commented:
hi dovholuk! :)

Thanx for the points, but there's no need really :-)
At the time I wrote my comment you already had an answer to the question, so I enjoyed giving some extra input.

Save the points for the next time! ;-)

Gretings,
    </ntr> :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.