Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number...

23
Esempio: Tombola!

Transcript of Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number...

Page 1: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Esempio: Tombola!

Page 2: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

2

Random numbers

package java.util;Random(long seed) Creates a new random number generator using a single

long seed

public int nextInt(int n)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive),

drawn from this random number generator's sequence.

Page 3: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Banco

 package tombola; import java.util.*;

public class Banco { Random generatore; Set numeriUsciti; final int MAXNUM = 9;

public Banco() { generatore = new

Random(System.currentTimeMillis()/101); numeriUsciti = new HashSet(); }

Page 4: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Banco

int getNextNumber() { boolean isNew = false; int num = 0; do { num = generatore.nextInt(MAXNUM) + 1; isNew = numeriUsciti.add(new Integer(num)); System.out.println(num + " " + isNew); } while (!isNew); return num; }

Page 5: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Banco

private void test() { while (numeriUsciti.size() < MAXNUM) { this.getNextNumber(); } Iterator iter=numeriUsciti.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } }

public static void main(String[] args) { Banco banco1 = new Banco(); banco1.test(); }

}

Page 6: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Banco

1 true2 true1 false7 true6 true8 true8 false7 false8 false4 true5 true3 true24861375

Page 7: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Player

package tombola;import java.util.*;

public class Player { Collection cartella; Collection cartellaOriginale; final int MAXNUM=9; final int NCELLS=3; String nome=null;

Player(String nome){this.nome=nome;}

Page 8: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Player

void creaNuovaCartella(){ Random generatore = new

Random(System.currentTimeMillis()/27); cartella=new HashSet(); for (int i=1; i<=NCELLS; i++) { boolean creatoNuovoNumero=false; do { int x=generatore.nextInt(MAXNUM)+1; creatoNuovoNumero=cartella.add(new Integer(x)); } while (!creatoNuovoNumero); } cartellaOriginale=new HashSet(); cartellaOriginale.addAll(cartella); }

Page 9: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Player

public boolean checkNumber(int x) { boolean presente=cartella.remove(new Integer(x)); if (presente) { System.out.println(nome+" ha il numero "+x); } return presente; }

public boolean isFinished() { if (cartella.isEmpty()) { System.out.println(nome+" ha finito!"); System.out.println("Aveva la seguente cartella:"); Iterator iter=cartellaOriginale.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } return true; } else return false;}

Page 10: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Player

private void test() { this.creaNuovaCartella(); // elimina i numeri for (int i=1; i<MAXNUM; i++) { boolean res=checkNumber(i); System.out.println(i+" "+res); if (isFinished()) break; } }

public static void main(String[] args) { Player player1 = new Player("GIOVANNI"); player1.test(); }

}

Page 11: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Player

C:\bin\JBuilderX\jdk1.4\bin\javaw tombola.Player GIOVANNI ha il numero 11 true2 falseGIOVANNI ha il numero 33 trueGIOVANNI ha il numero 44 trueGIOVANNI ha finito!Aveva la seguente cartella:413

Page 12: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Gioco

public class Gioco { public Gioco() { Banco b1=new Banco(); Player p=new Player("Pippo"); p.creaNuovaCartella(); while (true) { int x=b1.getNextNumber(); System.out.println("Numero estratto :"+x); p.checkNumber(x); if (p.hasFinished()) break; } } public static void main(String[] args) { Gioco gioco1 = new Gioco(); }

}

Page 13: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Gioco

C:\bin\JBuilderX\jdk1.4\bin\javaw tombola.Gioco

3 trueNumero estratto :31 trueNumero estratto :13 false5 trueNumero estratto :57 trueNumero estratto :78 trueNumero estratto :8Pippo ha il numero 87 false2 true

Numero estratto :2Pippo ha il numero 21 false2 false1 false7 false1 false3 false5 false4 trueNumero estratto :4Pippo ha il numero 4Pippo ha finito!Aveva la seguente cartella:248

Page 14: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Gioco

Esercizio:

Modificare il codice aggiungendo altri giocatori

Page 15: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Javadoc

Page 16: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Input

/** * Returns an Image object that can then be painted on the screen. * The url argument must specify an absolute {@link URL}. The name * argument is a specifier that is relative to the url argument. * <p> * This method always returns immediately, whether or not the * image exists. When this applet attempts to draw the image on * the screen, the data will be loaded. The graphics primitives * that draw the image will incrementally paint on the screen. * * @param url an absolute URL giving the base location of the image * @param name the location of the image, relative to the url argument * @return the image at the specified URL * @see Image */ public Image getImage(URL url, String name) { try { return getImage(new URL(url, name)); } catch (MalformedURLException e) { return null; } }

Page 17: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Output

getImagepublic Image getImage(URL url, String name)

Returns an Image object that can then be painted on the screen. The url argument must specify an absolute URL. The name argument is a specifier that is relative to the url argument. This method always returns immediately, whether or not the image exists. When this applet attempts to draw the image on the screen, the data will be loaded. The graphics primitives that draw the image will incrementally paint on the screen. Parameters:

url - an absolute URL giving the base location of the image name - the location of the image, relative to the url argument

Returns:the image at the specified URL

See Also:Image

Page 18: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Tags

Include tags in the following order:

@author (classes and interfaces only, required) @version (classes and interfaces only, required.) @param (methods and constructors only) @return (methods only) @exception (@throws is a synonym added in Javadoc 1.2) @see (additional references)@since (since what version/ since when is it available?)@serial (or @serialField or @serialData) @deprecated (why is deprecated, since when, what to use)

Page 19: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Documentation generation

To generate the html documentation, run javadoc followed by the list of source files, which the documentation is to be generated for, in the command prompt (i.e. javadoc [files]).

javadoc also provides additional options which can be entered as switches following the javadoc command (i.e. javadoc [options] [files]).

Page 20: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

javadoc options

Here are some basic javadoc options: -author - generated documentation will include a author section -classpath [path] - specifies path to search for referenced .class files. -classpathlist [path];[path];...;[path] - specifies a list locations (separated by ";") to search for referenced .class files. -d [path] - specifies where generated documentation will be saved. -private - generated documentation will include private fields and methods (only public and protected ones are included by default). -sourcepath [path] - specifies path to search for .java files to generate documentation form. -sourcepathlist [path];[path];...;[path] - specifies a list locations (separated by ";") to search for .java files to generate documentation form. -version - generated documentation will include a version section

Page 21: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Examples

Basic example that generates and saves documentation to the current directory (c:\MyWork) from A.java and B.java in current directory and all .java files in c:\OtherWork\.

c:\MyWork> javadoc A.java B.java c:\OtherWork\*.java 

More complex example with the generated documentation showing version information and private members from all .java files in c:\MySource\ and c:\YourSource\ which references files in c:\MyLib and saves it to c:\MyDoc. c:\> javadoc -version -private -d c:\MyDoc

-sourcepathlist c:\MySource;c:\YourSource\ -classpath c:\MyLib

Page 22: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

More info

The javadoc tool does not directly document anonymous classes -- that is, their declarations and doc comments are ignored. If you want to document an anonymous class, the proper way to do so is in a doc comment of its outer class

http://java.sun.com/j2se/javadoc/writingdoccomments/index.html

Page 23: Esempio: Tombola!. 2 Random numbers package java.util; Random(long seed) Creates a new random number generator using a single long seed public int nextInt(int.

Gioco

Esercizio:

Aggiungere i commenti alla Tombola