Esempio: Tombola!.

Slides:



Advertisements
Similar presentations
IS Programming Fundamentals 1 (Lec) Date: September 14, Array.
Advertisements

The Random Class.
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Taking Input Java Md. Eftakhairul Islam
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Java Programming: Advanced Topics 1 Collections and Utilities.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Design By Contract Using JMSAssert.
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Review Generics and the ArrayList Class
John Hurley Cal State LA
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – week 7, 2010 • Pakages • Looking back.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Programming Methodology (1). Implementing Methods main.
COSC 2006 Data Structures I Instructor: S. Xu URL:
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Remote method invocation. Introduction First introduced in JDK 1.1. Allows distributed Java programs to work with each others by behaving as if they are.
Introduction to Programming G51PRG University of Nottingham Revision 1
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Visual Classes 1 Class: Bug 5 Objects: All Bug Objects.
CS18000: Problem Solving and Object-Oriented Programming.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
CSCI 160 Midterm Review Rasanjalee DM.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Java Programming Strings Chapter 7.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Building Java Programs
Computer Programming Lab 8.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
More sophisticated behavior Using library classes to implement some more advanced functionality 5.0.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
Documentation Array and Searching. Documentation rules Easy rules: –Naming convention for variables, constants and methods Difficult rules: –Professional.
Javadoc A very short tutorial. What is it A program that automatically generates documentation of your Java classes in a standard format For each X.java.
Using Java Class Library
Lesson 6 – Libraries & APIs Libraries & APIs. Objective: We will explore how to take advantage of the huge number of pre-made classes provided with Java.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
Libraries and APIs Don’t reinvent the wheel. What’s an API? API – Application Programmers Interface –We want to use code someone else has written –API’s.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Introduction to programming in java Lecture 16 Random.
Esempio: Tombola! Parte seconda.
Java Software Structures: John Lewis & Joseph Chase
null, true, and false are also reserved.
Esempio: Tombola! Parte seconda.
Anatomy of a Java Program
Introduction to javadoc
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Presentation transcript:

Esempio: Tombola!

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.

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(); }

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;

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();

Banco 1 true 2 true 1 false 7 true 6 true 8 true 8 false 7 false 4 8 6 1 3 7 5

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;}

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);

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;

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();

Player C:\bin\JBuilderX\jdk1.4\bin\javaw tombola.Player GIOVANNI ha il numero 1 1 true 2 false GIOVANNI ha il numero 3 3 true GIOVANNI ha il numero 4 4 true GIOVANNI ha finito! Aveva la seguente cartella: 4 1 3

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();

Gioco Numero estratto :2 Pippo ha il numero 2 1 false 2 false 7 false 3 false 5 false 4 true Numero estratto :4 Pippo ha il numero 4 Pippo ha finito! Aveva la seguente cartella: 2 4 8 C:\bin\JBuilderX\jdk1.4\bin\javaw tombola.Gioco 3 true Numero estratto :3 1 true Numero estratto :1 3 false 5 true Numero estratto :5 7 true Numero estratto :7 8 true Numero estratto :8 Pippo ha il numero 8 7 false 2 true

Gioco Esercizio: Modificare il codice aggiungendo altri giocatori

Javadoc

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; } }

Output getImage public 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

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)

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]).

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

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

More info http://java.sun.com/j2se/javadoc/writingdoccomments/index.html 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

Gioco Esercizio: Aggiungere i commenti alla Tombola