S
S
striver2020-01-24 23:28:29
Java
striver, 2020-01-24 23:28:29

How to add data to an object and then use it?

The question was originally

There is a class SomeMulti. It contains both data processing and output. You need to separate the output into another class. That is, transfer the processed data, for example, to the Draw class, and then add the necessary text for output to the same console.

classMain
public class Main {

    public static void main(String[] args) {

        SomeMulti someMulti = new SomeMulti();
        Draw formatter = new Draw();
        int input = 20;
        someMulti.multiply(input);
    }
}

class SomeMulti
public class SomeMulti {

    public void multiply(int input) {
        int result = input * 1;
        System.out.println(1 + " action: " + result);
        for (int i = 1; i < 10; i++) {
            result = (result + 1) * i;
            System.out.println(i + 1 + " action: " + result);
        }
    }
}

I have edited and added below answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
striver, 2020-01-26
@striver

I messed around a little and stuffed everything into a sheet, this is what happened:

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

        SomeMulti someMulti = new SomeMulti();
        Draw formatter = new Draw();
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        ArrayList<Integer> numbers2 = new ArrayList<Integer>();
        numbers = someMulti.multiply();
        numbers2 = someMulti.multiply2();
        System.out.println(formatter.drawHz(numbers, numbers2));
    }
}

import java.util.ArrayList;

public class SomeMulti {

    public ArrayList<Integer> multiply() {
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            numbers.add((2 + (i * 2)));
        }
        return numbers;
    }

    public ArrayList<Integer> multiply2() {
        ArrayList<Integer> numbers2 = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            numbers2.add(i + 1);
        }
        return numbers2;
    }
}

import java.util.ArrayList;

public class Draw {

    public StringBuilder drawHz(ArrayList<Integer> numbers, ArrayList<Integer> numbers2) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numbers.size(); i++) {
            sb.append("nubmer " + i + " is " + numbers.get(i)).append("\n");
            sb.append("nubmer2 " + i + " is " + numbers2.get(i)).append("\n");
        }
        return sb;
    }
}

I need to transfer more values ​​and each time call the array sheet in the main - somehow not very beautiful. How can this be stuffed into an object and used?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question