This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!

Masha Stekker

Greenhorn
+ Follow
since Mar 19, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Masha Stekker

Hi

I am having problems seperating my program into classes rather than just having everything in one class.

The program I am working on reads data from file, places it into an array and then does stuff to it. At the moment I have two classes, one that takes user input, the other does everything else. I would like to be able to seperate the second class into classes that can read values into the array, check through the array and alter it, etc.

I have put a simplified version of my two classes at

http://www.metroweb.co.za/~bmstekker/test/TestMain.java
http://www.metroweb.co.za/~bmstekker/test/ReaderTest.java

If someone could suggest to me how to go about seperating these into neater
classes?

Thanks
16 years ago
OK, that makes sense - I have done that - I mean, I have said not just

One array1 = new One[10];

But created a loop with array1 = new One(); and then set each element to be a specific value.

So I assume I now have the 10 One objects, as you say.

My problem is this: I want to to change the values of the variables in these objects (if that is the correct way of putting it). It is important that it is the values of these specific objects that must change. I try to call a method of the class One that will change the values of class One's private variables.

The method call is in class Three, which is a subclass of class Two. The method that class Three calls is setAlive(); which is in method in class Two,

public void setAlive(int x)
{
array1[x].setAlive();

}

and the method that is called in this method (also called setAlive()) is a method in class One that changes the values of class One's private variables.

But I get a null point exception unless I create a new set of objects of the array in the class Two method in this way:

public void setAlive(int x)
{
array1[x] = new One();
array1[x].setAlive();

}

But this does not work. I dont get a null point exception any more, but it seems as though there is now a new array1, and changing the values of that does not affedt the objects in the original array. Can this be?

I've also tried creating a sort of "interim" array to hold the new value and then set the value of the original array elements equal to the interim like this
{
array2[x] = new One();
array2[x].setAlive();
array1[x] = array2[x];

}

but this seems to have exactly the same effect as the version above - ie it does not seem that the correct object gets changed.


Thanks for your patience
[ May 16, 2004: Message edited by: Masha Stekker ]
18 years ago
I am struggling with a problem in which I'm attempting to change the values of an array.

I'm unsure if the problem is because I'm interpreting the rules of Java incorrectly, or whether the mistake is something specific in my code somewhere. Maybe someone could read my understanding of what is supposed to happen and see if I've got it horribly wrong...

These are my assumptions:

I have three classes: in class Two, I instantiate an array of type class One. Lets say this array of objects is called array1.

Would it be correct to say that in doing so I'm instantiating a number of objects of class One?

I set the values the elements of this array using the public methods of class One, which changes the values of the private variables of class One. Lets say this array of objects is called array1.

Class Three is a subclass of class Two - it inherits class Two. In class Three, I attempt to change the values of the variables of the elements in array1 in the following way.

I use the public methods in class Two (which are inherited by class three) which in turn call the public methods of class One, which then change the values of that class's private variables.

This seems the correct way to me, but it's not working. I get a "null pointer exception" at the point when Class Two attempts to call the methods of Class One.

I hope this is not too vague for someone to get an idea of what I'm attempting: are there any obvious flaws in my assumptions?
18 years ago
Oh I THOUGHT it was something like that!
OH COOOOOOL its finally working. Thanks so much for pointing that out to me!
For any other begginers out there that might ever read this, I changed my code so that in the writeArray method, output is set to be an empty string every time the method starts. Then I changed the bit of code that puts the output in the text area from "outputArea.setText(game3.output);" to "outputArea.append(game3.output);" and moved it inside the loop that makes the method call 500 times.
It seems to work! But I must add, that I've thought that so many times now I'll have to test the thing some more before I can really relax.
Thanks also for the tips on indenting - thats been bothering me.
18 years ago
Um.
Cant spot it. Whereabouts?
ALso, if I sidestep the whole "display in a scrollable text area" then the thing seems to work (if I simply print the arrays out in MSDos window.)
Why would displaying it in the window make the loops slow down? Is it using up memory somehow the way I'm doing it?
18 years ago
Hi
I'm still stuck with my extending loops, despite a lot of help already received on this forum.
I've narrowed down the problem somwhat;
Each time the method that displays the arrays gets called it takes longer to complete. This is bad enough that the program slows down to a complete halt. And its supposed to repeat the loop 1000 times for arrays of 100x100, I'm just testing it at 100 times for arrays of 50x50.
It seems as though it needs to check through more & more information each time. But I dont understand why this might be.
I've added little print out comments at the beginning and end of the methods so that one can see how they slow down.
If I comment out the method that creates and initialises the arrays that get displayed, this makes no difference: so it cant be that I'm re-initialising the arrays and that this takes up more memory. Can it? I'm not sure.

