#ANONIEM | dinsdag 13 mei 2014 @ 23:12 |
Goedendag,
Ik snap het niet meer zo goed. 
De opdracht luidt om aan de hand van een template, dus indeling van classes en methods, de Game of Life te simuleren mbv JAVA.
Nu heb ik in een class GameOfLife een 2-dimensionale array firstfield gemaakt die uit een bestandje de initiële generatie inleest. Maar nu luidt de opdracht dat ik in een class Field de generatie moet printen mbv deze voorgeschreven methode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void drawLine( ) { String s = "+"; for (int i=1; i<cells[0].length-1; i++) { s+="--"; } s += "+"; System.out.println(s); } public void print() { drawLine(); for (int i = 1; i < cells.length-1; i++) { System.out.print("|"); for (int j = 1; j < cells[i].length-1; j++) { System.out.print( cells[i][j].draw() ); System.out.print( " " ); // to get a more square-like output } System.out.println("|"); } drawLine(); } |
Ik snap niet zo goed hoe ik nu verder moet. Het is duidelijk dat ik in een andere klasse moet gaan rommelen met mijn eigen array, maar daar is JAVA nu juist zo op tegen toch?
Enfin, de opgave is nu:
In the class Field, add a method that calculates the next generation.
Hiervoor heb ik de beginsituatie nodig, en die staat nu opgeslagen in een andere klasse.. 
[ Bericht 5% gewijzigd door #ANONIEM op 13-05-2014 23:14:06 ] |
Hoplahopla | dinsdag 13 mei 2014 @ 23:20 |
Wat is er mis met een global er van maken? |
#ANONIEM | dinsdag 13 mei 2014 @ 23:29 |
quote: Hoe doet ik dat precies? 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import java.util.Scanner; import java.io.*;
class Field{
/* void drawLine( ) { String s = "+"; for (int i=1; i<cells[0].length-1; i++) { s+="--"; } s += "+"; System.out.println(s); } public void print() { drawLine(); for (int i = 1; i < cells.length-1; i++) { System.out.print("|"); for (int j = 1; j < cells[i].length-1; j++) { System.out.print( cells[i][j].draw() ); System.out.print( " " ); // to get a more square-like output } System.out.println("|"); } drawLine(); } int numNeighbours(){} //This method calculates the number of living neighbours of a cell with coördinates i,j */ public char[][] calcNew(){ //This method calculates the new generation } }
class Cell{ boolean alive; //This variable describes the state of a cell boolean isAlive(){ //This method returns the state of a cell }
String draw()} //This method returns the cell as a character to be printed
}
void setAlive(boolean state){} //This method changees the state of a cell }
public class GameOfLife{ //Main class File source; Scanner sc = new Scanner(System.in); char firstfield[][]; //This is going to be our array where we store the first generation int height; int width; //We obtain these values from the input file String s; //This string is used for filling the array with the first generation public char[][] readInitial(String filename){ //This method returns an array with the first generation
try { source = new File(filename); //Declares the source Scanner sf = new Scanner(source); //Initialises the scanner height = sf.nextInt(); //Reads the height and width from the input file width = sf.nextInt(); firstfield = new char[height+2][width+2]; //Creates an array with heigth + 2 rows and width+2 columns char dot = '.' ; //This is used to create the border. for(int i = 0; i < width+2; i++){ //This for-loop creates the horizontal border of the field. firstfield[0][i] = dot; firstfield[height+1][i] = dot; } for(int j = 0; j < height+2; j++){ //These for-loop creates the vertical border of the field. firstfield[j][0] = dot; firstfield[j][width+1] = dot; } for(int j = 1; j < height + 2; j++){ //These 2 loops fill the array with the first generation if(sf.hasNext()){ String s = sf.next(); for(int i = 1; i < width + 1 ; i++){ char c = s.charAt(i-1); firstfield[j][i] = c;} }
else{ } } } catch(FileNotFoundException e) { //This handles possible errors System.out.println("The file "+filename+" could not be found"); } sc.close(); return firstfield; }
void play(){
System.out.println("Give number of desired generations please"); int numberofgen = 10; //int numberofgen = sc.nextInt(); System.out.println("Give the name of the file please"); String filename = "first.txt" ; //String filename = sc.next(); readInitial(filename); //This executes the method which initialises the first generation from the input for(int i = 1; i < numberofgen+1; i++){ Field.calcNew(); //This executes the method which calculates a new generation try{Thread.sleep(2000);}catch{InterruptedException e){} //This pauses the programm for 2 seconds } } public static void main(String[] args) { new GameOfLife().play();} } |
Dit is wat ik nu heb. Ik ben een kutnoob als het om programmeren gaat, maar ik snap niet hoe ik dat verplichte nummertje met die array cells werkend moet krijgen.  |
Hoplahopla | dinsdag 13 mei 2014 @ 23:40 |
Wat ik er van kan maken is dat je een instantie van Field in the GameOfLife class moet gebruiken. Ik zie dat niet terug in je code. |
#ANONIEM | dinsdag 13 mei 2014 @ 23:44 |
quote: Op dinsdag 13 mei 2014 23:40 schreef Hoplahopla het volgende:Wat ik er van kan maken is dat je een instantie van Field in the GameOfLife class moet gebruiken. Ik zie dat niet terug in je code. Wat is een instantie? |
Hoplahopla | dinsdag 13 mei 2014 @ 23:46 |
quote: Misschien kun je beter even uitzoeken wat een object en instantie precies inhouden. |
#ANONIEM | dinsdag 13 mei 2014 @ 23:48 |
quote: Ja, ik was er al mee bezig. Ik snap de structuur van JAVA niet zo heel goed denk ik. |
#ANONIEM | dinsdag 13 mei 2014 @ 23:51 |
Ah, ik moet met een constructor die cells maken?  |
Hoplahopla | dinsdag 13 mei 2014 @ 23:52 |
Java is ontzettend logisch op zich... Maar als object georiënteerd programmeren nieuw voor je is, moet je even nadenken bij alles. |
Hoplahopla | dinsdag 13 mei 2014 @ 23:52 |
quote: Een constructor is onderdeel van de klasse, dus dan heb je hem in feite al gemaakt.
Een instantie van een cell aanmaken doe je zo:
|
Hoplahopla | dinsdag 13 mei 2014 @ 23:55 |
Weetje... Ik kan niet de opdracht voor je uitwerken. Dit is waarschijnlijk schoolwerk... Maar als je iets niet snapt wil ik heb best een beetje toelichten. |
#ANONIEM | dinsdag 13 mei 2014 @ 23:59 |
quote: Op dinsdag 13 mei 2014 23:55 schreef Hoplahopla het volgende:Weetje... Ik kan niet de opdracht voor je uitwerken. Dit is waarschijnlijk schoolwerk... Maar als je iets niet snapt wil ik heb best een beetje toelichten. Inderdaad. Ik studeer wiskunde aan de TU/e, we hebben nu een cursus programmeren in JAVA.
Even kijken… |
devzero | woensdag 14 mei 2014 @ 00:26 |
quote: Wat dacht je een simpele loop die over iedere cell gaat, de buren telt en aan de hand daarvan bepaalt of een cel tot leven komt, sterft of blijf leven? Dus iets als (sorry mijn java is erg gelimiteerd en roestig, dus niet foutvrij)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public void calcNew(){ //This method calculates the new generation Cell nieuwveld[cells.length][cells[0].length]; for (int i = 1; i < cells.length-1; i++) { for (int j = 1; j < cells[i].length-1; j++) { // numNeighbours zul je dus ook zelf even moeten schrijven int buren = numNeighbours(i,j); if(buren < 2 || buren > 3) { nieuwveld[i][j].setAlive(false); // eenzaam of overbevolking } else { if(buren==3 && !cells[i][j].isAlive()) { nieuwveld[i][j].setAlive(true); // wederopstanding } else { // niets veranderd nieuwveld[i][j].setAlive(cell[i][j].isAlive); } } } } cells = nieuwveld; // kopieer nieuwe generatie naar het veld. } |
[ Bericht 4% gewijzigd door devzero op 14-05-2014 05:29:58 (typo) ] |
#ANONIEM | woensdag 14 mei 2014 @ 07:04 |
quote: Op woensdag 14 mei 2014 00:26 schreef devzero het volgende:[..] Wat dacht je een simpele loop die over iedere cell gaat, de buren telt en aan de hand daarvan bepaalt of een cel tot leven komt, sterft of blijf leven? Dus iets als (sorry mijn java is erg gelimiteerd en roestig, dus niet foutvrij) [ code verwijderd ] Dat had ik zelf ook al in gedachten. Ik heb niet voor niets zo'n moeite gedaan om die array te maken.  |
#ANONIEM | woensdag 14 mei 2014 @ 21:04 |
quote: Op dinsdag 13 mei 2014 23:55 schreef Hoplahopla het volgende:Weetje... Ik kan niet de opdracht voor je uitwerken. Dit is waarschijnlijk schoolwerk... Maar als je iets niet snapt wil ik heb best een beetje toelichten. Ik snap nog steeds niet hoe ik nu in die class Field die array firstfield kan aanroepen. |
Hoplahopla | woensdag 14 mei 2014 @ 21:24 |
quote: Heb je specifieke restricties gekregen? |
#ANONIEM | woensdag 14 mei 2014 @ 21:30 |
quote: Dit is de opgave:
http://2wh20.jrvturnhout.nl/Downloads/Week4/Instruction7Files.zip
Het is een zipfile met de methodes en de opgavebeschrijving. |
Hoplahopla | woensdag 14 mei 2014 @ 21:32 |
quote: Als dat het is zou ik zeggen knal er een parameter tegenaan en je hebt je output. |
#ANONIEM | woensdag 14 mei 2014 @ 21:35 |
quote: Dus zoiets:
class Field{
calcNext(char firstfield[][]){
}
}
public class GameOfLife{
.......
void play{
....
Field.calcNext(){
firstfield[][] = firstfield;
}
} }
? |
Hoplahopla | woensdag 14 mei 2014 @ 21:41 |
quote: Op woensdag 14 mei 2014 21:35 schreef Amoeba het volgende:[..] Dus zoiets: class Field{ calcNext(char firstfield[][]){ } } public class GameOfLife{ ....... void play{ .... Field.calcNext(){ firstfield[][] = firstfield; } } } ? Iets in je aanroep stoppen is handig. |
#ANONIEM | woensdag 14 mei 2014 @ 21:44 |
quote: Wat vergeet ik dan?  |
Hoplahopla | woensdag 14 mei 2014 @ 21:51 |
quote: Niet om het één en of ander maar wat zegt de compiler?
Een aanroep van een methode moet altijd in de vorm naam(parameter) zijn... |
#ANONIEM | woensdag 14 mei 2014 @ 22:20 |
x
[ Bericht 99% gewijzigd door #ANONIEM op 14-05-2014 22:24:26 ] |
#ANONIEM | woensdag 14 mei 2014 @ 22:24 |
Nu
void play{
......
Field.calcNew(){ firstgen = firstfield; };
}
en
class Field{ public void calcNew(char firstgen[][]){ //This method calculates the new generation char c = firstgen[1][1]; } }
1 error and 1 warning found: ------------- *** Error *** ------------- File: O:\Bureaublad\GameOfLife\GameOfLife.java [line: 126] Error: Syntax error, insert ";" to complete Statement -------------
En die slaat op de dikgedrukte regel  |
Hoplahopla | woensdag 14 mei 2014 @ 22:56 |
Heb je firstgen wel gedefinieerd? |
#ANONIEM | woensdag 14 mei 2014 @ 22:58 |
quote:

Een maatje van me zegt dat dit ook kan door een methode in Game Of Life te maken, zoiets als
Array fieldarray(){
return firstfield;
}
en deze dan aan te roepen in de klasse Field om zodoende firstfield te bemachtigen. Kan dit? |
Hoplahopla | woensdag 14 mei 2014 @ 23:00 |
Wel als je de array wil ophalen ja. |
#ANONIEM | woensdag 14 mei 2014 @ 23:01 |
quote: Ik krijg weer 100k foutmeldingen over non static methods
Fuck dit. Ik kap ermee en ga slapen. Weer 2 uur van m'n leven verneukt.  |
#ANONIEM | woensdag 14 mei 2014 @ 23:03 |
SPOILER 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | import java.util.Scanner; import java.io.*;
class Field{
/* void drawLine( ) { String s = "+"; for (int i=1; i<cells[0].length-1; i++) { s+="--"; } s += "+"; System.out.println(s); } public void print() { drawLine(); for (int i = 1; i < cells.length-1; i++) { System.out.print("|"); for (int j = 1; j < cells[i].length-1; j++) { System.out.print( cells[i][j].draw() ); System.out.print( " " ); // to get a more square-like output } System.out.println("|"); } drawLine(); } int numNeighbours(){} //This method calculates the number of living neighbours of a cell with coördinates i,j */ public void calcNew(){ //This method calculates the new generation GameOfLife.fieldarray(); char c = firstfield[0][0]; System.out.println(c); } }
/*class Cell{ boolean alive; //This variable describes the state of a cell boolean isAlive(){ //This method returns the state of a cell }
String draw()} //This method returns the cell as a character to be printed
}
void setAlive(boolean state){} //This method changees the state of a cell }*/
public class GameOfLife{ //Main class File source; Scanner sc = new Scanner(System.in); char firstfield[][]; //This is going to be our array where we store the first generation int height; int width; //We obtain these values from the input file String s; //This string is used for filling the array with the first generation public char[][] readInitial(String filename){ //This method returns an array with the first generation
try { source = new File(filename); //Declares the source Scanner sf = new Scanner(source); //Initialises the scanner height = sf.nextInt(); //Reads the height and width from the input file width = sf.nextInt(); firstfield = new char[height+2][width+2]; //Creates an array with heigth + 2 rows and width+2 columns char dot = '.' ; //This is used to create the border. for(int i = 0; i < width+2; i++){ //This for-loop creates the horizontal border of the field. firstfield[0][i] = dot; firstfield[height+1][i] = dot; } for(int j = 0; j < height+2; j++){ //These for-loop creates the vertical border of the field. firstfield[j][0] = dot; firstfield[j][width+1] = dot; } for(int j = 1; j < height + 2; j++){ //These 2 loops fill the array with the first generation if(sf.hasNext()){ String s = sf.next(); for(int i = 1; i < width + 1 ; i++){ char c = s.charAt(i-1); firstfield[j][i] = c;} }
else{ } } } catch(FileNotFoundException e) { //This handles possible errors System.out.println("The file "+filename+" could not be found"); } sc.close(); return firstfield; } public Array fieldarray(){ return firstfield[][]; }
void play(){
System.out.println("Give number of desired generations please"); int numberofgen = 10; //int numberofgen = sc.nextInt(); System.out.println("Give the name of the file please"); String filename = "first.txt" ; //String filename = sc.next(); readInitial(filename); //This executes the method which initialises the first generation from the input Field.calcNew(); // for(int i = 1; i < numberofgen+1; i++){ //try{Thread.sleep(2000);}catch{InterruptedException e){} //This pauses the programm for 2 seconds // } public static void main(String[] args) { new GameOfLife().play();} } |
Dit is wat ik nu op ga slaan. Weinig anders dan gisteren, maar ik snap er echt de ballen van.  |
devzero | donderdag 15 mei 2014 @ 04:32 |
quote: Voor je verder gaat programmeren, probeer na te denken over de relatie tussen je classes en hoe deze elkaar manipuleren. Bijvoorbeeld, je GameOfLife class zal een Field manipuleren, maar hoe een Field de cellen aanpast is niet van belang voor de GOL class, net zoals voor je main het niet uitmaakt hoe GOL het spel implementeerd, alleen dat er een functie (ok, method) genaamd play is.
Bv, in je GOL class zul je een private variable van het type Field defineren (private Field field voor een variable genaamd field) en na inlezen van je veld initialiseer je field als "field.setInitialField(firstfield)". In je Field class heb je (bv) een private array cells type Cell en die setInitialField loop je over die char array en zet je ieder individuele cell in die array. Ipv een setInitialField kun je ook een constructor gebruiken in Field met firstfield als een parameter, maar als ik jou was zou ik daarmee wachten tot opdracht 2 
[ Bericht 0% gewijzigd door devzero op 15-05-2014 04:42:16 ] |
devzero | donderdag 15 mei 2014 @ 04:39 |
quote:
1 2 3 4 5 | class Field { public void calcNew... }
Field.calcNew(); //This executes the method which calculates a new generation |
Je roept de method calcNew aan als een "static" method (er is niet eens een instantie van Field hier) en calcNew is niet gedefineerd als static. Dat wil je ook niet, want je wil dat calcNew waardes aanpast in een instantie van Field. Dus
1 2 3 4 5 6 7 | class Field { public void calcNew... }
private Field field; ... field.calcNew(); //This executes the method which calculates a new generation |
|
devzero | donderdag 15 mei 2014 @ 04:41 |
Ik hoop overigens dat ik niet teveel C++ met java verwissel |
devzero | donderdag 15 mei 2014 @ 04:46 |
quote: Op woensdag 14 mei 2014 22:58 schreef Amoeba het volgende:[..]  Een maatje van me zegt dat dit ook kan door een methode in Game Of Life te maken, zoiets als Array fieldarray(){ return firstfield; } en deze dan aan te roepen in de klasse Field om zodoende firstfield te bemachtigen. Kan dit? Nee, dan doe je het verkeerd om. Je Field class hoeft niets te weten over de GoL class, laat de GoL class het maar doorgeven aan de Field class door een method aan te roepen in je Field class met firstfield als parameter. |