Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, 20 August 2015

European Option Pricing with Python, Java and C++

Please make sure to read the disclaimer at the bottom of the page before continuing reading.

Plain vanilla call and put european options are one of the simplest financial derivatives existing. A european call (put) option is essentially a contract which by paying a fee gives you the right to buy (sell) a stock at a predetermined price, the strike price, at a future date. This kind of contracts was originally intended as an insurance tool for companies to fix the selling price of their goods and hedge the price risk.

Options have some interesting features, for starters, if you are the buyer of an option the payoff is potentially unlimited and the loss is limited to the option price as you can see by the payoff diagram below

p

of course the reverse is true for the seller (huge downside and limited upside).

Wednesday, 18 March 2015

An implementation of the Tic Tac Toe game in Java

Lately I’ve been really busy, and I’ve not had that much time to spend on programming. I’ve had this implementation of the Tic Tac Toe game in my “to do list” for a very long time but sadly for one reason or another it just kept being delayed. In some sense it is easier to code than the text editor I made here, however it has not been so in practice. In particular, I can’t shake the feeling that the code of this project is redundant and sloppy. I’d be happy to be proved wrong but I guess there’s no point in arguing over such a small project.

This project, aside from getting more familiar with Java syntax and its libraries, helped me in becoming more fluent in building a GUI with Swing. There are some technical details which I’d like to discuss perhaps with some professional Java coder in case I get the chance.to talk to one (indeed networking is important).

The main Idea was to develop a Tic Tac Toe implementation and a battleship game as well, however, due to the lack of spare time I put aside the battleship game project and perhaps I will work on it in the future, although the basics of the underlying code are pretty similar to those used here (so similar that it might actually be a repetition).

Enough talk, let’s cut to the chase! Here are some screenshots, and below you can find the download link for the Java application. As usual, I’ve put into the .zip folder the source code so that you can check it out, work on it, whatever you like

How to play:
Basically you click on one of the three columns (if you click on any button on the selected column that’s also fine) and your mark (“x” for the player) is dropped into the lowest box available. Then the computer makes its move (randomly). Whoever reaches first 3 in a row.wins. The idea of the lowest box is due to the fact that I had another game in mind while developing this, it turned out as a “new” kind of Tic Tac Toe and I thought it was nice to play with. Of course by working on the source code I uploaded along with the runnable jar one could easily remove this and get back to the classic game.

Dropbox download link.

Immagine 001

Immagine 002

Hope this was interesting.

Monday, 9 March 2015

My first serious Java project

Finally here I am with my first serious Java project: a text editor!

Yeah I know, perhaps the word “serious” is a bit of an overkill for a simple project such as a text editor. However for a Java newbie, as I consider myself to be, this was actually a middle level project. The hard part was using Swing to implement the user interface, although I find Swing to be easier to use than Tkinter (Python) once that you’ve got the basics.

So, what’s this text editor about? Basically it is a notepad (like the one that comes natively with Windows but with less options, and of course it is written in Java. Here is a screenshot:

im1

Now, let’s talk about how to use this.

The file menu:

The file menu has 4 options: open, show current working directory, set current working directory and exit.
The open command lets you search for a .txt file to display in the user-interface.
The show and change current working directory commands are used to show and change, respectively, the directory you are working in.

You can open a file with the commands “open” and “load .txt file” or work on a new one simply by typing into the white box. When you have edited a file in the text editor, you can save it by typing a name into the bottom text field and then clicking the bottom right button. If you do not enter a name, the file will be saved with a default one. Furthermore, once you click on “save .txt data” the program asks you if you want to save the file in the current working directory. If you need to choose another location click “no” and then choose the directory to save the file in. Enter the same name of the file you are working on, to overwrite it.

The edit menu:

The edit menu basically lets you change font size (by 3 pt a click).

You can download the program (the runnable .jar file and the source code) at this address below:

https://www.dropbox.com/s/e4jhp1w9n1w6pp8/java%20text%20editor.zip?dl=0

Hope this was interesting.

Sunday, 8 March 2015

Two simple Java programs

So far I’m still a newbie Java programmer and I am mostly familiar with the basics (the syntax and basic concepts) therefore I thought I could practice my little knowledge by writing two simple programs. In particular, this task helped me to get more familiar with user input, random number generation and arrays. I took the idea from a website, however I lost the bookmark therefore I can not post it.

The first program is quite simple. It asks you to guess a number within 0 and 99. Many improvements could be done to this simple program, perhaps in the future I’ll work on it.

Here it is:

The second program is a little bit more complex, it asks for an input sentence from the user and then it outputs a sentence of the same length using random part of the entered sentence.

Hope this was interesting.

Saturday, 13 December 2014

Here again Java

Java, Java and Java again! Here is my last script in Java, which I wrote today.

The script is quite simple, it is composed of three classes and lets you handle some basic calculations with lines and parabolas on the real plane and calculate the area under the curve (definite integrals).

Optionally you can plot the data.

file

Here is the main class with a simple example:


And the integral class which handles all the background operations


Optionally, should you want to output something to a .txt file, here is the class that can help in doing that:


Bye until the next project! Occhiolino

Thursday, 11 December 2014

Java again, in English this time though! ;)

As i said earlier in my article in German, I studied Java only once in a while, since I had so many other things to do and study. Anyway here is another bit of code I’ve written in order to practice what I’ve learnt.

This script is a simple program to keep track of your inventory. Frankly speaking, an excel spreadsheet would have been faster and easier but where’s the fun in it? Occhiolino

There are three classes: the main class, the products class and the inventory class. In the products class lies all the code related to the object “product” while in the inventory class you can find all the code needed to run the inventory. Finally the main class executes the main program and (as it should be I guess).

For the sole purpose of exercising myself with Java, I tried to generate a inventory of food with three items. However you can easily add as many inventories as you like and figure out a way to speed up the process of adding items to each inventory.

Perhaps in the future I’ll add the option to print out a bar chart or something similar as soon as I have time to study Java. As for now, I find it quite interesting and demanding since it asks every time for the type of the data you are going to work with and the arrays are different from python’s lists. That can be a small issue for someone who got used to Python as myself. Furthermore Java libraries are hugely vast and that can be overwhelming at the beginning. Nonetheless I hope to get better at it soon! Sorriso

Here is the products class

public class Products 
{
private String name;
private int id;
private int quantity;
private double price;

//Constructor of the class
public Products(String name,int id, int quantity, double d)
{
this.name = name;
this.id = id;
this.quantity = quantity;
this.price = d;
}

//Quantity setter and update method
public void changeQuantity(int q,String s)
{
if(s.equals("add"))
{
this.quantity += q;
}else if(s.equals("subtract"))
{
this.quantity -= q;
}
System.out.println("Warehouse stock updated succesfully!");
}

//Price setter
public void changePrice(double p)
{
this.price = p;
System.out.println("Price changed succesfully!");
}

//Get all the info on the product
public void getInfoProduct()
{
System.out.println("As for product "+this.name+" with id: "+this.id);
System.out.println("Quantity available: "+this.quantity);
System.out.println("Price "+this.price);
System.out.println("Total value in stock: " + this.getTotalValue());
}

//This function returns the total value of the stock for the product
public double getTotalValue()
{
double totalValue = this.price*this.quantity;
return totalValue;
}

}

The main class


public class mainClass 
{

public static void main(String[] args)
{
//we create the products
Products bread = new Products("Bread",0,10,0.5);
Products ooil = new Products("Olive oil",1,20,4.00);
Products oranges = new Products("Oranges",2,5,2.50);

//we create the inventory and add products to it
Inventory inventory1 = new Inventory("Inventory 1");
inventory1.addProduct(bread);
inventory1.addProduct(ooil);
inventory1.addProduct(oranges);

//We print info on the products
bread.getInfoProduct();
ooil.getInfoProduct();
oranges.getInfoProduct();

//And the total value of the inventory
System.out.println("\nTotal value of the inventory is "+inventory1.getInventoryValue());
}
}

Finally, the inventory class


//Dynamics arrays. I found them similar to those in Python
import java.util.ArrayList;
import java.util.List;

public class Inventory
{
private String name;
//Products[] productsInStock = new Products[]{};
List<Products> productsInStock = new ArrayList<Products>();
private int totalItems = 0;

//Constructor of the class
public Inventory(String name)
{
this.name = name;
}

//Add a product object to the inventory array
public void addProduct(Products p)
{
productsInStock.add(p);
this.totalItems += 1;
System.out.println("Product added to inventory "+this.name+" succesfully!");
}

//Get the total value of the inventory
public double getInventoryValue()
{
double valueToReturn=0;
int lenAr = this.totalItems;
for(int i=0;i<lenAr;i++)
{
valueToReturn += this.productsInStock.get(i).getTotalValue();
}
return valueToReturn;
}
}

Here below is the output


Product added to inventory Inventory 1 succesfully!
Product added to inventory Inventory 1 succesfully!
Product added to inventory Inventory 1 succesfully!
As for product Bread with id: 0
Quantity available: 10
Price 0.5
Total value in stock: 5.0
As for product Olive oil with id: 1
Quantity available: 20
Price 4.0
Total value in stock: 80.0
As for product Oranges with id: 2
Quantity available: 5
Price 2.5
Total value in stock: 12.5

Total value of the inventory is 97.5

Hope this was interesting.

Es ist endlich Zeit mit Java zu spielen

Heute hatte ich Lust ein bisschen zum Thema Java zu schreiben, und der Artikel auf Deutsch zu schreiben um meine Kenntnisse der deutschen Sprache zu verbessern.

Erst muss ich mich entschuldigen, weil es im Artikel Fehler seien könnten.
Ich studiere Deutsch seit zwei Jahren als Hobby, aber ich weiss noch nicht, wenn mein Deutsch gut genug ist, um einen kleinen Artikel zu Schreiben. Auf jeden Fall, wollte ich es ausprobieren, weil man sich nur mit Praxis verbessert.

Obwohl man in einem kleinen Artikel wie meiner nicht so viele Wörter braucht, kann es schwierig sein gut Deutsch zu schreiben. Aber ich denke, dass die komplizierte Grammatik der deutsche Sprache ein sehr attraktiver Aspekt sein kann.

Ich habe genug über die Sprache geredet, jetzt reden wir über Java.

Ich bin kein Experte von Java, ich habe seit dem letzten Sommer Java studiert aber nicht jeden Tag sondern nur ab und zu, wenn Zeit da war. Jedenfalls ist hier eines der ersten Projekte. Es ist ein sehr kurzes Skript, das euch eine Grafik zeigen kann.

Um das Skript zu machen, habe ich Eclipse benutzt. Auf dieser website kann man Eclipse finden.

Hier ist das Skript


Das Skript benutzt das Class “UniformlyAcceleratedMotion”, das hier unter ist:


Hier ist die Graphik:


Script


Ich hoffe, dass dieser Artikel lustig war. Auf wiedersehen, bis zum nächsten Mal.
Vielen Dank!