[ April 17, 2004: Message edited by: Jim Yingst ]
18 years ago
Oh well - but thanks yet again for your help.
I have to check it again, but at one point my machine never manages to get through the entire program, I think is slows down to a complete stop, so the performance is a bit of an issue! But I'll check it again now that I've made these changes.
18 years ago
OK, Thanks! I did those things. Very illuminating.
But I still have my slowing down problem.
Have a look - maybe I misunderstood what you meant with some things. One thing I did was to move the array declaration and initialisation from the method checkNeighbour to the beginning, where the class starts. I hoped that this would help with the memory problem you mentioned, but possibly I misunderstood?
Have look if you dont mind spending more time on this one!
import javax.swing.*;
import java.awt.*;
public class GameOfLife3
{
public int array[][];
public int array3[][];
public String output;

public GameOfLife3()
{
array = new int[10][10];
array3 = new int[10][10];
output = "";
}

public static void main (String args[])
{

JTextArea outputArea = new JTextArea(20, 60);
outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
JScrollPane scroller = new JScrollPane(outputArea);
//create an instance of class GameOfLife3

GameOfLife3 game3 = new GameOfLife3();
//call method that initialises elements of 2d array
game3.initArray(game3.array);
game3.output = game3.writeArray();

//call method that checks the values of the "neighbours" of elements
//in 2d array and then checks whether they should live or die.
//call method that puts these values in a String. Loop 1000x
for(int count = 0; count <100; count++)
{
game3.checkNeighbour(game3.array);
game3.output = game3.writeArray();
}
outputArea.setText(game3.output);
//Display scrollable JTextArea containing the string "output"
JOptionPane.showMessageDialog(null, scroller,
"Game Of Life 3", JOptionPane. PLAIN_MESSAGE);
System.exit(0);
} //end main

//method initArray: initialises 2d-array
//to random int values 1 & 0
public void initArray( int array2[][])
{
for(int row = 0; row < array2.length; row ++)
{

for(int column = 0; column < array2[row].length; column++)
{
array2[row][column] = 0 + (int)(Math.random()*2);


}


}

}
//method check Neighbour does 3 things:
//1. checks values of "neighbour" elements in array
//2. creates new array according to game of life rules
//3. places values ofnew array back in first array
public void checkNeighbour(int array2[][])
{

int neighbour = 0;

for(int row = 0; row < array2.length; row ++)
{
for(int column = 0; column < array2[row].length; column++)
{

if(row ==0 && column == 0)
{
if( array2[row][column+1]==1)
neighbour++;

if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;


}
if(row == 0 && column > 0 && column < array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;

if(array2[row][column+1]==1)
neighbour++;

if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;


}

if(row == 0 && column == array2.length-1)
{

if(array2[row][column-1]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;


}
if(column == 0 && row > 0 && row < array2.length-1)
{
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;

}
if( column == 0 && row == array2.length-1)
{

if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;


}
if(column == array2.length-1 && row == array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;


}
if(row == array2.length-1 && column > 0 &&
column < array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1] ==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;


}
if(column == array2.length-1 && row >0 &&
row < array2.length -1)
{
if(array2[row+1][column]==1)
neighbour++;

if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row][column-1] ==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;


}

if(column > 0 && column < array2.length-1 &&
row > 0 && row< array2.length-1)
{
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row][column-1] ==1)
neighbour++;

}
if(array2[row][column] == 0)
{
if(neighbour == 3)
array3[row][column] = 1;
else
array3[row][column] = 0;
}
if(array2[row][column] == 1)
{

if(neighbour == 3 || neighbour == 2)
array3[row][column] = 1;
else
array3[row][column] = 0;
}

neighbour = 0;

System.out.println(column);




}//close loop column


System.out.println("row" + row);

}//close loop row




System.arraycopy(array3, 0, array2, 0, 10);
}

