Question

In: Computer Science

Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming...

Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming Language that gives us a way to utilize native datatypes as Objects with attributes and methods. A Wrapper class in Java is the type of class which contains or the primitive data types. When a wrapper class is created a new field is created and in that field, we store the primitive data types. It also provides the mechanism to covert primitive into object and object into primitive.Working with collections, we use generally generic ArrayList<Integer> instead of this ArrayList<int>. An integer is wrapper class of int primitive type. We use a Java wrapper class because for generics we need objects, not the primitives."

Problem: Create a Word Counter application, submit WordCounter.java.

Word Counter takes input of a text file name (have a sample file in the same directory as your Word Counter application to we don't have to fully path the file). The output of our Word Counter application is to echo the file name and the number of words contained within the file.

Sample File: Hello.txt:

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// WordCounter.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class WordCounter {

      // varriables to store file name and word count

      private String filename;

      private int wordCount;

      // constructor taking file name, will throw FileNotFoundException if file is

      // not found

      public WordCounter(String filename) throws FileNotFoundException {

            // storing file name

            this.filename = filename;

            // opening file in read mode

            Scanner scanner = new Scanner(new File(filename));

            // counting number of words in file, updating wordCount variable

            while (scanner.hasNext()) {

                  scanner.next();

                  wordCount++;

            }

            // closing file

            scanner.close();

      }

      // returns the words count

      public int getWordCount() {

            return wordCount;

      }

      // returns the file name

      public String getFileName() {

            return filename;

      }

      public static void main(String[] args) throws FileNotFoundException {

            // setting up a scanner, reading input file name from user

            Scanner scanner = new Scanner(System.in);

            System.out.print("Enter a file name: ");

            String file = scanner.nextLine();

            // creating a WordCounter object and passing filename

            WordCounter counter = new WordCounter(file);

            // displaying file name and number of words.

            System.out.println("File name: " + counter.getFileName());

            System.out.println("Number of words: " + counter.getWordCount());

      }

}

/*OUTPUT*/

Enter a file name: Hello.txt

File name: Hello.txt

Number of words: 10


Related Solutions

A)   What are wrapper Classes? What are the available wrapper classes? B) What is Autoboxing and...
A)   What are wrapper Classes? What are the available wrapper classes? B) What is Autoboxing and Unboxing?
Why do you think Java provides both primitive data types and wrapper classes for them? Why...
Why do you think Java provides both primitive data types and wrapper classes for them? Why not just one or the other?
Introduction In this lab, we will look at various aspects of methods in Java programming such...
Introduction In this lab, we will look at various aspects of methods in Java programming such as passing arguments to a method, use of local variables, and returning a value from a method. Exercise-1: RainFall Statistisc(50 pts) Write a program that asks user the total rainfall for the last six months of two consecutive years and do the following: the total rainfall per year the average monthly rainfall (for two years) the month with the most rain (per year) You...
This program focuses on programming with Java Collections classes. You will implement a module that finds...
This program focuses on programming with Java Collections classes. You will implement a module that finds a simplified Levenshtein distance between two words represented by strings. Your program will need support files LevenDistanceFinder.Java, dictionary.txt, while you will be implementing your code in the LevenDistanceFinder.java file. INSTRUCTIONS The Levenshtein distance, named for it's creator Vladimir Levenshtein, is a measure of the distance between two words. The edit distance between two strings is the minimum number of operations that are needed to...
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples...
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples of autoboxing and unboxing. Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
Java Programming Please describe the roles of the following classes in processing a database: Connection ResultSet
Java Programming Please describe the roles of the following classes in processing a database: Connection ResultSet
0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane...
0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane figures from elementary geometry. The object of this assignment is not to do anything useful, but rather to demonstrate how methods can be inherited by extending classes. 1. Theory. A polygon is a closed plane figure with three or more sides, all of which are line segments. The perimeter of a polygon is the sum of the lengths of its sides. A rectangle is...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT