Answer the question
In order to leave comments, you need to log in
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.
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);
}
}
package multiplication;
public class Formatter {
String format(MultiplicationResult result) {
String drawString =
result.getMultiplicand()
+ " * "+
+result.getMultiplier()
+" = "
+result.toString();
return drawString;
}
}
package multiplication;
public class Multiplication {
public int multiply(int multiplier, int multiplicand) {
int result = multiplier * multiplicand;
return result;
}
}
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;
}
}
res= (int) product;
return res;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question