numbers_300x300

Write simple SOAP-based WS with RPC style bindings. Use Kadane’s algorithm as a web method.

 

 

 

 

Example Solution:

Service Endpoint Interface

package com.fuzilaga.jws.kadane;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface KadaneService
{
	@WebMethod
	public int maxSubarray(int array[]);
}

Service Implementation Bean

package com.fuzilaga.jws.kadane;

import javax.jws.WebService;

@WebService(endpointInterface = "com.fuzilaga.jws.kadane.KadaneService")
public class KadaneServiceImpl implements KadaneService
{
	// Kadane's algo.
	public int maxSubarray(int[] array)
	{
		int maxSoFar = 0;
		int maxEndingHere = 0;
		for (int i : array)
		{
			maxEndingHere = Math.max(0, maxEndingHere + i);
			maxSoFar = Math.max(maxSoFar, maxEndingHere);
		}
		return maxSoFar;
	}
}

Service Publisher

package com.fuzilaga.jws.kadane;

import javax.xml.ws.Endpoint;

public class KadaneServicePublisher
{
	public static void main(String args[])
	{
		Endpoint.publish("http://localhost:8880/kadane", new KadaneServiceImpl());
	}
}

Service Client

package com.fuzilaga.jws.kadane;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class KadaneClient
{
	public static void main(String args[]) throws Exception
	{
		URL url = new URL("http://localhost:8880/kadane");
		QName qname = new QName("http://kadane.jws.fuzilaga.com/", "KadaneServiceImplService");
		Service service = Service.create(url, qname);
		KadaneService port = service.getPort(KadaneService.class);
		int result = port.maxSubarray(new int[]{ -2, 1, -3, 4, -1, 2, 1, -5, 4 });
		// the contiguous subarray with the largest sum will be 4, -1, 2, 1, with sum 6.
		System.out.println("result = " + result);
	}
}

NOTE: Full source and instructions on how to run locally can be found here.