Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ArrayList - Contact Information/Address Book

 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Really poor and confusing choice of names...


Ooof. Not half.

@Derek: We (or my) assumption was that you were implementing java.util.List, which contains VERY similar methods, and that class's add() method is:
public boolean add(E element)

As has been said before, List is a really poor choice of name to use for an interface, unless your prof's main intention was to show you why you shouldn't.

It's also possible (if s/he is a descendant of Machiavelli) that they did it precisely to get the wrong response from a forum like ours,

Winston
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Derek Nickerson wrote:

That is a point of Object Oriented Programming. You represent all things as objects.



Ok, but isn't it better (more organized) to have all the information for a contact within a single class with parameters or to use setters/getters -- as opposed to having an object for each type of information?
How should you determine whether to create a class or to do what I've done in this code by instantiating each contact?

Forgive me for being a pain...but I really want to understand all of this.



Object Oriented Programming is a paradigm that programmers use to create a system that is modular enough that allows its components to be replaceable or reusable, which provides for a more flexible system...

To get an analogy you can think of manufacturers of motherboards for a computer... the individual parts that make up the system as a whole can either be integrated into the board or swappable by means of replaceable parts which you can plug in or out...

If you were a manufacturer of such boards and you create it in such a way that every part was integrated, if one part goes bad then the whole system goes down... or say you come up with a better implementation for some part then you have to create a whole new system to incorporate such new implementations...

The point is the more modular you design your system the more flexible and maintainable it will become...
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Derek Nickerson wrote:

That is a point of Object Oriented Programming. You represent all things as objects.



Ok, but isn't it better (more organized) to have all the information for a contact within a single class with parameters or to use setters/getters -- as opposed to having an object for each type of information?
How should you determine whether to create a class or to do what I've done in this code by instantiating each contact?



If something can be completely described by a primitive or an established class like a string, then you don't have to create a new class for it. So if we're talking about a quantity, it would be an int. There's nothing more about a quantity than a number. A book probably should have a new class to describe it: it has a title, pages, genre, etc.

Now all these things, primitives and objects, are fields in the Contact class. Having private fields with getters and setters is a good thing; do it!

Forgive me for being a pain...but I really want to understand all of this.



Hey, that's what this forum is for!
 
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been focusing on the Contact class, but don't see how this relates at all to my list. Should I be building my methods with ArrayListC<E> implements List<E> instead?

Main:




However, if I were to change the object type in the main class to :



Would I need to then create each of these method names within List? If that is the case, I would need to implement unnecessary methods in the class ArrayListC<E> implements List<E>.

Instead, if I used, List<Contact> contact = new ArrayListC<Contact>(); , would I then move and define the methods that now exist within the Contact class to ArrayListC?
 
Bartender
Posts: 2233
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Derek Nickerson wrote:I've been focusing on the Contact class, but don't see how this relates at all to my list.


That's the point. They should not be related at all.

Couple of notes. I'm short on time so I can't analyse all your code

1. Fields in your Contact class should not be static.
2. Your Contact class should have only one responsibility. To represent a contact. That means it should have methods to get any information about a contact that you need.
   It should not be concerned with getting an input from an user.
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm understanding you, you want List<> and Contact to be "decoupled", that is, List should know nothing about Contact and vice versa.
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips on isolating my Contact class...I have a bad habit of going overboard with implementing certain types of code.

From our assignment: "All the data must be input from and saved/updated in an XML file. You must design your own XML parser. Any imported/copied XML parser is not allowed. Generate an exception for any error."

As far as isolating List and Contact, all I want to do is create a group of contacts that are then stored within an array. I have a class called ArrayListC which implements List...I'm not sure how to use this ArrayListC class with my Contact class. I feel like its redundant, but it is what we're supposed to do -- as far as I know. My intention was to simply create the Contact object and implement the methods from that class. I think you can see from my earlier post what List represents...so how do I go about using the add(E newMem) from List, which is defined in the class ArrayListC, and to be used to store a contact?!?!?!
 
Paweł Baczyński
Bartender
Posts: 2233
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an interfaceSo, you need to write a class that implement that interfaceThis class should not know Contact even exists.

What does this class do? It stores a number of elements of type E where E is just a placeholder (you specify what it is exactly when you declare a variable of this type).
I assume it is supposed to use an internal array do to the job.


So, you may (and probably should) declareThis is the way to make your list work with your contacts.

Read a tutorial about generics and a tutorial about collections.
Your ArrayList is not technically a "collection" (as it does not implement java.util.Collection) but you'll see how to work with them. It is worth to read it anyway.
 
Paweł Baczyński
Bartender
Posts: 2233
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you forbidden to use any class from Java Collection Framework internally in Contact? Like any implementation of java.util.list or java.util.Set?
If you are not forbidden, I recommend you use one of them to store specific information about your contact. One collection for phone numbers, one for emails etc. It would be easier than playing with arrays.
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question, how do I add elements to an array from JFrame? Is it possible to do so from a single JTextField (i.e. jtfAddress)?
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I would do it all in the callback from the button press event.
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the direction I was headed, but I don't know how to allow or account for the multiple addresses entered in the JTF.
Imagine the single JTF, if I write the following: johnd@jd.com, johnd@email.com, jdoe@jd.com
(with or without commas)

How would I separate the 3 and pass them to the array?
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, my personal preference would be to use regular expressions (regex) but others might use indexes and substrings. Or do you mean how would you know what array index to put them in? Without a database, I would say you should do all of one contact at a time. I think that's a reasonable restriction.
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic