0

I'm trying to code in java a pet project using the periodic table. What's the best/efficient way to make a data set out of periodic element properties. Should I make a class of all the elements individually? or make an array or objects?

Initial plan was to just create redundant classes with properties for each individual element. (ex. class for hydrogen, class for carbon, class for nitrogen... etc). with each class having attributes such as atomic number, weight, name, etc.. It's a lot of work, but wondering if there are efficient ways lol. The goal is to make this as modular as possible so i can add more property values or conditions if I choose to do so

I am pretty new to java, so please suggest anything. Also let me know if someone has already done this or has made a library for this. :)

briancpark
  • 9
  • 1
  • 2
  • 5
    You need to know what you're modeling. Making each element a different class (with a same ancestor class) makes sense if you're modeling atoms (as objects that behave differently based on their class). Entries in a periodic table do not behave differently, they all have the same data (atomic weight, group, period, symbol, name...) and the same behaviour (can be drawn somewhere), so it makes sense to model them all as objects of the same class. However, you gave not much detail as to what you're trying to do, so it's difficult to be definitive about it. – Amadan May 28 '19 at 03:00
  • It depends on what properties of the individual elements you plan on using. If you use the more generic stuff like produce number, weight, potential, etc.. you can define a common class with instances. Probably even an enum. Avoid deep instance hyerarchies. – Dr Phil May 28 '19 at 03:03
  • See https://stackoverflow.com/questions/22738023 – tkruse May 28 '19 at 06:21

2 Answers2

0

I was wondering why don't you go with inheritance, create a class with general attributes i mean name,atomic weight,name etc, and inherit those attributes for where you want...

uvindu sri
  • 264
  • 1
  • 4
  • 16
0

Make one class for a chemical element with all its properties.

For categorical properties, like its group; is it a metal (alkali/lanthanoid /...) or noble gas, or trans-uranes or whatever, you can either add it as field, or hold an external Set of them, or indeed use inheritance. Also possible is to implement a (marker) interface Metal.

Inheritance is sensible only for different behavior, methods, in a strict hierarchy. Even an Animal should not inherit from SwimmingAnimal or FlyingAnimal, but implement interfaces Swimming and Flying, as it can be both. So for the groups Metal/Noble Gass/... one might use inheritance. But the background color in the periodic table for a group would then be held in every instance. IMHO it only makes sense if for a Metal there is a deriveSalt(...) and such. And that I do not see.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138