Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 21, Problem 21.8PE
Program Plan Intro
CountOccurenceofWordsfromFile.Java
Program Plan:
- Include a class named “DisplayFrequency”.
- Import BufferedReader.
- Import file from io package.
- Import FileReader from io package.
- Import exception package.
- Import java util package.
- Declare the main() function.
- Declare a scanner for user input.
- Prompt the user to input file path.
- Check if file is present or not.
- Create a word array.
- Inside a try block declare “BufferedReader” and create a new String buffer.
- Split the words using toString() method and append the strings.
- Catch block is declared to handle the exception.
- To hold words as key and count as value, a TreeMap is created.
- Use split method to split words.
- Convert word to lowercase.
- Check if length of key is greater than 0.
- Check if the map contains the key.
- Insert key into map.
- Get the key.
- Update the key value and store the updated value.
- Display key and value for each entry.
- Create a new text file “file”.
- Add words into the text file.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
(Class Average: Reading Student Records from a CSV File) Use Python
Use the csv module to read the grades.csv file from the previous exercise (exercise 9.3).
Display the data in tabular format, including an additional column showing each student’s average to the right of that student’s three exam grades and an additional row showing the class average on each exam below that exam’s column.
This is exercise 9.3
# Importing csv moduleimport csv# empty list to store datadata = []columns = ["firstname", "lastname", "grade1", "grade2", "grade3"]filename = "grades.csv"for i in range(3):firstname = input("Enter First Name : ")lastname = input("Enter Last Name : ")grade1 = float(input("Enter Grade 1 : "))grade2 = float(input("Enter Grade 2 : "))grade3 = float(input("Enter Grade 3 : "))data.append([firstname, lastname, grade1, grade2, grade3])print()# write data and columns as csv filewith open(filename, 'w') as csvfile:# creating a csv writer objectcsvwriter = csv.writer(csvfile)# writing the…
(Same-number subsequence) JAVA
Class Name: Exercise22_05
Write an O(n) program that prompts the user to enter a sequence of integers ending with 0 and finds the longest subsequence with the same number.
Sample Run 1
Enter a series of numbers ending with 0:2 4 4 8 8 8 8 2 4 4 0The longest same number sequence starts at index 3 with 4 values of 8
Sample Run 2
Enter a series of numbers ending with 0: 34 4 5 4 3 5 5 3 2 0
The longest same number sequence starts at index 5 with 2 values of 5
(This is for devc++ version 5.11)
Create a text file with following data to test your code. Please use array of structure and try to use functions and pass array of structures to functions. Read the following data from this text file:
Johnson 85 83 77 91 76Aniston 80 90 95 93 48Cooper 78 81 11 90 73Gupta 92 83 30 69 87Blair 23 45 96 38 59Clark 60 85 45 39 67Kennedy 77 31 52 74 83Bronson 93 94 89 77 97Sunny 79 85 28 93 82Smith 85 72 49 75 63
LOOK AT PICTURES
Chapter 21 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 21.2 - Prob. 21.2.1CPCh. 21.2 - Prob. 21.2.2CPCh. 21.2 - Prob. 21.2.3CPCh. 21.2 - Prob. 21.2.4CPCh. 21.2 - Prob. 21.2.5CPCh. 21.2 - Suppose set1 is a set that contains the strings...Ch. 21.2 - Prob. 21.2.7CPCh. 21.2 - Prob. 21.2.8CPCh. 21.2 - What will the output be if lines 67 in Listing...Ch. 21.2 - Prob. 21.2.10CP
Ch. 21.3 - Prob. 21.3.1CPCh. 21.3 - Suppose you need to write a program that stores...Ch. 21.3 - Suppose you need to write a program that stores...Ch. 21.3 - Suppose you need to write a program that stores a...Ch. 21.3 - Prob. 21.3.5CPCh. 21.3 - Prob. 21.3.6CPCh. 21.4 - Prob. 21.4.1CPCh. 21.4 - Prob. 21.4.2CPCh. 21.5 - Prob. 21.5.1CPCh. 21.5 - Prob. 21.5.2CPCh. 21.5 - Prob. 21.5.3CPCh. 21.6 - Prob. 21.6.1CPCh. 21.6 - Prob. 21.6.2CPCh. 21.6 - Prob. 21.6.3CPCh. 21.6 - Prob. 21.6.4CPCh. 21.7 - Prob. 21.7.1CPCh. 21.7 - Prob. 21.7.2CPCh. 21 - Prob. 21.1PECh. 21 - (Display nonduplicate words in ascending order)...Ch. 21 - Prob. 21.3PECh. 21 - (Count consonants and vowels) Write a program that...Ch. 21 - Prob. 21.6PECh. 21 - (Revise Listing 21.9, CountOccurrenceOfWords.java)...Ch. 21 - Prob. 21.8PECh. 21 - Prob. 21.9PE
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- (Convert decimals to fractions)Write a program that prompts the user to enter a decimal number and displays the number in a fraction.Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template athttps://liveexample.pearsoncmg.com/test/Exercise13_19.txt The problem can not use BigInteger //Below is the Rational LiveExample 13.13 that goes with the problem/question; notice "long" instead of "BigInteger" class Rational extends Number implements Comparable<Rational> { // Data fields for numerator and denominator private long numerator = 0; private long denominator = 1; /** Construct a rational with default properties */ public Rational() { this(0, 1); } /** Construct a rational with specified numerator and denominator */ public Rational(long numerator, long denominator) { long gcd = gcd(numerator,…arrow_forward(True/False): When the source code of a program is amended, it must be reassembled and linked before the updated code may be run.arrow_forward(Intro to Java) Only use while loop or for loop. DO NOT use try loop! DO NOT use null. Avoid using breaks! Tracking Customers This program will read in a series of names, along with an associated gender, from an input file of unknown length. The program uses a while loop to read in each name from the file by using the hasNextLine() method from the Scanner class. Please use a while loop for practice, although you could also write this assignment using a for loop to read from the file. As you read in each name, you will store it in an array, along with a title (Mr. or Ms. or Mx.) dependent on the stated gender. The first line of each file will contain the number of names contained in the file (hint: this is the same structure as the input file in your assignment 18.2). Additionally, this program should have two methods, as follows: The first method: The method is named extractInitial It takes a String parameter It extracts the first letter (initial) from the String parameter It…arrow_forward
- (Intro to Java) explain the answers to the below questions using step-by-step explanation. Assume you have a text file named in.txt with the following contents: All what-ifsAnd what can’t be,I cast them out;Now I am free. -Ms Moem Given Code: File file = new File("in.txt");Scanner input = new Scanner(file);int count = 1;while(input.hasNext()) {String str = input.next();System.out.println(count + ": " + str);count++;}input.close(); Given the code above, add some additional lines of code to write value of count to an output file named count.txt. And, run the code to verify you get the right output to receive full credit. Note that you must run this code without changing the first line of the starter code. File file = new File("in.txt");Scanner input = new Scanner(file);int count = 1;while(input.hasNext()) {String str = input.next();System.out.println(count + ": " + str);count++;}input.close();//Add your lines of code here!arrow_forward(Intro to Java) Tracking Customers This program will read in a series of names, along with an associated gender, from an input file of unknown length. The program uses a while loop to read in each name from the file by using the hasNextLine() method from the Scanner class. Please use a while loop for practice, although you could also write this assignment using a for loop to read from the file. As you read in each name, you will store it in an array, along with a title (Mr. or Ms. or Mx.) dependent on the stated gender. The first line of each file will contain the number of names contained in the file (hint: this is the same structure as the input file in your assignment 18.2). Additionally, this program should have two methods, as follows: The first method: The method is named extractInitial It takes a String parameter It extracts the first letter (initial) from the String parameter It returns a char for the initial. Note: You must write a complete Javadoc for this method to…arrow_forwardDesign void printStart(char *str, char ch). We know that str contains only uppercase words and Spaces, and the first and last characters are not Spaces, and there is only one space between two adjacent words. Ask to print all words in str beginning with the capital letter ch, separated by Spaces, on the screen. (please use c++)arrow_forward
- (Random sentences and story writer) Write an app that uses random-number generation to create sentences. Use four arrays of strings, called article, noun, verb andprepostion. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article, noun.As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the sentence is output, it should start witha capital letter and end with a period. The program should generate 10 sentences and output them to a text box. The arrays should be filled as follows:The article array should contain the articles "the", "a", "one", "some" and "any";The noun array should contain the nouns "boy", "girl", "dog", "town", "car";The verb array should contain the past tense verbs "drove", "jumped", "ran", "walked" and "skipped";The preposition array should contain the preposotions "to", "from", "over", "under" and "on";arrow_forward(Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class to obtain a rational number for the decimal number. Sample Run 1Enter a decimal number: 3.25 The fraction number is 13/4 Sample Run 2Enter a decimal number: -0.45452 The fraction number is -11363/25000arrow_forward(Creating Custom Exception Class) Write a Password class that stores a password string. Passwords must contain at least seven alpha-numeric characters, with at least one letter and one digit. Also write an InvalidPasswordException class that extends Exception. The Password constructor should throw an InvalidPasswordException, which should report an appropriate message based on a given string (e.g. if the string contains no digits, the exception should report this fact). Write a GUI program to prompt the user to enter a password and report an InvalidPasswordException if an invalid password is entered. Please teach and help me how to deisgn the program based on the two files given .Some sample screenshots are shown below: InvalidPasswordException.java public class InvalidPasswordException extends Exception{ public InvalidPasswordException (String errMsg){ super(errMsg); } } Password.java public class Password {private String passwordstr; public…arrow_forward
- (Sort three numbers) Write the following function to display three numbers in increasing order: def displaySortedNumbers(num1, num2, num3): Write a test program that prompts the user to enter three numbers and invokes the function to display them in increasing order. Here are some sample runs:arrow_forward(Count the letters in a string) Write a function that counts the number of letters in a string using the following header: def countLetters(s) : Write a test program that prompts the user to enter a string and displays the number of letters in the string. the answer should be in python.arrow_forward(True/False): A link library is added to a program just before producing an Executable file.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning