+ Start a Discussion
srinivas pulipatisrinivas pulipati 

Hi friends i can write addition program in apex and vf page but i can't get the output please help me

Apex:code

public class Example2{
public integer avalue{set;get;}
public integer bvalue{set;get;}
public integer result{set;get;}
public String operation{set;get;}
public pageReference subb(){
result=avalue - bvalue;
operation ='SUBTRACTION';
return null;
}
public pageReference add(){
result = avalue + bvalue;
operation ='ADDITION';
return null;
   }
}

Vf Code:


<apex:page controller="Example2">
<apex:form >
<apex:pageBlock title="caluculator">
<apex:pageBlockSection columns="1" title="simple operation" collapsible="false">
<apex:pageBlockSectionItem >
<apex:outputLabel >enter a value</apex:outputLabel>
<apex:inputText value="{!avalue}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >enter b value</apex:outputLabel>
<apex:inputText value="{!bvalue}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >you have performed {!operation} of{!avalue} and {!bvalue} and result is {!result}</apex:outputLabel> </apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
 
William TranWilliam Tran
You need something to trigger the addition, for example an "Add" button.
Cut and paste this VF page, type in value a and value b then hit "Add".

Thx
 
<apex:page controller="Example2">
<apex:form >
<apex:pageBlock title="caluculator">
<apex:pageBlockSection columns="1" title="simple operation" collapsible="false">
<apex:pageBlockSectionItem >
<apex:outputLabel >enter a value</apex:outputLabel>
<apex:inputText value="{!avalue}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >enter b value</apex:outputLabel>
<apex:inputText value="{!bvalue}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton action="{!add}" value="Add" id="theButton"/>
<apex:outputLabel >you have performed {!operation} of {!avalue} and {!bvalue} and result is {!result}</apex:outputLabel> </apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 
Ajay K DubediAjay K Dubedi
Hi,
Try this code this one help you :-
Visualforce Page:-
<apex:page controller="Test">
<apex:form >
<apex:pageBlock title="caluculator">
<apex:pageBlockSection columns="1" title="simple operation" collapsible="false">
<apex:pageBlockSectionItem >
<apex:outputLabel >enter a value</apex:outputLabel>
<apex:inputText value="{!avalue}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >enter b value</apex:outputLabel>
<apex:inputText value="{!bvalue}"/>
</apex:pageBlockSectionItem>
<apex:outputText value="{!result}"/>
<apex:commandButton value="Add" action="{!add}"/>
<apex:commandButton value="Add" action="{!subb}"/>
<apex:outputLabel >you have performed {!operation} of{!avalue} and {!bvalue} and result is {!result}</apex:outputLabel> 
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

public class Test {
public integer avalue{set;get;}
public integer bvalue{set;get;}
public integer result{set;get;}
public String operation{set;get;}

public pageReference subb(){
result=avalue - bvalue;
operation ='SUBTRACTION';
return null;
}

public pageReference add(){
result = avalue + bvalue;
operation ='ADDITION';
return null;
   }
}

 
srinivas pulipatisrinivas pulipati
Thank you 
 
Shrikant Patil 3Shrikant Patil 3
<!--Simple Apex class to perform addition, subtraction, multiplication and division based on a button you clicked.-->
<apex:page controller="Example4">
  <apex:form >
    <apex:pageBlock title="Mathematical Calculations">
       <apex:pageBlockSection columns="1">
         <apex:pageBlockSectionItem >
           <apex:outputText >Value 1 :</apex:outputText>
           <apex:inputText value="{!num1}"/>
         </apex:pageBlockSectionItem>
         <apex:pageBlockSectionItem >
           <apex:outputText >Value 2 :</apex:outputText>
           <apex:inputText value="{!num2}"/>
         </apex:pageBlockSectionItem>
         <apex:pageBlockSectionItem >
           <apex:outputText >Result:</apex:outputText>
           <apex:inputText value="{!num3}"/>
         </apex:pageBlockSectionItem>
         <apex:outputText >{!operation}  is performed with value {!num1} and {!num2}</apex:outputText>
       </apex:pageBlockSection>
       <apex:commandButton value="Addition" action="{!add}"/>
       <apex:commandButton value="Substraction" action="{!sub}"/>
       <apex:commandButton value="Multiplication" action="{!mul}"/>
       <apex:commandButton value="Division" action="{!div}"/>    
    </apex:pageBlock>
  </apex:form>
</apex:page>
Shrikant Patil 3Shrikant Patil 3
//Simple Apex class to perform addition, subtraction, multiplication and division based on a button you clicked.
public class Example4 {
  public Integer num1{set;get;}
  public Integer num2{set;get;}
  public Integer num3{set;get;}
  public String operation{set;get;}
  
  public PageReference add() {
    num3=num1+num2;
    operation='Addition';
    return null;
  }
  public PageReference sub() {
    num3=num1-num2;
    operation='Substraction';
    return null;
  }
  public PageReference mul() {
    num3=num1*num2;
    operation='Multiplication';
    return null;
  }
  public PageReference div() {
    num3 =num1/num2;
    operation='Division';
    return null;
  }
}