Question

In: Computer Science

In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from...

In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from some input text file:
Total number of characters used,without counting spaces and punctuation, total number of words used; Number of words, counting each word only once; Total number of punctuation characters;Number of words that are of size six or more;Number of words that are used only once

Solutions

Expert Solution

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author paramesh
*/
public class CountWords {
public static int totalChars(ArrayList<String> words){
int totalChars = 0;
for(int i=0;i<words.size();i++){
totalChars = totalChars +words.get(i).length();
}
return totalChars;
}
public static int totalCountWithoutSpaces(ArrayList<String> words){
return words.size();
}
public static int totalWords(ArrayList<String> words){
int totalWords = 0;
ArrayList<String> alreadyVisited = new ArrayList<String>();
//counting starts from here
for(int i=0;i<words.size();i++){
boolean visited = false;
for(int j=0;j<alreadyVisited.size();j++){
if(words.get(i).equals(alreadyVisited.get(j))){
visited = true;
}
}
if(!visited){
totalWords++;
alreadyVisited.add(words.get(i));
}
}
return totalWords;
}
public static void main(String args[]) {
System.out.println("Oppgave A");
ArrayList<String> words = new ArrayList<String>();
try {
File fr = new File("Alice.txt");
Scanner sc = new Scanner(fr);

while (sc.hasNext()) {
String line = sc.nextLine();
String[] space = line.split(" ");
for(int i=0;i<space.length;i++){
words.add(space[i]);
}
}
} catch (Exception e) {
System.out.println("File not found");
}
//calling methods
System.out.println("Total words: "+totalWords(words));
}
}


Related Solutions

JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm =...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); for (int i= 0; i<2; i++) { al.add(i); al.add(i+1); hs.add(i); hs.add(i+1); hm.put(al.get(i), al.get(i+1)); hm.put(hm.get(i), hm.get(i+1)); }   System.out.println(al); System.out.println(hs); System.out.println(hm); // {key=value}. ---------------------------------- What output is produced by the following code and why?
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
In Java, you can iterate an ArrayList in different ways. Write the following methods to print...
In Java, you can iterate an ArrayList in different ways. Write the following methods to print Integers in an ArrayList iterating in different ways: 1. // Using basic while / for loop void printArrayListBasicLoop(ArrayList<Integer> al); 2. // Using enhanced for loop (:) void printArrayListEnhancedLoop(ArrayList<Integer> al); 3. // Using basic for loop with iterator void printArrayListForLoopListIterator(ArrayList<Integer> al); 4. // Using basic while loop with iterator void printArrayListWhileLoopListIterator(ArrayList<Integer> al);
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
Java Data Structures (Stack and Recursion) Using the CODE provided BELOW (WITHOUT IMPORTING any classes from...
Java Data Structures (Stack and Recursion) Using the CODE provided BELOW (WITHOUT IMPORTING any classes from Java Library) modify the classes and add the following methods to the code provided below. 1. Add a recursive method hmTimes() to the CODE BELOW that states how many times a particular value appears on the stack. 2. Add a recursive method insertE() to the CODE BELOW that allows insert a value at the end of the stack. 3. Add a recursive method popLast()...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Move all zeros in an arraylist to the end of the list using a temp(java). if...
Move all zeros in an arraylist to the end of the list using a temp(java). if it is an array the code would look like the code below, change it to fit an arraylist int count = 0;     int temp;     for (int i = 0; i < n; i++) {     if ((arr[i] != 0)) {         temp = arr[count];         arr[count] = arr[i];         arr[i] = temp;         count = count + 1;     }     } }
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
Write the following methods in java class ARM that represent state information as well as functional...
Write the following methods in java class ARM that represent state information as well as functional blocks of the ARM platform. [Go through the following 5 classes then write methods for the instructions: mov, str, ldr, add in class ARM, finally, write a print method for ARM in Main.java that can display the registers and the memory locations that have been used. (make sure to make a visualization of the print method instead of just a console dump)] --- Please...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT