CS-140 Lab 6 -- Eclipse Tutorial

This is going to be a pretty easy lab... we are going to start using Eclipse. Start off by reading through the Using Eclipse web page. This is also linked from the main web page under Supplemental Materials.

Getting Started

Follow the directions on the Using Eclipse web page to set up an Eclipse workspace, and project. Then create a new package called lab06.

Creating a Simple Class

  1. Create a new class in the lab06 package called Student. Check the box under Which method stubs would you like to create? labeled public static void main(String args[]). You may also want to check Generate comments. If you do so, Eclipse will add Javadoc comments to your class. When you click Finish, Eclipse will create a Student.java file for you, with some information already filled in.
  2. Add three private fields to this class... int age, String name, and boolean male.
  3. Generate a constructor using Source->Generate Constructor using Fields...
  4. Generate all getters and setters using Source->Generate Getters and Setters.... Check the box for all three fields.
  5. Replace the comment // TODO Auto-generated method stub in the main method with Java code to create a new Student object. Fill in the fields with your information. Then print out this student object using System.out.println. Notice that Eclipse gives you hints along the way.
  6. Select Run->run to invoke your code. You will see what gets printed in the Console pane at the bottom of the Eclipse window. Not very interesting. The student object currently prints as lab06.Student@14db9742 (or something like that). This is because the default string representation of an object is package name, followed by class name, followed by the hexadecimal location of the object in memory. We can do better.
  7. Create a toString method by moving above the main method, and choosing Source->Override/Implement Methods.... Then check the toString() box and hit OK. You will need to replace the auto-generated lines // TODO Auto-generated method stub and return super.toString(); with a better string that describes this student object. For instance, return "[" + name +" age: " + age + "]";
  8. Rerun your code with Run->run... does it look better?
  9. Add the phrase implements Comparable<Student> to your Student class. Now look at the Problems tab on the bottom pane. You will notice that there is one error. Look at that error. Can you figure out what is wrong?
  10. Notice that there is a little red X in front of the line where the Student class is declared. If you move your cursor over this red X, and left click, a pop-up box will appear suggesting solutions to this problem. Try double clicking on the "Add unimplemented method" line. Now check out the bottom of the file! Fix the "compareTo" method to return this.name.compareTo(arg0.name);. Then, hit Ctrl-s to save the file. Does that fix the error?

Managing an ArrayList of Students

Perform the following steps. If at any time, Java errors or warnings appear in the Problems tab, fix them as soon as possible.

  1. Create a new class called Section. This should also have a main method.
  2. Add a private field - an ArrayList of Student objects called students.
  3. Hit Ctrl-Shift-O to add the proper import line.
  4. Create a constructor using Source->Generate Constructor using Fields... trick. However, this time uncheck the box next to students. Then replace the line that says super(); with students = new ArrayList<Student>();.
  5. Choose Source->Generate Delegate Methods and check the add(Object) box under students, and choose OK. Now, fix the declaration of the argument of the add method so that arg0 is a reference to a Student object instead of an Object object.
  6. Add the following methods to your code: (feel free to cut and paste)
        public boolean readFile(String fileName) {
    		try {
    			Scanner in = new Scanner(new File(fileName));
    			while(in.hasNext()) {
    				String name = in.next();
    				boolean male = in.nextBoolean();
    				Student ff = new Student(0,name,male);
    				this.add(ff);
    			}
    			in.close();
    			return true;
    		} catch(FileNotFoundException e) {
    			System.out.println("Unable to open file: " + fileName);
    		}
    		return false;	
    	}
    	
    	public void sort() { Collections.sort(students); }
    	
    	public void report() {
    		for(Student s : students) {
    			System.out.println(s);
    		}
    	}
    

    You will need to rerun Ctrl-Shift-O to refresh the imports.

  7. Update the main method with the following code...
      Section cs140b = new Section();
      cs140b.readFile("students.txt");
      cs140b.sort();
      cs140b.report();
    
  8. Download file students.txt, and move it to your eclipse project cs140 directory. The cs140 directory is a sub-directory of the eclipse workspace directory. If you refresh the cs140 project icon in package explorer, the result should look something like the following screen capture...
  9. Now click the icon that contains the green circle with the white triangle facing right. This is the "Run" icon. Does the right stuff appear in the console? (I don't know and don't want to publish your ages, so I left them all at 0 on purpose.)

Extra Credit

See if you can figure out how to get Eclipse to run javadoc. Check the error messages from Javaoc. Can you fix the error messages? Look at the results. Is it useful? Edit the javadoc comments and rerun.

Creating a zip file for submission

Left click on the lab06 package item in the Package Explore pane of the Eclipse window. Then right click to bring up a pop-up menu of actions. Choose the Export... action. This will open the Export dialog box. Expand the General folder, and choose Archive File. Then click on Next.

You will now see the Archive file dialog. There should be a check mark already next to lab06 in the left folder list, and check marks next to the two java files, Section.java and Student.java in the right hand column. (If you created javadoc, add the javadoc results to your archive file.) Click on the Browse... button to choose a folder to put the result in. Whichever folder you end up in, make the file name <userid>_lab06.zip where <userid> is your e-mail userid. Under Options, make sure you have Save in zip format selected in the left column, and Create only selected directories selected in the right column. Then hit the Finish button.

Double check the resulting zip file. Make sure it has a single directory called lab06 that has two files in it, Section.java and Student.java.

Once you have checked your file to make sure it is correct, go to blackboard using your web browser, open the CS-140-B1-Fall16 course, click on "Content"; then "Lab Submissions" and then "Lab 06 Submission" and upload your zip file.

Grading Criteria

This lab is worth 10 points. You will get the full 10 points if your code compiles without warnings, and produces the correct output. The following are reasons for deductions:

If you do the extra credit, you will get up to 3 extra points added to your grade, as long as your grade does not exceed 10 points.