Rainy Hills application calculates the volume of water which remains after the rain for given surface profile represented by array of integers.
It features great user interface and can be deployed to any Java EE 7 compatible app server, although Wildfly application server is recommended.
Imagine that the array describes profile of a surface, for example.
3 2 4 1 2
Now imagine that there was a heavy rain, and all possible "holes" are filled with water. In this case, we have volume == 2 units of water.
Write an application which takes an array as an input, and calculates the volume of water which remained after the rain, in units.
To solve this problem we use single-pass algorithm with O(n) time complexity and O(1) memory. It doesn't require extra O(n) memory.
The performance is the same in best and worst case.
In addition for demo purposes algorithm provides water remained on each hill - this requires extra O(n) memory, but it can be disabled in algorithm if it's not needed.
For details see SurfaceWaterCalc.java.
App uses JSF 2.2 for model/view layer and stateless EJB 3.2 for calculation. It can be deployed to any Java EE 7 compatible container.
On UI side following additional components are used:
- PrimeFaces Google Chart component
- BootsFaces used for Bootstrap-friendly layout and components.
App consists of following components:
- Surface model - represents the surface profile.
- WaterVolumeResult model - represents result of water volume calculation for particular surface.
- JSF view for application UI which binds to surfaceController.
- SurfaceController - provides input handling, calculation request and chart model representation.
- SurfaceWaterCalc - Stateless EJB which provides calculation for the surface.
- SurfaceWaterCalcTest - tests all possible cases for the calculation algorithm.
Prerequisites:
- Java JDK 8 http://www.oracle.com/technetwork/java/javase/
- Maven 3 https://maven.apache.org
- Wildfly 12 http://wildfly.org
Installation Steps:
- Install & Run Wildfly application server.
See documentation at http://wildfly.org for more details on running Wildfly on different platforms.
cd wildfly-12.0.0.Final ./bin/standalone.sh
- Unzip rainyhills.zip archive or clone repository
or
unzip rainyhills.zip cd rainyhills
git clone https://github.com/livin/rainyhills.git
- Run test & build with maven
mvn clean install
- Deploy rainyhills to application server
or deploy
mvn package wildfly:deploy
rainyhills.war
located inrainyhills/target/
to your application server.
Open application at http://localhost:8080/rainyhills. Specify your surface profile and click "Calculate".