Handled two instances to another class of methods.Understanding How to Re-Receive and Display Processed Values with Return Values

Asked 1 years ago, Updated 1 years ago, 309 views

I am currently working on a Java problem, but I have not been able to resolve the error, so I am asking you a question this time.

The contents are as follows.

Create two instances of Test1 and two instances of Test2 in the Testmain main method.

Pass the two Test1 instances you created in Test2's hikaku method.

A sum value of a and b of two instances received in the hikaku method is compared, and the larger one is returned as the return value.If the sum of a and b is the same, return the first recipient.

Declare the variable test3 in the Testmain method and receive the return value of the hikaku method.
Output test3 a and b to the console respectively.

I'm taking a reasonable amount of time to think about it, but I don't know.
(Maybe I'm writing a random logic.)

Also, I am particularly worried about how to write a return when passing the return value of the method to another class.
If there are multiple returns, is it common to return them in an array?

I apologize for the inconvenience, but please let me know.

Affected Source Code

class Test 1 {

    // field
    private inta;
    private intb;

    // constructor
    Test1(inta, intb){
        This.a = a;
        This.b = b;
    }

    // method
    public int getA(){
        return this.a;
    }

    public int getB(){
        return this.b;
    }
}

class Test2 {

    // method
    public static Test 1 hikaku (Test 1a, Test 1b) {

        // instance
        intd=(a.getA()+a.getB());
        enter=(b.getB()+b.getB());

        if(d==e){
            System.out.println(d);
            return a;
        } else {
            Math.max(d,e);
            return b;
        }

    }
}

US>class TestMain{
    // field
    Object test3;

    public static void main(String[]args) {
        // TODO AUTOMATICALLY GENERATED METHOD STUB

        // Test 1 instance
        Test1 a = new Test1(5,7);
        Test1b = new Test1(5,8);

        // Invoking the Hikaku Method
        Test 1 f = Test 2.hikaku(a,b);

        // Displayed on the console
        System.out.println();

    }
}

java

2022-09-30 21:50

1 Answers

Assume that no errors have occurred and that the execution itself has been completed, but the intended value cannot be retrieved.
「 If there are compilation errors or runtime errors, please copy and add the error to the questionnaire.

1. Create two instances of Test1 and two instances of Test2 in the Testmain main method.

I have not created an instance of Test2, but the hikaku method is static, so it is working.
Make sure you meet the requirements.If it's OK as long as it works, there's no problem.

2. Pass the two Test1 instances you created in Test2's hikaku method.

It's working.

3. Compare the sum of a and b of the two instances received in the hikaku method and return the larger one as the return value.If the sum of a and b is the same, return the first recipient.

The code in the questionnaire only implements logic to determine if the sum of a and b is the same.
Let's add a conditional expression

4. Declare the variable test3 in the Testmain method and receive the return value of the hikaku method.
Output test3 a and b to the console respectively.

I don't think the test3 field of Object type is necessary, but the processing of the upper stage is generally done.
The variable f is declared in place of the variable test3 in the method.
The bottom part is a shame.
The System.out.println(); description simply prints a new line to the console.
Rewrite to System.out.println(f.getA()); to output the variable a field to the console.
Rewrite to System.out.println(a.getB(); to output the variable a field b to the console.(Variable names are confusing)
In other words, rewriting the a of the variable f to the a to the a and b of test3 to the console, respectively, is likely to be the main point of the challenge.

Also, I am particularly troubled when passing the return value of the method to another class of
I'm worried about how to write a return.
If there are multiple returns, is it common to return them in an array?

If there is more than one return, it is common to return it either in an array or in a instance field.
We expect that your concern is, "I don't know how to return both a and b variables," but we have already returned an instance of the Test1 class as a return value for the hikaku method (if implemented correctly), so we can get two variables in the ab
field.
The Test1 class field is hidden as a private variable, but public access is available to other classes using getter, such as the getA method.

The usage of Class, Instance, Variable Name, and Method may be confusing.
If you reread the text of the textbook and past exercise code with the above words in mind, it looks like the code that you can get to the correct answer in a little while.
Good luck!


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.