String name; // The name of the chemical element int atomicNumber; // The atomic number of the element double atomicWeight; // The atomic weight of the element Override the toString() and equals() methods for the class with methods you d
Define a class named Element with the following private data fields:
- String name; // The name of the chemical element
- int atomicNumber; // The atomic number of the element
- double atomicWeight; // The atomic weight of the element
Override the toString() and equals() methods for the class with methods you design.
Define a Java HashMap named elements that uses the String value of a chemical symbol as the key and an instance of the Element class as the value.
For example, the chemical symbol for Carbon is the letter “C”. The atomic number of Carbon is 6, and the atomic weight of Carbon is 12.011. To add Carbon to the HashMap named elements, first create an instance of the Element class using “Carbon”, 6, and 12.011. Then add an entry to the HashMap collection using “C” as the key and the Element object as the value.
Write a menu-driven program that prompts your user with the following six choices:
Select an option number from the following menu:
Option Action
------ -----------------------------------------
1 Add a chemical element to the collection.
2 Search for an element in the collection.
3 Delete an element from the collection.
4 Load elements from a text file.
5 Display the collection sorted by symbol.
6 Quit.
Enter your choice:
Option 1 will prompt the user for a chemical symbol, the corresponding element name, atomic number, and atomic weight. It will then use that information to add an entry to the elements HashMap.
Option 2 will prompt the user for a chemical symbol such as “C” for Carbon. If that chemical symbol exists in the elements HashMap as a key, the program will display the name, atomic number, and atomic weight of that element. If the chemical symbol is not a key in the elements HashMap, an informative message will be displayed instead.
Option 3 will prompt the user for a chemical symbol such as “B” for Boron. If that chemical symbol exists in the elements HashMap as a key, the program will delete the corresponding entry from the elements HashMap. If the chemical symbol is not a key in the elements HashMap, an informative message will be displayed instead.
Option 4 assumes that a file named “elements.txt” is located in the Data folder of the “C:” drive (“C:\\Data\\elements.txt”). This text file contains one record per line containing the chemical symbol, element name, atomic number, and atomic weight of several elements. The fields are comma separated with no extraneous blank characters. A sample file named “elements.txt” is supplied to assist you in testing this portion of your program. Your program will read and process the records in the file, adding an entry to the elements HashMap collection for each record in the file.
Option 5 will display a formatted list of the contents of the collection sorted in ascending order by chemical symbol. This is an example of the desired output from selecting this option:
Atomic Atomic
Symbol Name Number Weight
------ ------------ ------ -------
Al Aluminum 13 26.982
Ar Argon 18 39.880
B Boron 5 10.810
Be Beryllium 4 9.012
C Carbon 6 12.011
Ca Calcium 20 40.078
Cl Chlorine 17 35.450
Option 6 will terminate the program.
Your program will repeatedly prompt the user with the menu choices until the user enters option 6 to quit. Use input validation to guard against your user from entering menu choices outside the range from 1 to 6.
Enclose your attempt to open the file containing the set of element definitions with try-catch blocks of code. Consider catching the FileNotFoundException and the InputMismatchException exceptions using separate catch blocks.
Use formatting to display the information contained in the HashMap collection. Avoid the use of the tab key to format your output. Unless you know everything there is to know about what you are going to display, the tab key will not perform as expected.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps