Home > OS >  Java setMethod dose not work. When I pass the value to the setMethod it doesn't pass it to line
Java setMethod dose not work. When I pass the value to the setMethod it doesn't pass it to line

Time:10-07

I have a Result class in which I keep all values/results. All my code:

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        Result result = new Result();
        Divider divider = new Divider();
        String[] myTestArray = new String[]{"1234", "12"};
        if (myTestArray.length != 2) {
            System.out.printf("You can not use %d arguments. "  
                    "To perform division, you need to use 2 arguments `", myTestArray.length);`
            System.exit(1);
        }
        int dividend = Integer.parseInt(myTestArray[0]);
        int divisor = Integer.parseInt(myTestArray[1]);
        divider.divide(dividend, divisor);
        Formatter formatter = new Formatter();
        formatter.format(result);
    }
}
    
public class Divider {
    Result result;

    public Divider() {
        this.result = new Result();
    }

    /**
     * divide method performs division of two numbers
     */
    public void divide(int dividend, int divisor) {
        result.setDividend(Math.abs(dividend));
        result.setDivisor(Math.abs(divisor));
        result.setQuotient(divideTwoNumbers(result.getDividend(),
                result.getDivisor()));
    }

    /**
     *
     */
    public int calculateProduct(int partialDividend) {
        int multiplicand = divideTwoNumbers(partialDividend, `result.getDivisor());`
        result.setProduct(multipleTwoNumbers(result.getDivisor(), `multiplicand));`
        result.setRemainder(partialDividend - result.getProduct());
        return result.getProduct();
    }

    /**
     * Method divideTwoNumbers is used instead of operands "/"
     */
    public int divideTwoNumbers(int dividend, int divisor) {
        int result = 0;
        for (int i = 0; dividend >= divisor; i  ) {
            dividend = dividend - divisor;
            result  ;
        }
        return result;
    }

    /**
     * Method multipleTwoNumbers is used instead of operands "*"
     */
    public int multipleTwoNumbers(int multiplicand, int multiplier) {
        int product = 0;
        for (int i = 0; i < multiplicand; i  ) {
            product = product   multiplier;
        }
        return product;
    }
}
    

I think, my problem is around here somewhere

public class Formatter {
    Result result;
    Divider divider;
    private int firstIndexPartialDividend = 0; // find the beginning of the number
    private int countSpace = 0; // space counter

    public Formatter() {
        this.result = new Result();
        this.divider = new Divider();

    }

    public void format() {
        // print the first row
        printFirstRow();
        String dividendText = Integer.toString(result.getDividend());
        for (int i = 1; i <= dividendText.length(); i  ) {
            result.setFirstPartialDividend(Integer.parseInt
                    (dividendText.substring(firstIndexPartialDividend, i)));
            // print the second row
            if (result.getFirstPartialDividend() >= result.getDivisor() &&
                    firstIndexPartialDividend == 0) {
                countSpace = dividendText.length() - i;
                printSecondRow(result.getFirstPartialDividend());
                firstIndexPartialDividend = i;
                // To align the space in the next row.
                if (Integer.toString(result.getProduct()).length()
                        > Integer.toString(result.getRemainder()).length() &&
                        result.getRemainder() > 0) {
                    countSpace = Integer.toString(result.getProduct()).length()
                            - Integer.toString(result.getRemainder()).length();
                } else {
                    countSpace = 0;
                }
            }
        }
    }

    /**
     * printFirstRow method - print the first row of an application
     */
    public void printFirstRow() {
        System.out.printf("%d|%d\n", result.getDividend(), `enter code here`result.getDivisor());
    }

    /**
     * printSecondRow method - print the second row of an application
     */
    public void printSecondRow(int firstPartialDividend) {
        divider.calculateProduct(firstPartialDividend);
        System.out.println(result.getProduct()   getSpace(countSpace)   `enter code here`"|"   (result.getQuotient()));
    }

    /**
     * getSpace method to get the number of spaces you want
     */
    public String getSpace(int count) {
        String space = "";
        for (int i = 0; i < count; i  )
            space  = " ";
        return space;
    }
}

public class Result {
    private int quotient; // keep the result of division
    private int dividend;
    private int divisor;
    private int firstPartialDividend; //keep the result of division the `enter code here`first partial dividend
    private int product;
    private int remainder;

    `enter code here`public Result() {
        this.dividend = dividend;
        this.divisor = divisor;
    }

    public int getQuotient() {
        return quotient;
    }

    public void setQuotient(int quotient) {
        this.quotient = quotient;
    }

    public int getDividend() {
        return dividend;
    }

    public void setDividend(int dividend) {
        this.dividend = dividend;
    }

    public int getDivisor() {
        return divisor;
    }

    public void setDivisor(int divisor) {
        this.divisor = divisor;
    }

    public int getFirstPartialDividend() {
        return firstPartialDividend;
    }

    public void setFirstPartialDividend(int firstPartialDividend) {
        this.firstPartialDividend = firstPartialDividend;
    }

    public int getProduct() {
        return product;
    }

    public void setProduct(int product) {
        this.product = product;
    }

    public int getRemainder() {
        return remainder;
    }

    public void setRemainder(int remainder) {
        this.remainder = remainder;
    }
}


   

values are still 0

My program should print long division result

Like this:

1234|12
12  |102
  34
  24
  10

Of course this is not the whole program, I am still working on it.

CodePudding user response:

The image shown has stopped before the assignment is complete. The parameter value is 1234, and the current value of this.dividend is zero. If you step the debugger forward, both will have the value of 1234


Your setters are working fine. You are just not calling the setters on the correct instances.

Regarding the "issue". You have a Result and Divider instance in main method that is not given to the Formatter. When you create a new Formatter() it has its own instances that are initialized with the default values of zero.

You probably should have a constructors that carry through the same instances. For example

Result result = new Result(); // this probably isn't even needed
Divider divider = new Divider(result);  // param could be removed
divider.divide(x1, x2); // This method could create and store `new Result()` value on its own
Formatter formatter = new Formatter(divider, result); // again, result not needed since it would be internal to the divider
formatter.format();

(and your formatter should probably only "format the result" as the name implies instead of also doing calculations with the Divider instance)

You can also remove new Main() since that isn't doing anything

CodePudding user response:

OneCricketeer - thanks a lot!!! I has changed constructor Divider and Formatter.

    Result result = new Result();
    Divider divider = new Divider(result);
    divider.divide(dividend, divisor);
    Formatter formatter = new Formatter(result);
    formatter.format();
   public Divider(Result result) {
    this.result = result;
}
  public Formatter(Result result) {
    this.result = result;
    this.divider = new Divider(result);

}

Now my output is

1234|12
12  |102
  •  Tags:  
  • java
  • Related