CSC212 Lab 7b 2014
--D. Thiebaut (talk) 20:56, 8 October 2014 (EDT)
This section of the lab assumes that you have installed Eclipse on your system. If not, then use one of the Linux Mint machines in the lab. For the purpose of discovering Eclipse and its amazing features, we will develop a program using the top-down approach.
Contents
Your Lab Assignment
Write a program that reads a collection of lines from the standard input (either from the keyboard or redirected from a file). The lines contain a list of file names along with their size, expressed in Kilobytes. Here's an example of the type of input your program will receive:
./ArtOfAssembly 10012 ./ArtOfAssembly/CH01 276 ./ArtOfAssembly/CH02 300 ./ArtOfAssembly/CH03 480 ./ArtOfAssembly/CH04 272 ./ArtOfAssembly/CH05 328 ./ArtOfAssembly/CH06 620 ./ArtOfAssembly/CH07 148 ./ArtOfAssembly/CH08 748 ./ArtOfAssembly/CH09 436 ./ArtOfAssembly/CH10 248 ./ArtOfAssembly/CH11 436 ./ArtOfAssembly/CH12 348 ./ArtOfAssembly/CH13 524 ./ArtOfAssembly/CH14 500 ./ArtOfAssembly/CH14/org_gifs 72 ./ArtOfAssembly/CH15 380 ./ArtOfAssembly/CH16 772 ./ArtOfAssembly/CH16/chars 32 ./ArtOfAssembly/CH17 248 ./ArtOfAssembly/CH18 304 ./ArtOfAssembly/CH19 612 ./ArtOfAssembly/CH20 348 ./ArtOfAssembly/CH21 200 ./ArtOfAssembly/CH22 196 ./ArtOfAssembly/CH23 60 ./ArtOfAssembly/CH24 352 ./ArtOfAssembly/CH25 256 ./ArtOfAssembly/chars 16 ./ArtOfAssembly/fwd 68
The last line indicates that the file fwd in the directory ArtOfAssembly is 68 KBytes in size.
The Assignment is to keep track of all the files that are larger than 100 KBytes and store them in a list for later processing (for example because we may want to compress them).
Your program will print the list of the large files (>100K) at the end of the program.
Top Level
Think for a minute about what your program needs to do:
1. get the input, one line at a time, 2. extract the size from the line. 3. if the size is larger than 100, add the file and its size to a list 4. once the input has been completely read, output the list of large files
Launch Eclipse
- Close the Welcome Window
Create A Project
- Create a new Project. Eclipse organizes Java programs into project. Let's create a project called CSC212 where you'll store your Java file(s). You should have to do this only once this semester (unless you want to organize your files by, say, homework or lab sections).
- File
- New
- Java Project
- CSC212
- Finish
Create a new Java File
- Select CSC212
- Right-Click or Control-Click on it, depending on your type of machine, and pick
- File
- New
- Class
- Lab7a (don't add the .java extension)
- Click on public static void main( String[] args )
- Finish
Reading Input
In the main program, declare an ArrayList called lines.
ArrayList lines = null;
- Notice that the ArrayList word is underlined in red, and there's a small red X in the left margin.
- Click on the red X. A menu of suggestions will appear. Usually the top suggestion will be the right one. Sometimes not, so make sure you read what it suggested as a fix:
- Double-click the import 'ArrayList' (Java.util) suggestion. This will clear the red marker and underline.
- Add more code:
ArrayList lines = null; lines = readInput();
- Note the new red underline and cross. Click on the cross and accept for Eclipse to create a new method for you. You will get a new method in your code:
private static ArrayList readInput() { // TODO Auto-generated method stub return null; }
- Instead of making the method read from the standard input (i.e. the keyboard), make it create a list of string and return it:
private static ArrayList readInput() { ArrayList l = new ArrayList(); l.add( "ArtOfAssembly 10012" ); l.add( "ArtOfAssembly/CH01 276" ); l.add( "ArtOfAssembly/CH02 300" ); l.add( "ArtOfAssembly/CH03 480" ); l.add( "ArtOfAssembly/CH04 272" ); l.add( "ArtOfAssembly/CH05 328" ); l.add( "ArtOfAssembly/CH06 620" ); l.add( "ArtOfAssembly/CH07 148" ); l.add( "ArtOfAssembly/CH08 748" ); l.add( "ArtOfAssembly/CH09 436" ); l.add( "ArtOfAssembly/CH10 248" ); l.add( "ArtOfAssembly/CH11 436" ); l.add( "ArtOfAssembly/CH12 348" ); l.add( "ArtOfAssembly/CH13 524" ); l.add( "ArtOfAssembly/CH14 500" ); l.add( "ArtOfAssembly/CH14/org_gifs 72" ); l.add( "ArtOfAssembly/CH15 380" ); l.add( "ArtOfAssembly/CH16 772" ); l.add( "ArtOfAssembly/CH16/chars 32" ); l.add( "ArtOfAssembly/CH17 248" ); l.add( "ArtOfAssembly/CH18 304" ); l.add( "ArtOfAssembly/CH19 612" ); l.add( "ArtOfAssembly/CH20 348" ); l.add( "ArtOfAssembly/CH21 200" ); l.add( "ArtOfAssembly/CH22 196" ); l.add( "ArtOfAssembly/CH23 60" ); l.add( "ArtOfAssembly/CH24 352" ); l.add( "ArtOfAssembly/CH25 256" ); l.add( "ArtOfAssembly/chars 16" ); l.add( "ArtOfAssembly/fwd 68" ); return l; }
Testing Method readInput()
- It is a good idea to always test a new method once it is added to the code.
- Change main() as follows:
public static void main(String[] args) { ArrayList lines = null; // get lines from input lines = readInput(); // display input lines (for debugging) Iterator<String> it = lines.iterator(); while ( it.hasNext() ) { String line = (String) it.next(); System.out.println( line ); } }
- You will have noticed that Eclipse suggests code as you type. This is a nice feature.
- Run the program by clicking on the white triangle in a green circle in the top bar. If you get a window opening up asking you to select a resource to save, simply check the box Always save resource before launching, and click OK.
- Notice that a new tab opens up in the bottom pane for the Console output. Your output will be there. You can scroll it up or down.
Minimizing The Methods
- Now that we see that readInput() works according to plans, let's minimize it, as it is a very long method.
- Locate the small minus-sign in the margin, to the left of private static ArrayList readInput() {.
- Click on it. Notice that the code of readInput() disappears. It's still part of the method, but not shown in the editor to save space. You can get it "back" by clicking on the plus-sign.
Adding a Private Function
- The code to display the list of lines can be useful in other parts of our program. Let's make it a method.
- Change your main() function to this code:
public static void main(String[] args) { ArrayList lines = null; // get lines from input lines = readInput(); // display input lines (for debugging) displayListOfLines( lines ); Iterator<String> it = lines.iterator(); while ( it.hasNext() ) { String line = (String) it.next(); System.out.println( line ); } }
- Eclipse will suggest adding the method you just called. Just do it, and put the 4 lines of code required for printing the list inside the new method.
- Fix errors (small red crosses) that Eclipse points out to you, if any.
- Main should look like this:
public static void main(String[] args) { ArrayList lines = null; // get lines from input lines = readInput(); // display input lines (for debugging) displayListOfLines( lines ); }