//method writeArray: puts elements of 2d array in string to be displayed
public String writeArray( )
{
for(int row = 0; row < array.length; row++)
{
for(int column = 0; column < array[row].length; column ++)
{

output += array[row][column];
}

output += "\n";


}

output += "\n";
for(int count = 0; count <= 100; count ++)
output+= "-";
output += "\n";

return output;
}
} // end GameOfLife1
18 years ago
Good, so far I seem to have managed to carry out your suggestions with succsess. But it seems that the big problem that slows down the loops is in the way I am displaying the information, because when I comment out the "writeArray" method the problem goes away. So I geuss I'll have to have another look at how Ive done that.
I dont really understand enough of how the mechanics of it works to understan why the way I did it makes it slow down. Any ideas?
18 years ago
Thanks! That helps a lot
18 years ago
I have a problem with a program thats supposed to create a two dimensional array, and then re-create it 1000 times based on the "neighbours" of each element of the array.
The problem is, the way I've done it, it takes longer to run through each iteration of the loop. I can tell because I've put a "System.out.prinln" command on every loop.
When the arrays are small its not too much of a problem, but when the arrays are 100x100 and get checked 1000 times the whole thing doesnt work.
I probably dont quite understand what I've done: Am I somehow re-creating or copying the array? How else can I do this?
import javax.swing.*;
import java.awt.*;
public class GameOfLife3
{
public int array[][];
public String output;
public GameOfLife3()
{
array = new int[100][100];
output = "";
}

public static void main (String args[])
{

JTextArea outputArea = new JTextArea(20, 60);
outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
JScrollPane scroller = new JScrollPane(outputArea);
//create an instance of class GameOfLife3

GameOfLife3 game3 = new GameOfLife3();
//call method that initialises elements of 2d array
game3.initArray(game3.array);
game3.output = game3.writeArray(game3.array, game3.output);
//call method that checks the values of the "neighbours" of elements
//in 2d array and then checks whether they should live or die.
//call method that puts these values in a String. Loop 1000x
for(int count = 0; count <=1000; count++)
{
game3.checkNeighbour(game3.array);
game3.output = game3.writeArray(game3.array, game3.output);
}
outputArea.setText(game3.output);
//Display scrollable JTextArea containing the string "output"
JOptionPane.showMessageDialog(null, scroller,
"Game Of Life 3", JOptionPane. PLAIN_MESSAGE);
System.exit(0);
} //end main

//method initArray: initialises 2d-array
//to random int values 1 & 0
public void initArray( int array2[][])
{
for(int row = 0; row < array2.length; row ++)
{

for(int column = 0; column < array2[row].length; column++)
{
array2[row][column] = 0 + (int)(Math.random()*2);


}


}

}
//method check Neighbour does 3 things:
//1. checks values of "neighbour" elements in array
//2. creates new array according to game of life rules
//3. places values ofnew array back in first array
public void checkNeighbour(int array2[][])
{
int array3[][] = new int[100][100];
int neighbour = 0;
for(int row = 0; row < array2.length; row ++)
{
for(int column = 0; column < array2[row].length; column++)
{

if(row ==0 && column == 0)
{
if( array2[row][column+1]==1)
neighbour++;

if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;


}
if(row == 0 && column > 0 && column < array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;

if(array2[row][column+1]==1)
neighbour++;

if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;


}

if(row == 0 && column == array2.length-1)
{

if(array2[row][column-1]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;


}
if(column == 0 && row > 0 && row < array2.length-1)
{
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;

}
if( column == 0 && row == array2.length-1)
{

if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;


}
if(column == array2.length-1 && row == array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;


}
if(row == array2.length-1 && column > 0 &&
column < array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1] ==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;


}
if(column == array2.length-1 && row >0 &&
row < array2.length -1)
{
if(array2[row+1][column]==1)
neighbour++;

if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row][column-1] ==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;


}

if(column > 0 && column < array2.length-1 &&
row > 0 && row< array2.length-1)
{
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row][column-1] ==1)
neighbour++;

}
if(array2[row][column] == 0)
{
if(neighbour == 3)
array3[row][column] = 1;
else
array3[row][column] = 0;
}
if(array2[row][column] == 1)
{

if(neighbour == 3 || neighbour == 2)
array3[row][column] = 1;
else
array3[row][column] = 0;
}

neighbour = 0;




}//close loop column


}//close loop row


for(int row = 0; row < array2.length; row ++)
{

for(int column = 0; column < array2[row].length; column ++)
{
array2[row][column] = array3[row][column];
}


}
}

//method writeArray: puts elements of 2d array in string to be displayed
public String writeArray( int array2[][], String output1)
{
for(int row = 0; row < array2.length; row ++)
{
for(int column = 0; column < array2[row].length; column ++)
{

output1 += array2[row][column];
}

output1 += "\n";


}

output1 += "\n";
for(int count = 0; count <= 100; count ++)
output1+= "-";
output1 += "\n";

return output1;
}
} // end GameOfLife1
18 years ago
I'm trying to create a single class which contains several methods. The main method calls the different methods to initialise the array etc.

Because the main method is static, I can't call other non static methods from it. The only way I've managed to get it to work is something like the example below. However, I'm sure this is all wrong and there must be a way to do it without creating the methods that get called as static. I'm geussing that I should not place the method call in the main method, but then what does go in the main method?
What exactly is " public static void main (String args[])" for, and what has to be inside that method, and what not? I also geuss I should declare the variables elsewhere. But where?
import javax.swing.*;

public class Mtry
{
public static void main (String args[])
{

int array[];
array = new int[10];
String output = "";

countArray(array);
output = writeArray(array, output);

JTextArea outputArea = new JTextArea();
outputArea.setText(output);

JOptionPane.showMessageDialog(null, outputArea, "Initializing an Array of int Values", OptionPane.INFORMATION_MESSAGE);

System.exit(0);
} // end main

static void countArray( int array2[])
{
for(int counter = 0; counter < array2.length; counter ++)
array2[counter] = counter ;


}

static String writeArray( int array2[], String output1)
{

for(int counter = 0; counter < array2.length; counter ++)
output1 += array2[counter];
return output1;
}
} // end class Mtry
18 years ago
OK Fred - I see you answered while I was still typing. I'll mull over your answer a bit...
18 years ago
OK - sorry for being unclear I'll have another go.
My problem is not because of the length -1 issue . That was a typo I am ashamed to say, and my actual code does have it limited to array. lenght -1.
Here goes my second try at explaining:
I have a 2d array.
I need to check what the values of the "neighbours" of each element in the array is:
For example, array[1][1] will have neighbours array[1][2], array[2][2], array[2][1], array[2][0], array[1][0] , array[0][0], array[0][1], array[1][2]
Which can be expressed as array[row][count] has neigbours array[row][column+1], array[row+1][column+1], array[row+1][column] etc.
Each array element has eight neighbours, if it is not on the "edge of the grid", as in, if it does not have a row or column value of 0 or a row or column value of array.length-1
Now, if a element has the same value as 3 of its neighbours a counter variable needs to be incremented.
To try to find the values of each elements neighbours, I loop through the rows and columns of the array, and compare each element to the array[row][column+1] etc as explained above to see if it has the same value as its neighbour.
But I get the out of bounds error if the element I'm checking has a row or column value of 0 or array.lenght-1 because if row is 0, then array[row-1][column] gives me a -1 value for row. And so on for column at 0 etc.
I can avoid this by if - else statements, that is, if(row == 0) then only check certain neighbours, not those that decrement row. But this gives me a very long ugly list of if-elses that confuse me and somewhere in there lurks a logical mistake.
I was wondering if there is a better way of checking for the "neighbours" of a 2d array.
18 years ago
Hi -
I've got a 2-dimensional array with 10 rows and 10 columns.
I need to be able to check what the value of each of the array element's neighbours are - that is the eight elements that are one column and or row removed - and if they are a specific value, a counter gets incremented.
Now, I can loop through the array
for(int row = 0; row < array.length; row++)
{
for( int column = 0; column < array[row].length; column++)
checking for the neighbours of each element by checking array[row-1][column-1] or array[row][column+1] or array[row+1][column+1]... etc
But of course, if the element I'm checking has a row value of 0, then I get a "array out of bounds" error for trying to chec array[row-1][column]
and so on for all the array elements that are on the "edge" of the array, so to speak.
I've tried coping with various if and else statements, as in "if row is equal to zero or column is equal to zero, check only these index values" but I end up with an immense and ugly if - else list that does not seem to work anyway.
What would be a more efficient way of doing this?
18 years ago