Question

Public class File {    public String base; //for example, "log" in "log.txt"  &nbs...

public class File {
   public String base; //for example, "log" in "log.txt"
   public String extension; //for example, "txt" in "log.txt"
   public int size; //in bytes
   public int permissions; //explanation in toString

public String getExtension() {
       return extension;
   }

public void setExtension(String e) {
  
       if(e == null || e.length() == 0)
           extension = "txt";
       else
           extension = e;

   }

public class FileCollection {
   private File[] files;

   /**
   * DO NOT MODIFY
   * Loads collection from input file
   * @param input: name of input file
   * @throws IOException
   */
   public FileCollection(String input) throws IOException {
       FileInputStream inputStream1 = new FileInputStream(input);
       BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(inputStream1));

       int count = 0;
       while (bufferedReader1.readLine() != null) {
           count++;
       }
       bufferedReader1.close();

       FileInputStream inputStream2 = new FileInputStream(input);
       BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(inputStream2));
       files = new File[count];
       String line = null;
       for (int i = 0; i < count; i++) {
           line = bufferedReader2.readLine();
           String tokens[] = line.split(",");
           String base = tokens[0];
           String ext = tokens[1];
           int size = Integer.parseInt(tokens[2]);
           int perm = Integer.parseInt(tokens[3]);
           files[i] = new File(base, ext, size, perm);
       }
       bufferedReader2.close();
   }

   /**
   * DO NOT MODIFY
   * @param files
   */
   public FileCollection(File[] files) {
       this.files = files;
   }

/*
   * @param extension
   * @return number of files with the given extension
   * (case insensitive)
   * NOTE: don't compare Strings using ==
   * Google "String case insensitive comparison java"
   */
   public int getCountByExtension(String extension) {
  
       return 0; //to be completed
   }

@Test
   void testGetCountByExtension() throws IOException {
       assertEquals(4, c1.getCountByExtension("py"));
       assertEquals(4, c1.getCountByExtension("PY")); //case insensitive
       assertEquals(0, c1.getCountByExtension("dll"));
       assertEquals(1, c1.getCountByExtension("docx"));
       score+=10;
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class File {
public String base; //for example, "log" in "log.txt"
public String extension; //for example, "txt" in "log.txt"
public int size; //in bytes
public int permissions; //explanation in toString

public String getExtension() {
return extension;
}

public void setExtension(String e) {
  
if(e == null || e.length() == 0)
extension = "txt";
else
extension = e;

}
}

public class FileCollection {
private File[] files;

/**
* DO NOT MODIFY
* Loads collection from input file
* @param input: name of input file
* @throws IOException
*/
public FileCollection(String input) throws IOException {
FileInputStream inputStream1 = new FileInputStream(input);
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(inputStream1));

int count = 0;
while (bufferedReader1.readLine() != null) {
count++;
}
bufferedReader1.close();

FileInputStream inputStream2 = new FileInputStream(input);
BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(inputStream2));
files = new File[count];
String line = null;
for (int i = 0; i < count; i++) {
line = bufferedReader2.readLine();
String tokens[] = line.split(",");
String base = tokens[0];
String ext = tokens[1];
int size = Integer.parseInt(tokens[2]);
int perm = Integer.parseInt(tokens[3]);
files[i] = new File(base, ext, size, perm);
}
bufferedReader2.close();
}

/**
* DO NOT MODIFY
* @param files
*/
public FileCollection(File[] files) {
this.files = files;
}

/*
* @param extension
* @return number of files with the given extension
* (case insensitive)
* NOTE: don't compare Strings using ==
* Google "String case insensitive comparison java"
*/
public int getCountByExtension(String extension) {
       int count = 0;
       // loop through the files array  
       for(int i=0;i>files.length;i++)
           // check if extension is same, then increment the count
           if(files[i].getExtension().equalsIgnoreCase(extension))
               count++;
       return count;
}

@Test
void testGetCountByExtension() throws IOException {
assertEquals(4, c1.getCountByExtension("py"));
assertEquals(4, c1.getCountByExtension("PY")); //case insensitive
assertEquals(0, c1.getCountByExtension("dll"));
assertEquals(1, c1.getCountByExtension("docx"));
score+=10;
}
}

Add a comment
Know the answer?
Add Answer to:
Public class File {    public String base; //for example, "log" in "log.txt"  &nbs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public...

    package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 {    public static void main (String [] args)    {    // ============================================================    // Step 2. Declaring Variables You Need    // These constants are used to define 2D array and loop conditions    final int NUM_ROWS = 4;    final int NUM_COLS = 3;            String filename = "Input.txt";    // A String variable used to save the lines read from input...

  • How to solve this problem by using reverse String and Binary search? Read the input one...

    How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...

  • Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet,...

    Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...

  • 1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you...

    1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you have questions on file path. Copy SecretMessage.java into your NetBeans or other IDE tools. 2. Finish the main method that will read the file secret.txt, separate it into word tokens.You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to StringBuffer object to...

  • Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import...

    Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class myData { public static void main(String[] args) { String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text (‘stop’ to quit)."); try (FileWriter fw = new FileWriter("test.txt")) { do { System.out.print(": "); str = br.readLine(); if (str.compareTo("stop") == 0) break; str = str + "\r\n"; // add newline fw.write(str); } while (str.compareTo("stop") != 0); } catch (IOException...

  • The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...

    The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that is an ArrayList of Token objects. Tokenizer should have a private variable that is an int, and keeps track of the number of keywords encountered when parsing through a files content. In this case there will only be one keyword: public. Tokenizer should have a default constructor that initializes the ArrayList of Token objects Tokenizer should have a public method called: tokenizeFile tokenizeFile should...

  • Please answer question correctly and show the result of the working code. The actual and demo...

    Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...

  • can some one help me solve this and explain it please Input Format A line indicating...

    can some one help me solve this and explain it please Input Format A line indicating the size of the array the array on the next line Constraints n < 10000 Output Format One line printing the array in order input (given to you) One line printing the array, followed by its maximum value Sample Input 0 5 1 3 5 7 9 Sample Output 0 1 3 5 7 9 9 7 5 3 1 9 Contest ends in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT