cancel
Showing results for 
Search instead for 
Did you mean: 

Calculator example

Former Member
0 Kudos

Hi All,

i am creating Calaculator examle according to given steps,

everything was fine

When i copy javabean code from example into java editor,

it giving error means could not able to save , it giving red sign left side.

regards,

krishna

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

try this ---

package com.sap.examples.calculator.beans;

import javax.naming.InitialContext;

import com.sap.examples.calculator.Calculator;

import com.sap.examples.calculator.CalculatorHome;

import javax.rmi.PortableRemoteObject;

/**

  • @author asisodia

*

  • To change the template for this generated type comment go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

public class CalcProxy {

private Calculator calc;

public void init() throws Exception {

//Lookup the enterprise bean

try {

InitialContext ctx = new InitialContext();

Object ob = ctx.lookup("ejb/CalculatorBean");

CalculatorHome home =

(CalculatorHome) PortableRemoteObject.narrow(

ob,

CalculatorHome.class);

//Initialize the enterprise bean

calc = home.create();

} catch (Exception e) {

throw new Exception(

"Error instantiating Calculator EJB .." + e.toString());

}

}

public CalcProxy() throws Exception {

init();

}

public float getResult(

String firstNumber,

String secondNumber,

String expression)

throws Exception {

float result = 0;

try {

if (firstNumber != null && secondNumber != null) {

//Parse the input parameters

float first = Float.parseFloat(firstNumber);

float second = Float.parseFloat(secondNumber);

int expr = Integer.parseInt(expression);

//Invoke the relevant method of the enterprise bean

switch (expr) {

case 1 :

result = calc.multiply(first, second);

break;

case 2 :

result = calc.divide(first, second);

break;

case 3 :

result = calc.add(first, second);

break;

case 4 :

result = calc.subtract(first, second);

break;

}

}

} catch (Exception re) {

throw new Exception("Fill in all required fields with appropriate values!");

}

//Return the result of the calculation

return result;

}

amit

}

Former Member
0 Kudos

Hello

I am trying to do the calculator example in J2EE in SAP Netweaver Development Studio and am getting a lot of errors.

Has anyone got a solution and can they explain what is going on please to me?

I am new to SAP Netweaver Development Studio.

Thanks.

Anirban.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

What are the errors?

Regards,

RIch HEilman