Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Oriented Class Communication

Tags:

java

I have a Java assessment that gets marked by a robot. Whenever I upload my assignment it shows a screen like this.

A good object-oriented design places each method into the most appropriate
class. The most appropriate class for a method should be the same class as
the data fields that that method needs to access. If you don't place a method
into the right class, then this will most likely increase the amount of
communication that is required between your classes.

The score below measures the amount of communication between your classes. A
lower score is better.

19 method invocations between classes
7 arguments passed between classes
15 results returned between classes

Amount of communication = invocations + 2*inputs + 2*outputs = 63

Now what exactly does "method invocations between classes", "arguments passed between classes" and "results returned between classes" mean?

like image 703
mglmnc Avatar asked Dec 28 '22 07:12

mglmnc


2 Answers

Method invocations between classes

As your class contains their own methods if you want to call the method from another class you have to use an instance of this class.

For example :

class A{
    public void methodA(){
    }
}
class B{
    public void methodB(){
    }
}

If I want to call methodA() from the class B I must use this:

public void methodB(){
    A a = new A();
    a.methodA(); // method invocation between classes
}

Argument passed between classes

This time methodA() will need an argument, and B as a field which could be used as argument.

class A{
    public void methodA(int argument){
    }
}
class B{
    private int fieldB = 42;
    public void methodB(){
    }
}

To call methodA() from B you will pass an argument from a class to another.

public void methodB(){
    A a= new A();
    a.methodA(fieldB); //Argument passed between classes
}

Results returned between classes

And now methodA() returns a result this is the code.

class A{
    public int methodA(){
        return 42;
    }
}
class B{
    private int fieldB;
    public void methodB(){
    }
}

To use/handle the returned value of the methodA() from the class B you'll have to do this:

public void methodB(){
    A a= new A();
    fieldB = a.methodA(); //Result returned between classes
}
like image 170
2 revs Avatar answered Jan 04 '23 23:01

2 revs


I'd have to say:

method invocations between classes
Suppose you have classes X and Y. This would be any time class X calls some method on class Y.

e.g.,

class Y
{
    public void foo() { }
}
class X
{
    public void someMethod()
    {
        Y y = new Y();
        y.foo();
    }
}

arguments passed between classes
Could possibly mean one of two things.

Either you are accessing a field of another class directly.

class Y
{
    public int number;
}
class X
{
    public void someMethod()
    {
        Y y = new Y();
        int yNum = y.number;
    }
}

Or a method was called where arguments are supplied. (most likely case)

class Y
{
    public void foo(int arg) { }
}
class X
{
    public void someMethod()
    {
        Y y = new Y();
        y.foo(56);
    }
}

results returned between classes
Received a value from a method of another class that returned a value. e.g., getters or other methods.

class Y
{
    public int foo() { return 42; }
    private int number;
    public int getNumber() { return number; }
}
class X
{
    public void someMethod()
    {
        Y y = new Y();
        int yFoo = y.foo();
        int yNumber = y.getNumber();
    }
}
like image 25
Jeff Mercado Avatar answered Jan 04 '23 23:01

Jeff Mercado