Question
Hey guys i have a question on how to solve this java question using methods. just static methods file processing no private class/append/array. these are
Hey guys i have a question on how to solve this java question using methods. just static methods file processing no private class/append/array. these are of the things we've learned.
- Consider the following example which prints all words in the string that begin with "a"
// creates a new Scanner that scans through the String literal provided Scanner lineScan = new Scanner("spider ant elephant aardvark antelope"); while(lineScan.hasNext()) { //reads in current token and advances to the next token String word = lineScan.next(); if(word.startsWith("a")) { System.out.println(word); } }
- Example which counts the number of words in a String:
import java.util.*; public class ScanStringExample { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Enter a phrase > "); String line = console.nextLine(); // nextLine reads until a is found int count = 0; // creates a new Scanner that scans through the phrase entered Scanner lineScan = new Scanner(line); while(lineScan.hasNext()) { //reads in current token and advances to the next token String word = lineScan.next(); count++; } System.out.println("Number of words entered = " + count); } }
a report that is written to a file.
// searches for and returns the next line of the given input that contains // the given phrase; returns an empty string if not found public static String find(Scanner input, String phrase) { while (input.hasNextLine()) { String line = input.nextLine(); if (line.toLowerCase().contains(phrase)) { return line; } } return ""; }
Program Development (QUESTION STARTS HERE)
- 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.
sample
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