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 14 of 14

Thread: Question on Generics and collections

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    De Panne (Belgium)
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Lightbulb Question on Generics and collections

    Hello everyone!
    I'm new to java and I got a test today and kind of failed to make it, I was hoping that someone could explain it to me because I still don't get how to use Generic collections....
    this is what I got and we had to make the proper classes to get this output:
    Room 11 has H. DS as it's teacher
    Felix De Vliegher follows basis
    Koen Vanhoutte follows java
    Serge Vereecke follows java
    Freddy Himpe follows .net

    Room 11 has H. DS as it's teacher
    There are 4 cursisten
    Felix De Vliegher follows C++
    Serge Vereecke follows java
    Freddy Himpe follows .net
    Alexandra Blondeel follows java


    public class Main {
        public static void main(String[] args) {
            Traject traject1 = new Traject  ("C++");
            Traject traject2 = new Traject  ("java");
            Traject traject3 = new Traject  (".net");
            Traject traject4 = new Traject  ("basis");
            Cursist cursist1 = new Cursist  ("Felix", "De Vliegher", traject4);
            Cursist cursist2 = new Cursist  ("Koen", "Vanhoutte", traject2);
            Cursist cursist3 = new Cursist  ("Serge", "Vereecke", traject2);
            Cursist cursist4 = new Cursist  ("Freddy", "Himpe", traject3);
            Instructeur instructeur = new Instructeur("H.","DS");
            Room room = new Room(11,instructeur);
            room.cursistAdd(cursist1);
            room.cursistAdd(cursist2);
            room.cursistAdd(cursist3);
            room.cursistAdd(cursist4);
            System.out.println(lokaal);
            cursist1.setTraject(traject1);
            Cursist cursist5 = new Cursist("Alexandra","Blondeel", traject2);
            lokaal.cursistToevoegen(cursist5);
            lokaal.cursistVerwijderen(cursist2);
            System.out.println(lokaal);        
        }
    }


    what I tried to make is this:
    make 4 new classes(Traject, Room, Instructeur, cursist)
    I have no idea how to fill these classes.... My problem is the fact that I don't know the syntax and don't know how to use generics if someone could explain this to me it would be great!!!
    Thank you


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Question on Generics and collections

    If you don't know even syntax, you shouldn't bother yourself with generics. Go here and read from the very beginning.

    ---UPD---
    in fact, I can see no generics in the code.

  3. The Following User Says Thank You to angstrem For This Useful Post:

    profit7 (May 28th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Location
    De Panne (Belgium)
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Generics and collections

    by not knowing the syntax i meant that it's not clear to, not that i don't know how it works... the reason why there is no generics in the codes is because this is our test we have to USE generics to get the out shown above...

  5. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Question on Generics and collections

    So you don't have to use your Traject, Cursist and Instructor classes? Then what's the task, precisely?

  6. The Following User Says Thank You to angstrem For This Useful Post:

    profit7 (May 28th, 2013)

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Location
    De Panne (Belgium)
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Generics and collections

    to get this output from the code:

    Room 11 has H. DS as it's teacher
    Felix De Vliegher follows basis
    Koen Vanhoutte follows java
    Serge Vereecke follows java
    Freddy Himpe follows .net

    Room 11 has H. DS as it's teacher
    There are 4 cursisten
    Felix De Vliegher follows C++
    Serge Vereecke follows java
    Freddy Himpe follows .net
    Alexandra Blondeel follows java

  8. #6
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Question on Generics and collections

    System.out.println("Room 11 has H. DS as it's teacher ");
    System.out.println(" Felix De Vliegher follows basis");
    //And so on
    This will do exactly what you want.

  9. The Following User Says Thank You to angstrem For This Useful Post:

    profit7 (May 28th, 2013)

  10. #7
    Junior Member
    Join Date
    Mar 2013
    Location
    De Panne (Belgium)
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Generics and collections

    this is not the point, we HAVE to create classes and use collections, generics and so on to make the code work.
    using println() is not correct... it's a program we have to complete and the answers should come on automatically and not with println()....

  11. #8
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Question on Generics and collections

    Then describe please, what the program precisely must do under the hood. Because your current formulation of problem is similar to describing a TV like a "moving picture": under that definition, even a window can be a TV.

  12. The Following User Says Thank You to angstrem For This Useful Post:

    profit7 (May 28th, 2013)

  13. #9
    Junior Member
    Join Date
    Mar 2013
    Location
    De Panne (Belgium)
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Generics and collections

    thank you for your time! but i'm afraid that this is all what i have:
    we got the code you can see above and we were asked to complete the program using, generics, inheritance, collections... to get the output i showed you. This is the final test about our course "Java Fundamentals" so using just println() won't do me any good :-)

  14. #10
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Question on Generics and collections

    Then all I can recommend is to use a collection like List to store all your output elements and then output them via iterating over the collection. Collections use generics.

  15. The Following User Says Thank You to angstrem For This Useful Post:

    profit7 (May 28th, 2013)

  16. #11
    Junior Member
    Join Date
    Mar 2013
    Location
    De Panne (Belgium)
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Generics and collections

    This is exactly what my problem is, I wrote the program without generics and it works fine, I can't use the generics! this what I have for Traject, but in my Main Class it still shows an error...

    import java.util.*;
     
    public class Traject {
        private List<String> traject = new ArrayList<String>();
     
        public void addVak(String vak){
            traject.add(vak);
        }
     
        public List<String> getTraject() {
            return traject;
        }
     
        public void setTraject(List<String> traject) {
            this.traject = traject;
        }
     
        }

  17. #12
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Question on Generics and collections

    In your main method that you've submitted earlier, you construct Traject classes with constructors, that accept some arguments.

  18. The Following User Says Thank You to angstrem For This Useful Post:

    profit7 (May 29th, 2013)

  19. #13
    Junior Member
    Join Date
    Mar 2013
    Location
    De Panne (Belgium)
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Question on Generics and collections

    This is what I have so far:
    public class Traject {
        public Traject(String traject) {
            setTraject(traject);        
        }
     
        private String traject;
     
        public String getTraject() {
            return traject;
        }
     
        public void setTraject(String traject) {
            this.traject = traject;
        }    
    }

    public class Instructeur {
        public Instructeur(String naam, String familieNaam) {
            setNaam(naam);
            setFamilieNaam(familieNaam);
        }
     
        private String naam;
        private String familieNaam;
     
        public String getNaam(){
            return naam;
        }
     
        public String getFamilieNaam(){
            return familieNaam;
        }
     
        private void setFamilieNaam(String familieNaam){
            this.familieNaam = familieNaam;
        }
     
        private void setNaam(String naam) {
            this.naam = naam;
        }    
    }

    public class Cursist  {
        public Cursist(String naam, String familieNaam) {
            setNaam(naam);
            setFamilieNaam(familieNaam);
        }
        public Cursist(String naam, String familieNaam, Traject traject){}
     
        private String naam;
        private String familieNaam;
     
        public String getNaam() {
            return naam;
        }
     
        private void setNaam(String naam) {
            this.naam = naam;
        }
     
        public String getFamilieNaam() {
            return familieNaam;
        }
     
        private void setFamilieNaam(String familieNaam) {
            this.familieNaam = familieNaam;
        }
     
        public void setTraject(Traject traject) {
     
        }


    --- Update ---

    What I'm having problems with is the fact this class


    public class Lokaal extends Main{
        public Lokaal(int nr, Cursist cursist, Traject traject){
            setNr(nr);       
        }
     
        public Lokaal(int nr, Instructeur instructeur){}
     
        private int nr;
        LinkedList<Cursist> lokaal = new LinkedList<Cursist>(); 
     
        public int getNr() {
            return nr;
        }
     
        public void setNr(int nr) {
            this.nr = nr;
        }
     
        public void toevoegen(Cursist cursist){
     
        }
         public void remove(Cursist cursist){
     
        }
    }

    isn't good...

  20. #14
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Question on Generics and collections

    Why methods tooevoegen() and remove() are not implemented? Their implementation must be one trivial line of code, if you look at the List API.
    Also, it would be nice to override toString method in your classes, so that you'll be able to convert each object to a desired String.
    Also, I suggest you to use
    List<Cursist> lokaal = new LinkedList<Cursist>();
    instead of
    LinkedList<Cursist> lokaal = new LinkedList<Cursist>();
    Because if you decide to change implementation of locaal to, say, ArrayList, you'll have to do much more work with your current implementation.

Similar Threads

  1. collections
    By anis.laghaei in forum Collections and Generics
    Replies: 3
    Last Post: January 15th, 2013, 09:29 AM
  2. Comparing Generics; Quick Question
    By clydefrog in forum Collections and Generics
    Replies: 6
    Last Post: February 22nd, 2012, 09:48 PM
  3. collections
    By koushik in forum Collections and Generics
    Replies: 2
    Last Post: January 14th, 2012, 12:06 PM
  4. Question(s) on Generics/Templates
    By Kumarrrr in forum Java Theory & Questions
    Replies: 4
    Last Post: January 7th, 2012, 10:44 AM
  5. Generics collections and reflection
    By juza in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2010, 03:14 AM