Converting Binomial Tree in C++ to Java - java

I have the following code that I am trying to convert from C++ into Java. The code is supposed to generate a binomial tree that will be used calculate stock option prices.
Here is the following C++ code:
class Price {
public:
double stockPrice;
double optionPrice;
};
int numIntervals = 500;
Price ** binomialTree;
binomialTree = new Price*[numIntervals+1];
for (i = 0; i <= numIntervals; i++) {
binomialTree[i] = new Price[i + 1];
}
for (i = 0; i <= numIntervals; i++) {
for (j = 0; j <= i; j++) {
binomialTree[i][j].stockPrice = sNaught * pow(up, j) * pow(down, i-j);
}
}
I need the java code that will initialize the binomial tree so that I can iterate through it and calculate the various prices. The part that is throwing me off is the binomialTree[i] = new Price[i+1]; which occurs inside the loop, making the 2D array dynamic which you can't do in java. This is what I came up with which runs but the resulting price is incorrect with compared to the values given.
class Price {
double stockPrice = 0.0;
double optionPrice = 0.0;
Price(double sP, double oP) {
this.stockPrice = sP;
this.optionPrice = oP;
}
}
int i,j;
Price[][] binomialTree = new Price[numIntervals+1][numIntervals+2];
for (i = 0; i <= numIntervals; i++) {
for (j = 0; j <= i; j++) {
binomialTree[i][j] = new Price(option.getsNought() * Math.pow(up, j) * Math.pow(down, i-j), 0);
}
}

Two dimensional arrays whose row length is dynamic are very much possible in Java. Since I don't have your entire settings, here is a short example for you:
Price[][] binomialTree = new Price[20][];
for ( int i = 0 ; i < 20 ; i++ ) {
binomialTree[i] = new Price[i+1];
}
for ( int i = 0; i < 20; i ++ ) {
System.out.println( Arrays.toString(binomialTree[i]));
}
The output of this is (since we haven't populated the arrays):
[null]
[null, null]
[null, null, null]
[null, null, null, null]
...
It is worth noting that Java does not have "two dimensional arrays" (in the sense that all dimensions are allocated together as a consecutive chunk of memory) at all. All it has is one-dimensional arrays, whose base type can be a reference type. And the reference type can be that of an array.
A declaration such as
Price[][] arr = new Price[5][7];
is merely syntactic sugar that does the same as creating a 5-element array of references to arrays of Price, then creating 5 arrays of 7 references to Price and assigning them to each element of that first array.
For a formal discussion of this, read the Java Language Specification.

Related

References Mutability in Java summing two vectors

I am new to Java and I am following a class, however in one of the exercises I came up with a doubt when comparing my answer vs. teacher.
Say I have a class that holds as attributes the coefficients of a polynomial and I now want to add two polynomials.
In the teacher solution (method add_1) he makes a copy of the coefficients of both arrays being summed, however from what I understood so far Doubles are immutable, so I assume I donĀ“t need to use a copy, but only address directly (method add_2). I have tested an my values in the array are not mutated, however I wanted to confirm my understanding.
public class Polynomial {
// coefficient at index k belongs to term x^k
// consistency: array is always present and contains at least one number (which
// may be zero)
private double[] coefficients;
// --------------------- constructors
// ----------------------------------------------
// constructor: zero polynomial
public Polynomial() {
coefficients = new double[1];
coefficients[0] = 0;
}
public static Polynomial add_1(Polynomial f, Polynomial g) { // Option 1
double[] f_array = f.getCoefficients();//Do I need this copy?
double[] g_array = f.getCoefficients();//Do I need this copy?
int n = Math.max(f_array.length, g_array.length); // new array needs to be this long
double[] target = new double[n];
// fastest way to do it without if-statements:
for (int k = 0; k < n; k = k + 1) {
target[k] = 0;//Zero vector array
}
for (int k = 0; k < f_array.length; k = k + 1) {
target[k] = target[k] + f_array[k];
}
for (int k = 0; k < g_array.length; k = k + 1) {
target[k] = target[k] + g_array[k];
}
// Turn array into an object
Polynomial p = new Polynomial();
p.setCoefficients(target);
return p;
}
public static Polynomial add_2(Polynomial f, Polynomial g) { // Option 2
int n = Math.max(f_array.length, g_array.length); // new array needs to be this long
double[] target = new double[n];
// fastest way to do it without if-statements:
for (int k = 0; k < n; k = k + 1) {
target[k] = 0;//Zero vector array
}
for (int k = 0; k < f.coefficients.length; k = k + 1) {
target[k] = target[k] + f.coefficients[k];
}
for (int k = 0; k < g.coefficients.length; k = k + 1) {
target[k] = target[k] + g.coefficients[k];
}
// Turn array into an object
Polynomial p = new Polynomial();
p.setCoefficients(target);
return p;
}
// --------------------- setter / getter methods
// -----------------------------------
// setter for coefficients, creates a copy(!) of coefficients and stores it
public void setCoefficients(double[] coefficients) {
this.coefficients = new double[coefficients.length];
for (int k = 0; k < coefficients.length; k = k + 1) {
this.coefficients[k] = coefficients[k];
}
}
// getter for coefficients, returns a copy(!) of the polynomials coefficients
public double[] getCoefficients() {
double[] copy = new double[coefficients.length];
for (int k = 0; k < coefficients.length; k = k + 1) {
copy[k] = coefficients[k];
}
return copy;
}
}
Is my method add_2 protected against mutation or is there any reason I should use a copy?
Many thanks
double[] f_array = f.getCoefficients();//Do I need this copy?
This belies a misunderstanding of java.
f.getCoefficients() doesn't return a double array. That is impossible; java can only return primitives and references. It returns a reference to a double array.
No copy of an array is being made here. The only copies java makes implicitly are of references and primitives; any other copying is something you'd have to do explicitly using e.g. a copy() or clone() or Arrays.copyOf invocation. All that you've done here is make a copy of the reference which doesn't matter. f.getCoefficients() already returns a copy of the reference.
Let me try to explain with an example:
class Example {
double[] array = new double[10];
public static void main(String[] args) {
Example ex = new Example();
double[] arr = ex.array;
arr[0] = 1;
System.out.println(arr[0]);
System.out.println(ex.array[0]);
ex.array[0] = 2;
System.out.println(arr[0]);
System.out.println(ex.array[0]);
arr = new double[20];
arr[0] = 20;
System.out.println(arr[0]);
System.out.println(ex.array[0]);
}
}
> 1
> 1
> 2
> 2
> 20
> 2
Initially there is only one array, and both ex.array and arr are references pointing to this array. arr[0] = is dereferencing the reference and doing an operation on what you find there, thus, whilst arr and ex.array are copies, it's a copy of the same reference, and just like if I make a copy of a treasure map and hand it to you, if you then follow your map, dig down, and steal the treasure, and then later I follow my copy, I find the treasure gone - so it is here: arr[0] = 1 affects the one and only array, and thus printing ex.array[0[] also shows 1.
Later, with arr = new double[20] we first make a new object (new double[20]) and then assign a reference to this newly created object to arr, which has no effect on ex.array. Thus, now, it's like you have a treasure map to a completely different treasure vs. my ex.array treasure map. Therefore, when you follow your map, dig down, and put a 20 in the box, if I follow my map, I do not see that, hence why the last 2 lines prints 20 and 1, and not 20 and 20.

Rewriting array form Object[] to float[] in Java

I have to get an float[] array from an TockaXY[] array.
Example of TockaXY[] with 4 elements:
[60.039005,86.44917][96.53153,41.086178][19.988914,31.67395][96.84925,41.90731]
but I need an float[]. Tried:
for (int i = 0; i < objectArray.length; i++)
floatArray[i] = (Float)objectArray[i];
But I get error cannot Cast.
Any Ideas?
If i understood it right, you have an array within an array.
if you want to keep it this way than you have to create another array within an array
so;
float floatArray[][]; //declare a 2D array
floatArray = new float[4][2]; //make it's length same as yours
for (int i = 0; i < objectArray.length; i++){
for(int j =0; j<objectArray[i].length; j++){
//parse each value as float, and assign it to your new array
floatArray[i][j] = Float.parseFloat(objectArray[i][j]);
}
}
First of all your given element is not array, its array of array.
You can try this to convert Object[][] to Float[][].
Object[][] objectArray = { { 60.039005, 86.44917 }, { 96.53153, 41.086178 }, { 19.988914, 31.67 },
{ 96.84925, 41.90731 } };
Float[][] floatArray = new Float[objectArray.length][2];
for (int i = 0; i < objectArray.length; i++) {
floatArray[i][0] = ((Double) objectArray[i][0]).floatValue();
floatArray[i][1] = ((Double) objectArray[i][0]).floatValue();
}
System.out.println(floatArray);
Assuming TockaXY is something like
public sclass TockaXY {
private float x;
private float y;
//Constructors, getters, setters etc.
#Override
public String toString() {
return "["+ x + ", " + y + "]";
}
}
and you want a float[] containing the values of x and y from each element of a TockaXY[], the size of the float[] must be 2 * size of TockaXY[].
float [] floatArray = new float[objectArray.length * 2];
for (int i = 0, j=0; i < objectArray.length; i++) {
floatArray[j++] = objectArray[i].getX();
floatArray[j++] = objectArray[i].getY();
}
This: (SomeType) someExpr; is called a cast operation. Unfortunately, there are 3 completely different things that java can do, that all look exactly like this. A real guns and grandmas situation!
Casts can convert things, or can assert generics in types, or can coerce types itself. The latter two, at runtime, do nothing (maybe throw ClassCastException), it's just ways to tell the compiler you know what you are doing; to treat things as different types.
The ONLY one that converts anything is the 'type conversion' mode, and that one only kicks in if both the type in the parentheses and the expression you're applying it on are primitive (auto-unboxing may kick in, but it ends there).
Float is not primitive (float is primitive), so you're not doing a type conversion here, but your question makes it sound like you think you are.
Okay, and.. how do I fix my code?
It looks like TockaXY is a class that looks something like:
class TockaXY {
public float x, y;
}
From your question it is unclear what you want here. Do you want all 8 floats in an 8-sized float array? Do you only want the 'x' elements? Only the 'y' elements?
A TockaXY is not a float (it's a coordinate), so this is not easy, you'd have to program that. For example:
TockaXY[] in = ...;
float[] fs = new float[in * 2];
for (int i = 0; i < in.length; i++) {
fs[i * 2] = in[i].x;
fs[(i * 2) + 1] = in[i].y;
}

Adding Numbers and printing the sum vertically using arrays

I have to create a program which adds two integers and prints the sum vertically.
For example, I have.
a=323, b=322.
The output should be:
6
4
5
I've created the code for when the integers are up to two digits, but I want it to work for at least three digits.
Below is the best I could think of.
It may be completely wrong, but the only problem I'm facing is the declaration of array.
It says that the array might not be initialized.
If I set it to null then also it won't assign values to it later.
I know maybe I'm making a big mistake here, but I'll really appreciate if anyone could help me out.
Please keep in mind that I must not use any other functions for this code.
Hope I'm clear.
public class Vert
{
public static void main(String args[])
{
int n,i=0,j,a=323,b=322;
int s[];
n=a+b;
while(n>9)
{
s[i]=n%10;
i++;
s[i]=n/10;
if(s[i]>9)
{
n=s[i];
}
}
j=i;
for(j=i;j>=0;j--)
{
System.out.println(+s[j]);
}
}
}
String conversion seems like cheating, so here's a Stack.
int a = 323, b = 322;
java.util.Stack<Integer> stack = new java.util.Stack<>();
int n = a + b;
while (n > 0) {
stack.push(n % 10);
n = n / 10;
}
while (!stack.isEmpty())
System.out.println(stack.pop());
If an array is required, you need two passes over the sum
int a = 323, b = 322;
// Get the size of the array
int n = a + b;
int size = 0;
while (n > 0) {
size++;
n = n / 10;
}
// Build the output
int s[] = new int[size];
n = a + b;
for (int i = size - 1; n > 0; i--) {
s[i] = n % 10;
n = n / 10;
}
// Print
for (int x : s) {
System.out.println(x);
}
To initialize an array, you need to specify the size of your array as next:
int s[] = new int[mySize];
If you don't know the size of your array, you should consider using a List of Integer instead as next:
List<Integer> s = new ArrayList<Integer>();
Here is how it could be done:
// Convert the sum into a String
String result = String.valueOf(a + b);
for (int i=0; i <result.length();i++) {
// Print one character corresponding to a digit here per line
System.out.println(result.charAt(i));
}
I'd do it like this:
int a = 322;
int b = 322;
int sum = a + b;
String s = Integer.toString(sum);
for(int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
But your problem looks like an array is required.
The steps are same as in my solution:
Use int values
Sum the int values (operation)
Convert the int value in an array/string
Output the array/string

I have an array that is being altered even though I have not specifically changed it

I am writing an AI to play Mancala and this is my method in which the AI's calculations are done by examining the outcomes of all 6 possible moves. I use the array staticBoardState to restore boardState (which stores the information about all of the holes on the board) back to its original values after each examination of move outcomes, but staticBoardState seems to be changing in odd ways even though I believe that I do not change it. I am a beginner amateur coder, so please ask questions if my code does not make sense. This is my code:
public int getBotCalc(int boardState[]) {
int[] staticBoardState = boardState;
double[] movePoints = new double[6];
int initialScore = boardState[6];
int scorePoints;
int freeTurnPoints;
double bestMovePoints;
int bestMove;
for(int f = 0; f <= 5; f++) {
boardState = staticBoardState;
int botChoice = f;
int botHole = boardState[botChoice];
boardState[botChoice] = 0;
for(int g = 0; g < botHole; g++) {
botChoice++;
if(botChoice>12) {
botChoice = 0;
}
boardState[botChoice]++;
}
if(botChoice<=5&&boardState[botChoice]==1&&boardState[12-botChoice]>=1) {
boardState[6] += boardState[12 - botChoice] + 1;
boardState[botChoice] = 0;
boardState[12 - botChoice] = 0;
}
scorePoints = boardState[6] - initialScore;
if(botChoice==6) {
freeTurnPoints = 1;
} else {
freeTurnPoints = 0;
}
movePoints[f] = scorePoints + (1.5 * freeTurnPoints);
}
bestMovePoints = movePoints[0];
bestMove = 0;
for(int f = 1; f <= 5; f++) {
if(movePoints[f]>bestMovePoints) {
bestMovePoints = movePoints[f];
bestMove = f;
}
}
boardState = staticBoardState;
return bestMove;
}
Any help is greatly appreciated.
It looks like you're confusing value-type assignment with reference assignment. When you write
staticBoardState = boardState
what happens is that staticBoardState simply holds a reference to the array in memory that boardState is also already referring to. Not they both refer to the same array in memory, which is why staticBoardState is apparently being modified through the use of boardState. What you need to do to fix this is allocate staticBoardState as a new array and explicitly copy its contents, for example using a boardState.clone(), and perform similar copying each time you want to restore your boardState.

How to switch int sort to String sort?

I have written some code for sorting random integers that a user inputted. How would I switch this into sorting randomly inputted letters? Aka, user inputs j, s, g, w, and the programs outputs g, j, s, w?
for (int i = 0; i < random.length; i++) { //"random" is array with stored integers
// Assume first value is x
x = i;
for (int j = i + 1; j < random.length; j++) {
//find smallest value in array (random)
if (random[j] < random[x]) {
x = j;
}
}
if (x != i) {
//swap the values if not in correct order
final int temp = random[i];
random[i] = random[x];
random[x] = temp;
}
itsATextArea.append(random[i] + "\n");// Output ascending order
}
Originally I hoped (though I knew the chances of me being right were against me) that replacing all the 'int' with 'String' would work...naturally I was wrong and realized perhaps I had to list out what letter came before which by using lists such as list.add("a"); etc.
I apologize if this seems like I am asking you guys to do all the work (which I'm not) but I'm not entirely sure how to start going about this, so if anyone can give some hints or tips, that would be most appreciated!
You could use String.compareTo() to do that:
Change this:
int[] random = new int[sizeyouhad];
...
if (random[j] < random[x]) {
...
final int temp = random[i];
to:
String[] random = new String[sizeyouhad];
...
if (random[j].compareTo(random[x]) < 0) {
...
final String temp = random[i];
Trial with your code:
String[] random = new String[3];
random[0] = "b";
random[1] = "c";
random[2] = "a";
int x = 0;
//"random" is array with stored integers
for (int i = 0; i < random.length; i++) {
// Assume first value is x
x = i;
for (int j = i + 1; j < random.length; j++) {
//find smallest value in array (random)
if (random[j].compareTo(random[x]) < 0) {
x = j;
}
}
if (x != i) {
//swap the values if not in correct order
final String temp = random[i];
random[i] = random[x];
random[x] = temp;
}
System.out.println(random[i] + "\n");// Output ascending order
}
If you're just trying to sort a list of strings you should probably use the java.util.Collections.sort method rather than writing your own sorting routine.
Was random originally int[]? If you had changed this to String[], you can use String#compareTo method to discern if one string is "less than" another.
Incidentally, you can change the type of random to Comparable[] and then you can use the same algorithm to sort any object whose class implements the interface!
Try to use Collections.sort() function
List<String> l = Arrays.asList("j","s", "g","w");
Collections.sort(l);
If you consider every character to be a code point[1] and you want to sort by Unicode code point order[2], then there is really no need to change your logic. The work is converting from whatever input you are given (String, char[], etc.) into an int[] of the code points.
[1] - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#codePointAt(int)
[2] - http://en.wikipedia.org/wiki/Code_point
You can make your code work on any type of Object by using generics.
The following code is very simple and works perfectly (With this library you can solve your problem in few lines):
import static ch.lambdaj.Lambda.sort;
import static ch.lambdaj.Lambda.on;
import java.util.Arrays;
import java.util.List;
public class Test{
public static void main(String[] args) {
List<String> list = Arrays.asList("1","102","-50","54","ABS");
List<String> newList = sort(list, on(String.class));
System.out.println(newList);//[-50, 1, 102, 54, ABS]
}
}
This code uses lambda library (download here, website). Find in the website this example:
List<Person> sorted = sort(persons, on(Person.class).getAge());

Categories