S
S
striver2020-02-02 15:41:07
Java
striver, 2020-02-02 15:41:07

Working with objects. How to pass an integer to an object?

There are 3 additional class, simplified for example. In the Multiplication class - only work with application logic. In the MultiplicationResult class, the results of processing are added, Formatter - draws what you need on the screen, taking into account the processed data.
Main class - this is what should be the result.

classMain
package multiplication;

public class Main {

    public static void main(String args[]) {
        int multiplier = 10;
        int multiplicand = 2;

        Multiplication multiplication = new Multiplication();
        Formatter formatter = new Formatter();
        MultiplicationResult result = multiplication.multiply(multiplier, multiplicand);
        String formattedResult = formatter.format(result);

        System.out.println(formattedResult);
    }
}

class Formatter
package multiplication;

public class Formatter {
    
     String format(MultiplicationResult result) {
         String drawString =
                 result.getMultiplicand()
                 + " * "+
                 +result.getMultiplier()
                 +" = "
                 +result.toString();
        return drawString;
        }
    }

class Multiplication
package multiplication;

public class Multiplication {

    public int multiply(int multiplier, int multiplicand) {
        int result = multiplier * multiplicand;
        return result;
    }
}

class MultiplicationResult

package multiplication;

class MultiplicationResult {

    int multiplier;
    int multiplicand;

    MultiplicationResult() {
        this.multiplier = multiplier;
        this.multiplicand = multiplicand;
    }

    public int getMultiplier() {
        return multiplier;
    }

    public void setMultiplier(int multiplier) {
        this.multiplier = multiplier;
    }

    public int getMultiplicand() {
        return multiplicand;
    }

    public void setMultiplicand(int multiplicand) {
        this.multiplicand = multiplicand;
    }

    MultiplicationResult result(int multiplier, int multiplicand) {
        MultiplicationResult res = new MultiplicationResult();
        Multiplication multi = new Multiplication();

        if (multiplicand == 0 || multiplier == 0) {
            System.out.println("Result is 0");
        } else {
          int product=  multi.multiply(multiplier, multiplicand);
        }
        res= (int) product;
        return res;
    }
}


The output should be:
10 * 2 = 20
I have a misunderstanding on the step in the MultiplicationResult class
res= (int) product;
        return res;

There is a logic where the result is returned as a pure integer or a list of numbers. There can be many methods. But first you need to understand how to deal with one.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question