Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: board of galton,simulation.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default board of galton,simulation.

    I have a project for java,i have to simulate the board of galton(Galton Board -- from Wolfram MathWorld).

    My simulation code is ready but i'm having some final problems. I have to reset a value to zero every time the simulation runs. But i'm using the values in more than one method . So if i initialize the value in the main class is doesn't reset. But if i but it in every class it resets to zero in each method.

    So the values i want to reset are bakje1 bakje2 bake3 bakje4 bakje5 bakje6 bake7

    /**
    * Project 1
    *Dit gaat over  het bord van Galton
    *
    *@author Geldolf Robin
    *@version 22 november 2011
    *
    */
    public class Bordvangalton3 { //bakjes initialiseren
    	long bakje1 = 0 ; long bakje2 = 0 ; long bakje3 = 0 ; long bakje4 = 0 ; long bakje5 = 0 ;
    	long bakje6 = 0 ;long bakje7 = 0 ;//?????????????????????
    	String keuze;
    	String keuze1a;
    	long linksOfrechts;
    	int balletjeLinksOfRechts = 0 ;
    	int aantalSimulaties;
     
    	public void keuzesWeergeven() {
    		keuzeMogelijkheden();//hier krijg je de drie keuzes op je scherm
    		keuzes();// hier wordt de input van de gebruiker doorgeschakeld naar de juiste methode
     
     
    	}
    	public void weergavePerX() {//de layout + het berekenen waar het balletje zal vallen
    		piramidelayout();
    		algoritme();
    		tellenVanBalletjes();
    		plaatsenVanBalletjes();
    		opvangBakjesNummers();
    		}
     
     
    	public void keuzeMogelijkheden() { // weergave van keuzemogelijkheden
    		long bakje1 = 0 ; long bakje2 = 0 ; long bakje3 = 0 ; long bakje4 = 0 ; long bakje5 = 0 ;
    		long bakje6 = 0 ;long bakje7 = 0 ;
     
     
     
    		do {
    		System.out.println("   === Bord van Galton simulator ===\nWat wil je doen ?\n\t\t1)Stap voor stap het bord berekenen\n\t\t2)Automatisch het bord berekenen\n\t\t3)Het programma stoppen\nKeuze:");
    		keuze = Input.readString();
    		}
    		while ( !keuze.equals("1") && !keuze.equals("2") && !keuze.equals("3") );
     
     
    		}
    	public void keuzes() {
     
    				if (keuze.equals("1")) {
    					weergavePerX();
    					keuzes1x();
    					keuzesWeergeven();
    				}
    				else if(keuze.equals("2")) {
    				keuzes2();
    				AantalSim();
    				keuzes2a();
     
     
    				}
    				else if (keuze.equals("3")){
    				keuze3();
    				}
    		}
     
    	public void keuzes1x() {//elke keer als men op x duwt,loopt de simulatie nog 1 keer
    		long bakje1 = 0 ; long bakje2 = 0 ; long bakje3 = 0 ; long bakje4 = 0 ; long bakje5 = 0 ;
    		long bakje6 = 0 ;long bakje7 = 0 ;
     
    		do {
    		System.out.println("Druk op x om nog een stap uit te voeren,druk op iets anders om de mogelijkheden te krijgen");
    		keuze1a = Input.readString();
    		weergavePerX();
    		}
    		while(keuze1a.equals("x"));
     
     
     
    		}
     
    	public void keuzes2() {
     
     
     
     
    		do {
    		System.out.println("Hoeveel simulaties wil je uitvoeren?(minimaal 0,maximaal 1000)");
    		aantalSimulaties = Input.readInt();
    		}
    		while ( aantalSimulaties <= 0 || 1000 <= aantalSimulaties );
     
    	}
    	public void AantalSim() {
    		long y;
    		for ( y = 0 ; y < aantalSimulaties ; y ++) {
    			algoritme();
    			tellenVanBalletjes();
    			}	
    	}
     
     
    	public void keuzes2a() {//tellen van balletjes zonder ze te tekenen
     
     
     
    			piramidelayout();
    			plaatsenVanBalletjes();
    			opvangBakjesNummers();
    			keuzes2c();
     
     
    			}
     
     
     
     
    	public void keuzes2c() {
     
    		System.out.println("Druk op x om nog een stap uit te voeren,druk op iets anders om de mogelijkheden te krijgen");
    		keuze1a = Input.readString();
     
    			if (keuze1a.equals("x")) {
    			keuzes2();
    			AantalSim();
    			keuzes2a();
    			}
    			else {
    			keuzesWeergeven();
    			}
     
    		}
     
    	public void piramidelayout() {// piramide weergeven
     
    		System.out.println("                   *         ");
    		System.out.println("                *     *        ");
    		System.out.println("             *     *     *   ");
    		System.out.println("          *     *     *     *   ");
    		System.out.println("       *     *     *     *     *   ");
    		System.out.println("    *     *     *     *     *     *    ");
     
    		}
     
     
     
    	public void algoritme() {//berekenen waar de balletjes gaan neervallen(balletjeslinksofrechts)
    		balletjeLinksOfRechts = 0;
    		for( int x = 0 ; x <= 5 ; x ++ ) {
     
    			linksOfrechts = (int)(Math.random() * 2);
    			System.out.println(linksOfrechts);
    				if (linksOfrechts == 0) {
     
    				balletjeLinksOfRechts --  ;// Balletje valt naar links als 0,5 groter is dan linksofrechts
    				}
    				else if (linksOfrechts == 1) {
    				balletjeLinksOfRechts ++  ;//Balletje valt naar rechts als 0,5 kleiner is dan linksofrechts
    				}
     
     
    		}
    	}
     
     
     
    	public void tellenVanBalletjes() {//de plaats waar het balletje is gevallen optellen bij het bakje
     
    		switch( balletjeLinksOfRechts) {
     
    			case -6: //balletje is 6 keer naar links gevallen 
    				bakje1 ++ ;
    				break;
     
    			case -4: //balletje is 5keer naar links gevallen en 1 keer naar rechts
    				bakje2 ++ ;
    				break;
     
    			case -2://balletje is 4 keer naar links gevallen en 2 keer naar rechts
    				bakje3 ++ ;
    				break;
     
    			case 0://balletje is 3 keer naar links en 3 keer naar rechts gevallen
    				bakje4 ++ ;
    				break;
     
    			case 2: //balletje is 2 keer naar links en 4 keer naar rechts gevallen
    				bakje5 ++ ;
    				break;
     
    			case 4: //balletje is 1 keer naar links en 5 keer naar rechts gevallen
    				bakje6 ++ ;
    				break;
     
    			case 6: // balletje is 6 keer naar rechts gevallen
    				bakje7 ++ ;
    				break;
    			default: System.out.println(">> " + balletjeLinksOfRechts);
    			}
    			}
     
    		public void plaatsenVanBalletjes() {//het balletje bij het bakje optellen
    		System.out.println("|  " + bakje1 + "  | " + bakje2 + "   | " + bakje3 + "   | " + bakje4 + "   | "
    		+ bakje5 + "   | " + bakje6 + "   | " + bakje7 + "   | ");
     
    	}
    	public void opvangBakjesNummers() {//weergave van bakjesnummers
    		System.out.println("|  1  |  2  |  3  |  4  |  5  |  6  |  7  |");//???????????????????
     
    		}
     
     
    	public void keuze3() {
    		System.out.println("Programma sluit af");
     
    		}
     
     
     
     
    	public static void main(String [] args) {
    		Bordvangalton3 a = new Bordvangalton3();
    		a.keuzesWeergeven();
     
    		}
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: board of galton,simulation.

    Can you define the variables at the class level vs inside of the methods?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: board of galton,simulation.

    if i do that the input doesn't reset to zero if i want to run another simulation.If i run 5 simulations i will have 5 balls in the bottom,if i run a second simulation of 5 it wil add this to the first simulations. Its has to start from zero each time i run the simulation

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: board of galton,simulation.

    Is your problem because you have variables with the same name as class variables inside of methods?
    You should rename all the variables used in your methods so they have different names from the class variables.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: board of galton,simulation.

    Don't they have to be the same name, i'm adding them in 1 method,and printing them out in another method. I'm searching for a way to initialize them to zero every time they ask for a new simulation.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: board of galton,simulation.

    Don't they have to be the same name
    Make sure that you do NOT give the SAME names to local variables as class variables have. This can be a problem. Give them different names.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: board of galton,simulation.

    ok i did what you said,but i still won't work.
    I have to use the variables over 2 methods. So if i initialize them in the methods separately it doesn't work. Because it resets in each method. If i set the variable in the class it doesn't reset each time i want to call to the methods.

    	public void tellenVanBalletjes() {//de plaats waar het balletje is gevallen optellen bij het bakje
     
    		switch( balletjeLinksOfRechts) {
     
    			case -6: //balletje is 6 keer naar links gevallen 
    				bakje1 ++ ;
    				break;
     
    			case -4: //balletje is 5keer naar links gevallen en 1 keer naar rechts
    				bakje2 ++ ;
    				break;
     
    			case -2://balletje is 4 keer naar links gevallen en 2 keer naar rechts
    				bakje3 ++ ;
    				break;
     
    			case 0://balletje is 3 keer naar links en 3 keer naar rechts gevallen
    				bakje4 ++ ;
    				break;
     
    			case 2: //balletje is 2 keer naar links en 4 keer naar rechts gevallen
    				bakje5 ++ ;
    				break;
     
    			case 4: //balletje is 1 keer naar links en 5 keer naar rechts gevallen
    				bakje6 ++ ;
    				break;
     
    			case 6: // balletje is 6 keer naar rechts gevallen
    				bakje7 ++ ;
    				break;
    			default: System.out.println(">> " + balletjeLinksOfRechts);
    			}
    			}
     
    		public void plaatsenVanBalletjes() {//het balletje bij het bakje optellen
     
    		System.out.println("|  " + bakje1 + "  | " + bakje2 + "   | " + bakje3 + "   | " + bakje4 + "   | "
    		+ bakje5 + "   | " + bakje6 + "   | " + bakje7 + "   | ");
     
    	}

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: board of galton,simulation.

    If the methods need there own variables, give them their own variables but use names for the variables in the method that are different from the class variables.

Similar Threads

  1. Programming USB Relay Board
    By Infinity8 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 23rd, 2013, 03:38 AM
  2. help with board game assignment
    By pjay in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 19th, 2011, 12:58 PM
  3. Chess Board GUI full of buttons
    By falkowa42 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2011, 07:07 AM
  4. Help with score board!
    By vlan in forum Java Applets
    Replies: 0
    Last Post: June 2nd, 2010, 04:34 AM
  5. DOS board game
    By SageNTitled in forum Java Theory & Questions
    Replies: 5
    Last Post: March 4th, 2010, 03:53 PM