Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree1Likes

Thread: Boolean help

  1. #1
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Boolean help

    Hi

    I have an arrayList and i must use a method.

    Java Code:
    /**
         * Method to add a student to the lis, but can't be added if the number (id) already exist.
         * Methos return true if the student is added or false
         * 
         * @param  aluno   student to be added
         * @return   True if added 
         */
        public boolean addAluno(Aluno aluno)
        {
            boolean estado = false;
            for(int i=0; i<alunos.size(); i++)
            {
                
                if(aluno(Aluno.getID))
                {
                    return false;
                }
           
           }
           alunos.add(aluno);
           return true;
        }
    Class Aluno

    Java Code:
    public class Aluno
    {
        private int id; // Nº do aluno (identificador único)
        private String nome; // Nome do aluno
    
        /**
         * Construtor dos objectos da classe Aluno
         */
        public Aluno(String nome, int ID)
        {
            this.nome=nome;
            this.id=ID;
        }
    
         /**
         * Definir métodos de acesso e de mutação para os campos, 
         * considerando que o nº de aluo não pode ser alterado
         * getID, getNome, setNome
         */
        
        //Dá o nome do aluno em causa
        public String getNome()
        {
            return nome;
        }
       
        //Vai dar quantos ID estão registados
        public int getID()
        {
            return id;
        }
        
        //Mudança de nome por mutação
        public void SetNome( String name)
        {
            nome = name;
        }   
    }
    The getID method is situated in another class, i have an error saying, canno't find symbol getID..

    any help please?

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Boolean help

    You have to use an instance of Aluno to get to the getID() method. Right now you're referring to the whole class, which doesn't make sense. Also, you seem to be trying to use an aluno() method, which I don't see at all.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Thanks for your answer

    Well, i'm lost

    Java Code:
    public boolean addAluno(Aluno aluno)
        {
            boolean estado = false;
            
            Aluno t = new Aluno();
            for(int i=0; i<alunos.size(); i++)
            {
    
           
           }
           alunos.add(aluno);
           return true;
        }
    Error saying

    found no arguments in line Aluno t = new Aluno();

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Boolean help

    Quote Originally Posted by alphasil View Post
    Well, i'm lost

    :
    :

    Error saying

    found no arguments in line Aluno t = new Aluno();
    The error message is telling you exactly what's wrong. The Aluno class doesn't have a constructor that takes no parameters.

    What parameters does the constructor expect?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Hi

    Thank's for your help

    Class Aluno have a construtor.

    Java Code:
    /**
         * Construtor
         */
        public Aluno(String nome, int ID)
        {
            this.nome=nome;
            this.id=ID;    
        }
    Is not correct?

    Thanks

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Boolean help

    Right. But when do you actually call that constructor?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Hi,

    Thanks again

    Quote Originally Posted by KevinWorkman View Post
    Right. But when do you actually call that constructor?
    Well, i must see who are the students in the list so i use:

    Java Code:
    public boolean addAluno(Aluno aluno)
        {
            boolean estado = false;       
            for(int i=0; i<alunos.size(); i++)
            {
                Aluno t = alunos.get(i);
                if(t.getID()==nAluno)
                {
                    System.out.println(t);
                    return false;
                }
            }
           alunos.add(aluno);
           return true;
        }
    But with this code always add a new student, even if the number are the same, wich is wrong...

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Boolean help

    At line #7; what is 'nAluno'?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Quote Originally Posted by JosAH View Post
    At line #7; what is 'nAluno'?

    kind regards,

    Jos
    Sorry, was a error but i have problems anyway, can't compile

    Java Code:
     public boolean addAluno(Aluno aluno)
        {
            boolean estado = false;       
            for(int i=0; i<alunos.size(); i++)
            {
                Aluno t = alunos.get(i);
                if(t.getID()==aluno)
                {
                    System.out.println(t);
                    return false;
                }
            }
           alunos.add(aluno);
           return true;
        }
    I understand why but don't know how to resolve,

    if(t.getID()==aluno) - Int and String, should i convert int to string to resolve?

    Thanks

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Boolean help

    Nope; 't' is an Aluno and 'aluno' is an Aluno; you want to compare their IDs, so:

    Java Code:
    if (t.getID() == aluno.getID())
       ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Quote Originally Posted by JosAH View Post
    Nope; 't' is an Aluno and 'aluno' is an Aluno; you want to compare their IDs, so:

    Java Code:
    if (t.getID() == aluno.getID())
       ...
    kind regards,

    Jos
    Thanks it works now, the only problem now if the student list, give me strange character...

    My method is this one

    Java Code:
    public void listaAlunos()
        {
            System.out.printf("\nPercorrendo o ArrayList (usando iterator)\n");
            Iterator<Aluno> iterator = alunos.iterator();
            while (iterator.hasNext()) 
            {
                System.out.println(iterator.next());
          
            }
        }
    The print gives me

    Percorrendo o ArrayList (usando iterator)
    Aluno@11a9732

    And if i go to the class Aluno and use the method getNome the return is correct...

    any help please??

    Thanks

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Boolean help

    And when are you actually calling that method?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Hi

    In the end of the class.

    Java Code:
    import java.util.Iterator;
    
    public class Turma
    {
        private ArrayList<Aluno> alunos; // Lista de alunos
        private String aluno;
        private int nAluno;
        
        public Turma()
        {
            alunos = new ArrayList<Aluno>();
        }
    
        /**
         * @param  numAluno  
         * @return            Aluno      */
        public Aluno getAluno(int numAluno)
        {
           for(int i=0; i<alunos.size(); i++)
           {
               Aluno t= alunos.get(i);
               if(t.getID() == numAluno)
               {
                   return t;
               }
            }
           return null;
        }
        
        /**
         * @param  aluno
         * @return   True
         */
     public boolean addAluno(Aluno aluno)
        {
            boolean estado = false;       
            for(int i=0; i<alunos.size(); i++)
            {
                Aluno t = alunos.get(i);
                if(t.getID()==aluno.getID())
                {
                    System.out.println(t);
                    return false;
                }
            }
           alunos.add(aluno);
           return true;
        }
        
        public void listaAlunos()
        {
            System.out.printf("\nPercorrendo o ArrayList (usando iterator)\n");
            Iterator<Aluno> iterator = alunos.iterator();
            while (iterator.hasNext()) 
            {
                System.out.println(iterator.next());
          
            }
        }
    }
    This last method is the one who gives that strange character

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Boolean help

    That's because you're simply passing an instance of Aluno into System.out.println(). What should it print out? What is the String representation of an Aluno instance?

    My question was not where are you calling System.out.println(). My question was about the other method you mentioned in your post, with the correct return value.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Hi

    It should print the number and the name of aluno(student)
    Instance Aluno represent the object Aluno with atributes (name and number)

    correct?

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Boolean help

    Quote Originally Posted by alphasil View Post
    Hi

    It should print the number and the name of aluno(student)
    Why? Where are you telling it to do that? When do you call the method that does that?

    Quote Originally Posted by alphasil View Post
    Instance Aluno represent the object Aluno with atributes (name and number)

    correct?
    Sure, but how should that be printed out? Name and then number? Number and then name? How should the number be formatted? All of that is up to you as a programmer, and Java doesn't just know how to do it automatically. You have to tell Java specifically what to print out and when to print it out. Telling it to print out an Object just calls that Object's toString() function, which prints out the stuff you're seeing now.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default o shoud

    Quote Originally Posted by KevinWorkman View Post
    Why? Where are you telling it to do that? When do you call the method that does that?
    Sure, but how should that be printed out? Name and then number? Number and then name? How should the number be formatted? All of that is up to you as a programmer, and Java doesn't just know how to do it automatically. You have to tell Java specifically what to print out and when to print it out. Telling it to print out an Object just calls that Object's toString() function, which prints out the stuff you're seeing now.
    I have changed to:

    Java Code:
     public void listaAlunos()
        {
            System.out.printf("\nPercorrendo o ArrayList (usando iterator)\n");
            for( int i = 0; i < alunos.size(); i++ )
            {
    			System.out.println(aluno);
    		}
        }
    and now my print out is this

    Percorrendo o ArrayList (usando iterator)
    null
    null
    null
    ---------------------

    So, my variable aluno is not printed...

    I'm lost

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Boolean help

    Where do you set a value to variable 'aluno'? (hint: you don't do it anywhere in the code snippet you showed us).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Boolean help

    Quote Originally Posted by JosAH View Post
    Where do you set a value to variable 'aluno'? (hint: you don't do it anywhere in the code snippet you showed us).

    kind regards,

    Jos
    Hi,

    thanks for your help and remarks

    Well, like i said, i have 2 classes, one for register student and the other to a classroom, so after insert student, i must create an object in classroom class and add the student

    Class classroom
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    public class Turma
    {
        private ArrayList<Aluno> alunos; // Lista de alunos
        private String aluno;
        private int nAluno;
        
        public Turma()
        {
            alunos = new ArrayList<Aluno>();
        }
    
       
         * @param  numAluno   
         * @return            Aluno 
         */
        public Aluno getAluno(int numAluno)
        {
           for(int i=0; i<alunos.size(); i++)
           {
               Aluno t= alunos.get(i);
               if(t.getID() == numAluno)
               {
                   return t;
               }
            }
           return null;
        }
         * @param  aluno 
         * @return   True 
         */
     public boolean addAluno(Aluno aluno)
        {
            boolean estado = false;       
            for(int i=0; i<alunos.size(); i++)
            {
                Aluno t = alunos.get(i);
                if(t.getID()==aluno.getID())
                {
                    System.out.println(t);
                    return false;
                }
            }
           alunos.add(aluno);
           return true;
        }
      
        public void listaAlunos()
        {
            System.out.printf("\nPercorrendo o ArrayList (usando iterator)\n");
            for( int i = 0; i < alunos.size(); i++ )
            {
    			System.out.println(aluno);
    		}
        }
    }
    Class Aluno
    [CODE=java]

    public class Aluno
    {
    private int id; // Nº do aluno (identificador único)
    private String nome; // Nome do aluno

    /**
    * Construtor
    */
    public Aluno(String nome, int ID)
    {
    this.nome=nome;
    this.id=ID;
    }

    public String getNome()
    {
    return nome;
    }

    public int getID()
    {
    return id;
    }

    public void SetNome( String name)
    {
    nome = name;
    }
    }
    [CODE]

    So, or i missing something...or i don't see what's wrong

    Thanks

  20. #20
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Boolean help

    You really have to think about what you're doing here. What exactly are you printing out? What exactly is aluno? Hint: it's different from Aluno.

    Break this up into smaller pieces. How do you get an instance from an array? Hint: you already did that in an earlier section of the code. How do you get a value contained by an instance? How do you print out a value?
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 1 of 2 12 LastLast

Similar Threads

  1. Boolean help, again.
    By Zigster in forum New To Java
    Replies: 3
    Last Post: 04-25-2012, 11:20 PM
  2. Boolean
    By Twixmaster123 in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 09:20 AM
  3. Boolean help
    By FadedAura in forum New To Java
    Replies: 2
    Last Post: 11-20-2011, 02:56 PM
  4. Replies: 3
    Last Post: 09-13-2011, 01:01 AM
  5. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •