Interview Questions

Wednesday, May 14, 2014

Calculator class in Ax

1-Create two Edt with real data type
     value1 & value2

2- Create Base Enum Calculator with elements
   Addition,Mul,Sub,Div.

3- Write following class
public class S_Calculator extends RunBaseBatch
{
    DialogField dFvalue1,dFvalue2,dialogType;
    DialogGroup  dlgGrp;
    DialogEnumComboBox  dialogEnumComboBox;
    S3_Calculator    calloc;
    real value1,value2,total;
    str v1,v2;
    int Valueloc1;
    //#define.FieldNoTmp(600)
}

public real add()
{
    ;
    total =this.parmValue1() + this.parmValue2();
    info(strFmt("Add = %1", total));

    return total;

}
 Object dialog()
{
   DialogRunbase   dialog = super();
    ;
    dFvalue1 = dialog.addField(extendedTypeStr(Value1), "value 1");
    dFvalue2 = dialog.addField(extendedTypeStr(Value2), "value 2");

    dialogType = dialog.addField(enumStr(Calculator), "Select the operation");

    return dialog;

}

public real divide()
{
    ;
    total =this.parmValue1() /  this.parmValue2();
    info(strFmt("Division : %1", total));
    return total;
}

public boolean getfromdialog()
{
    boolean ret;

    value1 = dFvalue1.value();
    value2 = dFvalue2.value();
    calloc = dialogType.value();
    if (dialogEnumComboBox)
    {
        Calculator = dialogEnumComboBox.selection();
    }

    return super();
}

public real multiple()
{
  ;
    total =this.parmValue1() * this.parmValue2();
    info(strFmt("Multiply : %1", total));
    return total;
}

private real parmValue1()
{
    //value1 = dFvalue1.value();
    return value1;
}

private real parmValue2(real _value2 = value2)
{
    //value2 = dFvalue2.value();
    return value2;
}

public void run()
{
    //super();
    Dialog      dialog;
    DialogField  field;
   ;

    dialog = new Dialog("Calculator");
    dialog.addText("Select your values:");
    dFvalue1 = dialog.addField(extendedTypeStr(Value1), "Value 1");
    dFvalue2 = dialog.addField(extendedTypeStr(Value2),  "Value2");
    dialogType = dialog.addField(enumStr(Calculator), "Select the operation");

    dialog.run();
    if (dialog.closedOk())
    {

        info(strFmt("value =  %1,value =  %2",dFvalue1.value(),dFvalue2.value()));
        value1 = dFvalue1.value();
        value2 = dFvalue2.value();
        calloc = dialogType.value();
        if(value1 == 0 && value2 == 0)
        {
            Error ("Please fill the values other than zero");
            break;
        }

        if(calloc == Calculator::Addition)
        {
            this.add();
        }
        if(calloc == S3_Calculator::Mul)
        {
            this.multiple();
        }
        if(calloc == Calculator::Sub)
        {
            this.subtract();
        }
        if(calloc == Calculator::Div)
        {
            if( value2 != 0)
            {
                this.divide();
            }
            else
            {
                info(strFmt("You can not divide with %1",value2));
            }
        }
    }
    else
    {
        dialog.close();
    }

}

Private real subtract()
{
     ;
    total =this.parmValue1() - this.parmValue2();
    info(strFmt("subtract = %1", total));
    return total;

}

public static void main(Args args)
 {
    S_Calculator calc   = new S_Calculator();
     ;

    calc.run();

 }

2 comments:

  1. where you make paramValue1() and paramValue2()?

    ReplyDelete
    Replies
    1. Hi,
      As I mentioned that we need to create a class S_Calculator and in same class we need to add both methods.

      Delete