Question: Java Problem I. Solve the following programming problem in Java language . Provide the CODE and a SCREENSHOT of a test run of the code.
Java Problem
I. Solve the following programming problem in Java language. Provide the CODE and a SCREENSHOT of a test run of the code.
Design a program that merges the content of two text files containing chemical elements sorted by atomic number and produces a sorted file of elements. The program should read the content from the two files, and outputs the data ordered by atomic number to the output file. Assume the name of the output file. Use the provided files. The format of each file will be as following:
ATOMIC_NUMBER
ELEMENT_NAME
ELEMENT_ABBREVIATION
ATOMIC_WEIGHT
chems:
1 Hydrogen H 1.01 2 Helium He 4.00 7 Nitrogen N 14.01 8 Oxygen O 16.00 9 Fluorine F 19.00 3 Lithium Li 6.94 13 Aluminium Al 26.98 14 Silicon Si 28.09 17 Chlorine Cl 35.45 18 Argon Ar 39.95 4 Beryllium Be 9.01
5 Boron B 10.81 10 Neon Ne 20.18 11 Sodium Na 22.99 12 Magnesium Mg 24.31 6 Carbon C 12.01 15 Phosphorus P 30.97 16 Sulfur S 32.06
These is my code it doesn't work it said that NO MAIN CLASS IS FOUND I need help:
package chemical; public class Chemical implements Comparable{ int atomicNumber; String name; String abbrev; double atomicWeight; /** * This is parametrized constructor * * @param a * @param n * @param abbr * @param w */ public Chemical(int a, String n, String abbr, double w) { atomicNumber = a; name = n; abbrev = abbr; atomicWeight = w; } @Override public int compareTo(Chemical item) { return Integer.compare(this.atomicNumber, item.atomicNumber); } }
package chemical; import java.io.*; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class ChemicalTest { static List chemicals = new ArrayList<>(); /** * This method take file name as input and read the file * * @param fileName */ public static void readFiles(String fileName) { File file = new File(fileName); int num; String nam; String abbre; double weight; try (Scanner sc = new Scanner(file)) { while (sc.hasNextLine()) { num = sc.nextInt(); nam = sc.next(); abbre = sc.next(); weight = sc.nextDouble(); Chemical chemical = new Chemical(num, nam, abbre, weight); chemicals.add(chemical); } } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * This method take file name as input and write list content to the file * * @param fileName */ public static void writeFile(String fileName) { try (BufferedWriter out = new BufferedWriter(new FileWriter(fileName))) { for (int k = 0; k < chemicals.size(); k++) { out.write(Integer.toString(chemicals.get(k).atomicNumber)); out.newLine(); out.write(chemicals.get(k).name); out.newLine(); out.write(chemicals.get(k).abbrev); out.newLine(); out.write(Double.toString(chemicals.get(k).atomicWeight)); out.newLine(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String file1 = "inp1.txt"; String file2 = "inp2.txt"; String outputFile = "out.txt"; readFiles(file1); //reading file 1 readFiles(file2); // reading file 2 //sorting the list chemicals.sort(new Comparator() { @Override public int compare(Chemical o1, Chemical o2) { return o1.compareTo(o2); } }); // writing list data to file writeFile(outputFile); } } inp1.txt
12 Magnesium Mg 24.31 6 Carbon C 12.01 15 Phosphorus P 30.97 16 Sulfur S 32.06 14 Silicon Si 28.09 17 Chlorine Cl 35.45 18 Argon Ar 39.95 4 Beryllium Be 9.01 5 Boron B 10.81
inp2.txt
1 Hydrogen H 1.01 2 Helium He 4.00 7 Nitrogen N 14.01 8 Oxygen O 16.00 9 Fluorine F 19.00 3 Lithium Li 6.94 13 Aluminium Al 26.98 10 Neon Ne 20.18 11 Sodium Na 22.99
out.txt
1 Hydrogen H 1.01 2 Helium He 4.0 3 Lithium Li 6.94 4 Beryllium Be 9.01 5 Boron B 10.81 6 Carbon C 12.01 7 Nitrogen N 14.01 8 Oxygen O 16.0 9 Fluorine F 19.0 10 Neon Ne 20.18 11 Sodium Na 22.99 12 Magnesium Mg 24.31 13 Aluminium Al 26.98 14 Silicon Si 28.09 15 Phosphorus P 30.97 16 Sulfur S 32.06 17 Chlorine Cl 35.45 18 Argon Ar 39.95
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
