{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Docs on the Java kernel\n",
    "https://github.com/SpencerPark/IJava/blob/master/docs/kernel.md\n",
    "\n",
    "#### Docs on displaying inside the notebook\n",
    "https://github.com/SpencerPark/IJava/blob/master/docs/display.md\n",
    "\n",
    "#### Cell and notebook Magic\n",
    "https://github.com/SpencerPark/IJava/blob/master/docs/magics.md"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sample Java code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sdssd"
     ]
    }
   ],
   "source": [
    "String sample = \"sdssd\";\n",
    "System.out.print(sample);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ensure a folder (i.e. lib) containing the jars are located inside the current folder (i.e. notebooks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "List<String> jars = %jars lib/*.jar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[/home/jupyter/notebooks/./lib/jackson-annotations-2.8.4.jar, /home/jupyter/notebooks/./lib/morfologik-stemming-2.1.3.jar, /home/jupyter/notebooks/./lib/grizzly-http-server-2.3.28.jar, /home/jupyter/notebooks/./lib/jersey-common-2.25.jar, /home/jupyter/notebooks/./lib/morfologik-fsa-2.1.3.jar, /home/jupyter/notebooks/./lib/jersey-media-jaxb-2.25.jar, /home/jupyter/notebooks/./lib/jersey-guava-2.25.jar, /home/jupyter/notebooks/./lib/hk2-api-2.5.0-b30.jar, /home/jupyter/notebooks/./lib/grizzly-http-2.3.28.jar, /home/jupyter/notebooks/./lib/jackson-jaxrs-base-2.8.4.jar, /home/jupyter/notebooks/./lib/opennlp-morfologik-addon-1.9.1.jar, /home/jupyter/notebooks/./lib/validation-api-1.1.0.Final.jar, /home/jupyter/notebooks/./lib/jersey-server-2.25.jar, /home/jupyter/notebooks/./lib/grizzly-framework-2.3.28.jar, /home/jupyter/notebooks/./lib/javax.annotation-api-1.2.jar, /home/jupyter/notebooks/./lib/opennlp-brat-annotator-1.9.1.jar, /home/jupyter/notebooks/./lib/morfologik-tools-2.1.3.jar, /home/jupyter/notebooks/./lib/osgi-resource-locator-1.0.1.jar, /home/jupyter/notebooks/./lib/morfologik-fsa-builders-2.1.3.jar, /home/jupyter/notebooks/./lib/javassist-3.20.0-GA.jar, /home/jupyter/notebooks/./lib/hk2-utils-2.5.0-b30.jar, /home/jupyter/notebooks/./lib/jackson-core-2.8.4.jar, /home/jupyter/notebooks/./lib/jackson-databind-2.8.4.jar, /home/jupyter/notebooks/./lib/javax.inject-2.5.0-b30.jar, /home/jupyter/notebooks/./lib/opennlp-tools-1.9.1.jar, /home/jupyter/notebooks/./lib/hppc-0.7.1.jar, /home/jupyter/notebooks/./lib/opennlp-uima-1.9.1.jar, /home/jupyter/notebooks/./lib/hk2-locator-2.5.0-b30.jar, /home/jupyter/notebooks/./lib/javax.ws.rs-api-2.0.1.jar, /home/jupyter/notebooks/./lib/jersey-container-grizzly2-http-2.25.jar, /home/jupyter/notebooks/./lib/jersey-media-json-jackson-2.25.jar, /home/jupyter/notebooks/./lib/jcommander-1.48.jar, /home/jupyter/notebooks/./lib/aopalliance-repackaged-2.5.0-b30.jar, /home/jupyter/notebooks/./lib/jersey-entity-filtering-2.25.jar, /home/jupyter/notebooks/./lib/jersey-client-2.25.jar, /home/jupyter/notebooks/./lib/jackson-jaxrs-json-provider-2.8.4.jar, /home/jupyter/notebooks/./lib/jackson-module-jaxb-annotations-2.8.4.jar]"
     ]
    }
   ],
   "source": [
    "System.out.print(jars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load java package from a pom defintion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%loadFromPOM\n",
    "\n",
    "<dependency>\n",
    "    <groupId>org.knowm.xchart</groupId>\n",
    "    <artifactId>xchart</artifactId>\n",
    "    <version>3.5.2</version>\n",
    "</dependency>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Drawing the chart\n",
      "There seems to be an issue with trying to draw the chart\n",
      "Error: java.lang.reflect.InvocationTargetException\n"
     ]
    }
   ],
   "source": [
    "import org.knowm.xchart.*;\n",
    "\n",
    "double xData[] = new double[] {1.0, 2.0, 3.0};\n",
    "double yData[] = new double[] {3.0, 2.0, 0.0};\n",
    "\n",
    "XYChart chart = QuickChart.getChart(\"Sample chart\", \"X\", \"Y\", \"y(x)\", xData, yData);\n",
    "\n",
    "try {\n",
    "    System.out.println(\"Drawing the chart\");\n",
    "    BitmapEncoder.getBufferedImage(chart);\n",
    "} catch (Error ex) {\n",
    "    System.out.println(\"There seems to be an issue with trying to draw the chart\");\n",
    "    System.out.println(\"Error: \" + ex.getCause());\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load java package from a pom defintion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%loadFromPOM\n",
    "\n",
    "<dependency>\n",
    "    <groupId>org.apache.commons</groupId>\n",
    "    <artifactId>commons-math3</artifactId>\n",
    "    <version>3.5</version>\n",
    "</dependency>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "80.0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import org.apache.commons.math3.fraction.*;\n",
    "Fraction.FOUR_FIFTHS.percentageValue();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Checkout the use of eval()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(int)eval(\"1+1\") + 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example of ArrayList usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cannot access an element from an empty list\n"
     ]
    }
   ],
   "source": [
    "List<String> strings = new ArrayList<>();\n",
    "try {\n",
    "    strings.get(0);\n",
    "} catch (Exception ex) {\n",
    "    System.out.println(\"Cannot access an element from an empty list\");\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".java",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.5+10-jvmci-19.3-b05-LTS"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
