Home > Software engineering >  Java method that produces an object
Java method that produces an object

Time:08-18

I have to follow the below UML diagram to design a programme.

The programme is supposed to generate 2 random integers, and ask user for input the answer to the sum of said integers.

However, I am not sure how to make use of the method static void getTwoIntegers(MyInteger m, MyInteger n) , or what to put inside it.

I have tried initialising 2 MyInteger objects with MyInteger int_1 = new MyInteger(m); and (n) in this method, but get a "cannot be resolved" error everytime I call its getter method int_1.getInteger();

And since the method getTwoIntegers is void, I cannot just return 2 random integers. I'm truly stuck on how to utilise this method

Any ideas? Thanks so much

enter image description here

CodePudding user response:

Gardener's answer nailed it. For the records, I'd like to share some more thoughts.

This class diagram is misleading. The parameters of an UML operation have a direction that should be indicated in front of the parameter name. It can be in, out, inout. If the direction is omitted in the diagram, UML assumes that it's an in argument. Which assumes that the parameter is not muted by the operation.

If it would have been correctly specified as getTwoIntegers(out m: MyInteger, out n: MyInteger) (yes, UML syntax order is slightly different from Java), you would have understood that the values of m and n are provided for the output of the values of the operation, and not as input. And indeed, as Gardener explained, in Java you can provide a class object that can then be mutated to store the results; because class objects are passed by reference (i.e. it's the same object that is used and not a copy). This is by the way why a class MyInteger is used in this lab instead of a built-in type int.

Other unrelated UML remarks: there is no static type modifier keyword in UML. Either is it marked as {static} or is it underlined. Last but not least, there should be no multiplicity on a dashed dependency arrow. Multiplicities are for associations, i.e. structural relationships.

CodePudding user response:

Simple demo of what your implementation can accomplish.

The MyInteger objects passed as parameters (m and n) can be modified by the implementation of getTwoIntegers using the setInteger method of the class.

public class Main
{
    public static void main(String[] args) {

        MyInteger a = new MyInteger(333);
        MyInteger b = new MyInteger(444);
        System.out.println("before: "   a.getInteger()   ","   b.getInteger());
        
        getTwoIntegers(a,b);
        System.out.println("after: "   a.getInteger()   ","   b.getInteger());
        
    }
    
    public static class MyInteger {
        private int val;
        public MyInteger() { val = 0; }
        public MyInteger(int v) { val = v; }
        public void setInteger(int n) { this.val = n; }
        public int getInteger() { return val; }
    }
    
    static void getTwoIntegers(MyInteger m, MyInteger n) {
        // in your case modify implementation to produce random numbers
        m.setInteger(222);
        n.setInteger(555);
    }
}

Prints:

before: 333,444
after: 222,555
  • Related