Question
Topics This assignment will give you practice with line-based and token-based file processing and writing to a file. Instructions You are going to write a
Topics
This assignment will give you practice with line-based and token-based file processing and writing to a file.
Instructions
You are going to write a program that will read the contents of a scientific log that contains several series of elements to chemical formulas. You will write a program that will combine the elements on each individual line of the file to create their chemical formulas. These formulas will then be named or identified as unknown and a summary will be printed as a report that is written to a file.
Program Development
- You must break your program into a minimum of 3 methods, including the main. Each method should accomplish a specific task and be appropriately named.
- I recommend writing the program without the file output to start. Once you get that part working, then print it to the file instead of the console.
Creating Formulas
Each line of the file will contain a number of elements each separated by a space. These elements are guaranteed to be grouped when there are several of the same element. You need to count the number of grouped elements by counting the number of occurrences of that element and then include that count after the element in the formula, except when there is only one.
For example:
H H O O
should become H2O2 because there are two H's followed by two O's.
and
K H C C C C H H H H O O O O O O
should become KHC4H4O6 combining the C's H's and O's
Naming the Compound
After you construct the formula for a given line in the log, you should then determine if the common name of that compound is known or unknown. Your program only knows the following six compounds:
- NaHCO3 is Baking Soda
- H2O2 is Liquid Bleach
- KHC4H4O6 is Cream of Tartar
- N2O is Laughing Gas
- NaCl is Salt
- C12H22O11 is Sugar
- All other compound formulas are unknown
Your program should compare the formula you created to these known compounds, printing the compound name if known and "UNKNOWN FORMULA" followed by the formula if the compound is unknown.
Writing the summary to a file
As you analyze each line, you should print a summary to a new file called report.txt using a PrintStream. The summary should include the name of the compound (if known) or UNKNOWN FORMULA followed by the formula created for that entry (if unknown).
The file should also include a total count of formulas processed, as well as the count of the total and the percentage of unknown formulas.
Note that the percentage is printed to the screen without any decimal places.
Example 1
So for the contents of log.txt,
N N O Na Cl N N O Na Cl K H C C C C H H H H O O O O O O C C C C C C C C C C C C H H H H H H H H H H H H H H H H H H H H H H O O O O O O O O O O O N N O O O O O N N O Na H C O O O H H O O
report.txt should contain:
Let's do some chemistry... Laughing Gas Salt Laughing Gas Salt Cream of Tartar Sugar UNKNOWN FORMULA (N2O5) Laughing Gas Baking Soda Liquid Bleach 10 total formulas processed. 1 (10%) unknown formulas.Transcribed image text
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started