How can I avoid using a lot of variables?

I would like to create a simple program that displays the atomic mass of any input element. I am taking a Java course that I recently started, so I don’t know how to avoid using more than 100 variables, each with atomic mass elements.

Also, how can I get an if to use the input name from a user (which I know how to store in a string) and match it with one of the elements to display the mass of the element (corresponding to the method used to store several elements).

How can I condense this sample code:

 int carbon = 12; int oxygen = 16; int hydrogen = 1; int sulfur = 32; etc.... 
+4
source share
8 answers

It seems like your first step is to find out about the map data structure. You can use it to associate string names with integer values, and then view them later.

 Map<String, Integer> elements = new HashMap<String, Integer>(); elements.put("CARBON", 12); elements.put("OXYGEN", 16); //etc 

Then, if you have an entrance, you can find the number.

 String userInput = scanner.next(); // or however you're getting input Integer atomicWeight = elements.get(userInput.toUpper()); if (atomicWeight == null) //print element not found etc 

Then, when you have the program down and running, you can find out which technology is suitable for loading reference data from outside the source code, whether it be a file or database or a web service or something else.

+9
source

I would define an enumeration if I ran into this problem.

 public enum Elements { HYDROGEN(1), ... UNOBTANIUM(666); public final int atomicWeight; Elements(int atomicWeight) { this.atomicWeight = atomicWeight; } } 

then to get the right item it

 String name = ...// from user input Elements e = Elements.valueOf(name.toUpperCase()); 
+3
source

Since the atomic mass of the elements will not change at any point in your application, you must define them as final:

 public class AtomicMass { public static final int CARBON = 12; public static final int OXYGEN = 16; ... } 

... or you can use the enumeration:

 public static enum Element { carbon(12), oxygen(16), hydrogen(1), sulfur(32); private int atomicMass; private Element( int mass ) { this.atomicMass = mass; } } 

If you order your elements sequentially (and add UNKNOWN for 0), you don’t even need to explicitly provide the mass.

+1
source

I would recommend using an enumeration, as some suggested, although I would do it a little differently. Cards have a lot of overhead, and since your data is not dynamic, it is not very convenient. The atomic mass should be decimal (double or BigDecimal, depending on what you use it for), not int

 public enum AtomicElement { HYDROGEN(1.00794), HELIUM(4.002602), ...; private double atomicMass; private AtomicElement (double atomicMass) { this.atomicMass = atomicMass; } public int getAtomicNumber() { return ordinal(); } public double getAtomicMass() { return atomicMass; } public static AtomicElement forAtomicNumber(int atomicNumber) { return AtomicElement.values()[atomicNumber]; } public static AtomicElement forElementName(String elementName) { return AtomicElement.valueOf(elementName); } } 

Then you can search by atomic number or element name

  AtomicElement.forAtomicNumber(2); AtomicElement.forElementName("CARBON"); 

This, however, assumes that you are going to represent the entire periodic table without spaces in the data, since it uses the value of ordinal () as an atomic number. If you need spaces, you will need to have an int field for the atomic number, and your function "forAtomicNumber" will have to cycle through "values ​​()" to find the number with the given number.

You can even expand this if you want to include known isotopes, etc., if your requirements dictate it.

+1
source

I like to group related data into arrays or lists of arrays.

 String[] elements = new String[# of elements in table]; 

Depending on the position of the element, you may have an atomic number.

Then I would go through them to find any element or fill an array. You can look into the Java Scanner class to get input from the user.

0
source

Create a class called Element that contains attributes such as name, atomic number, etc. Each element will correspond to an instance of Element . Then you can put all Elements on multiple cards using name, atomic number, etc. Use the factory class to create and initialize maps and provide search methods.

0
source

If I understand you correctly, you just want to have only one variable to store all the elements and their masses, in which case I would recommend HashMap. This will not really save on code lines, but will allow you to make number 2 pretty easy. HashMaps stores a set of key-value pairs, and you can get the value if you have a key so that it creates a list:

 //Declare a new hashmap and initialize it HashMap<String, Integer> elements = new HashMap<>(); //Add element information elements.put("CARBON", 12); elements.put("OXYGEN", 16); elements.put("HYDROGEN", 1); elements.put("SULFUR", 32); 

Then, for example, to get user input from a dialog box and print the result on the command line, you would do something like this:

 //Collect user input and convert it to all upper case (in real life you would validate this) String input = JOptionPane.showInputDialog(null, "Please enter an element name").toUpperCase(); //If element name exists in hashmap print its atomic weight if(elements.containsKey(input.toUpperCase())){ System.out.println("Atomic Weight: " + elements.get(input)); } 
0
source

Store data in a file

 Element, Weight oxygen = 16 carbon, 12 . . . 

pseudo code:

 //Read data file into a `Map<String, int>` //Get user input //Access map //Output 
0
source

Source: https://habr.com/ru/post/1435166/


All Articles