To Lecture Notes

IT 313 -- Jan 28, 2020

Review Exercises

  1. How do you do the following in an IntelliJ project?
    1. Add a new class?
      Ans: Right click on the src file and select New >> Class.  Enter the class name and click OK.
    2. Add a new file?
      Ans: Depending on where you want to place the new file, right click on the project name or on the src file. Select New >> File; enter the new file name.
    3. Read from the file in b?
      Ans: Use these statements:
      File f = new File("input.txt");
      Scanner s = new Scanner(f);
      String line = s1.nextLine( );
      
      If the input file is in a module, for example module1, in the src folder, use this statement to create the file:
      File f = new File("input.txt");
      
    4. Write to an output file? Ans:
      PrintWriter pw = new PrintWriter("output.txt");
      pw.println("Test output.");
      pw.close( );
      
  2. This main method is supposed to load the values from a file into a double array, then compute the average of the values in this array, writing the average to an output file. Reorder the lines of this main method to accomplish this:
    Here are the contents of the numbers.txt file:
    34,21,78,60,53,21,9
    
    Ans: the reordered lines are shown below the original scrambled main method.
  3. Refactor the main method in Exercise 1 by extracting two methods: loadValuesFromFile that returns an array of int, and computeAverage that returns the average of the value in the array of int.
    Ans: Look below the reordered lines.
  4. Given the classes B and Main, draw the variable trace table and predict the output.
    Ans: The variable trace table and output are shown below the classes.

Reading Data from the Web

Project 4

Using a FileChooser

ArrayLists

Additional Examples