Esercizio rubrica


Cronologico Percorso di conversazione 
  • From: " " < >
  • To: "pierpaolo loreti" < >
  • Subject: Esercizio rubrica
  • Date: Thu, 8 Jan 2009 18:54:40 +0100
  • Disposition-notification-to: " " < >

Gentilissimo professor Loreti,
le allego una cartella contenente una prima risoluzione dell'esercizio 
7b(rubrica) da me proposta. Vorrei sapere se è corretta ed se ci sono 
eventuali migliorie da aggiungere. Grazie per la disponibilità.
                                         Mattia Boccuni

public class Dati
{
        private String nome;
        private String indirizzo;
        private String numero;

        public Dati(String n,String i,String num)
        {
                this.setNomeIndirizzoNumero(n, i, num);
        }

        public String getNumero()
        {
                return this.numero;
        }

        public String getNome()
        {
                return this.nome;
        }

        public String getIndirizzo()
        {
                return this.indirizzo;
        }

        public void setNome(String n)
        {
                this.nome=n;
        }

        public void setNumero(String num)
        {
                this.numero=num;
        }

        public void setIndirizzo(String i)
        {
                this.indirizzo=i;
        }

        public void setNomeIndirizzoNumero(String n,String i,String num)
        {
                this.setNome(n);
                this.setIndirizzo(i);
                this.setNumero(num);
        }
}


public class Rubrica
{
        private Dati rubrica[];
        private int posizione=0;

        public Rubrica()
        {
                rubrica=new Dati[4];
        }

        public void aggiungiARubrica(Dati d)
        {
                if(posizione<rubrica.length)
                {
                        rubrica[posizione]=d;
                        posizione++;
                }
                else
                {
                        System.out.println("Il contatto "+d.getNome()+" non 
puo' essere inserito per mancanza di spazio in rubrica");
                        posizione++;
                }
        }

        public void cercaNome(String n)
        {
                int pos=0;
                while(pos<rubrica.length)
                {
                        if(rubrica[pos].getNome()==n)
                        {
                                System.out.println(rubrica[pos].getNome()+" 
"+rubrica[pos].getIndirizzo()+" "+rubrica[pos].getNumero());
                                pos=0;
                                break;
                        }
                        else
                        {
                                pos++;
                                if(pos==rubrica.length)
                                {
                                pos=0;
                                System.out.println("Il contatto "+n+" non e' 
presente in rubrica");
                                break;
                                }
                        }
                }



        }
}


public class Test
{
        public static void main(String args[])
        {
                Dati d1=new Dati("Pamela","via Margutta","0476");
                Dati d2=new Dati("Angela","viale Libia","0562");
                Dati d3=new Dati("Cristiano","via Cossu","0434");
                Dati d4=new Dati("Anna","via Amaretta","0198");
                Dati d5=new Dati("Simone","via Falcetti","0872");
                Dati d6=new Dati("Giulio","via Mastropietro","0551");
                Rubrica r1=new Rubrica();
                r1.aggiungiARubrica(d1);
                r1.aggiungiARubrica(d2);
                r1.aggiungiARubrica(d3);
                r1.aggiungiARubrica(d4);
                r1.aggiungiARubrica(d5);
                r1.aggiungiARubrica(d6);
                r1.cercaNome("Anna");
                r1.cercaNome("Cristiano");
                r1.cercaNome("Francesco");
                r1.cercaNome("Marina");
                r1.cercaNome("Andrea");
                r1.cercaNome("Simone");
                r1.cercaNome("Angela");
        }
}

Attachment: Dati.class
Description: application/java

Attachment: Rubrica.class
Description: application/java

Attachment: Test.class
Description: application/java

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" 
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="output" path="bin"/>
</classpath>

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
        <name>esercizio7b</name>
        <comment></comment>
        <projects>
        </projects>
        <buildSpec>
                <buildCommand>
                        <name>org.eclipse.jdt.core.javabuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
        </buildSpec>
        <natures>
                <nature>org.eclipse.jdt.core.javanature</nature>
        </natures>
</projectDescription>



  • Esercizio rubrica,

Archivio con motore MhonArc 2.6.16.

§