CSC 120   --   Lab 9

  1. Lab9a: Experiments with an array of Student objects.

    1. Create a project named Lab9a in NetBeans.

    2. Copy needed files into your src folder.

      Right click on each of these:

      and choose Save Target As... (or Save Link As...) and save the files in the src folder of your project. 

    3. When the proper files are in place, run the project to see the output. This program uses an array of Student objects to represent data describing 15 different students. The output is printed in a JTextArea GUI component using several append method calls.

    4. In the fillArray( ) method of the MUPanel class, change the first element in the array (element 0) so that it represents you. Change "Samantha Student" to your name, change the dorm to your residence hall (or use "off-campus" if you don't live in a residence hall), specify a code number that indicates your current class (1=Freshman, 2=Sophomore, 3=Junior, 4=Senior), specify a Boolean value indicating whether or not you are an intercollegiate athlete, and put in a decimal number representing your GPA (you may use your actual GPA or a made-up value, as you wish).

      Next, change the number of students so that the array will contain two more Student objects. Then add instantiations for a sixteenth and a seventeenth student in the fillArray() method that represents either friends of yours, or made-up people (could be your favorite historical figures, athletes, movie stars, etc.).

      Run this program and observe the results. The output should state that 17 students are listed, and the average GPA should be different than it was originally.

    5. Create and invoke a displaySophomores method.

      Make a copy of the displayEntireArray() method, then paste a copy of this code immediately after the method, and then change the name of the newly copied method to displaySophomores.

      Change the body of the displaySophomores method so that it only processes students in the array that are Sophomores (have a classCode value of 2). To do this, place an if statement immediately inside the for loop, and test to see if the result of the getClassCode() method called for element number k of the studentRoster array is equal to 2. Only if this test evaluates as true should the student in position k of the array be printed and her gpa added to the total. Also, edit the text strings in this second block so that the first line printed by the block is "Listing of Sophomores".

      Add a line in the MUPanel constructor method to invoke this method; it should be invoked AFTER displayEntireArray has been invoked. Run the program now; you should see two sets of output lines in the JTextArea: the original list of all students, and a second list showing only those students who are Sophomores.

    6. Create and invoke three more methods, as follows:

      1. One method displays a listing of students who are athletes,

      2. Another method displays a list of all students who are on the Dean's List (GPA >= 3.5),

      3. The third method displays a list of all the students who live in "McMaster Hall" (Hint:  to test a String for equality, use .equals("McMaster Hall") -- don't use the == operator for the test).

      You will need to add lines in the MUPanel constructor method to invoke these methods.

    7. After ensuring that your program works propertly to display the students in the various collections, close this project.

  2. Lab9b: Experiments with an array of Book objects.

    1. Create a new NetBeans project named Lab9b.

    2. Copy starter file into your src folder.

      Right click on MUPanel.java and choose Save Target As... (or Save Link As...) to save the file in the src folder of your project.

    3. This program WILL NOT run, because MUPanel, displayEntireArray and fillArray try to use an array of Book objects, and the Book class definition is not supplied. Your task for this step of the lab is to provide a complete class definition for a Book class, that allows the program to run correctly.  We will also learn about the "Insert Code" feature of NetBeans that will do a lot of the work of writing a new class definition for us.

      The Book class must have three (3) private data members: a String for the book's title, a String for the book's author, and an Integer for the number of pages in the book.  We also will need a constructor method for the class, a getNumPages() method that returns the number of pages in a Book object, three setter methods (one for each private data member of the Book class), and a toString() method.

      First of all, right-click on the src folder in the NetBeans project tree in the upper-left panel, and choose "New | Java Class" from the pop-up menu.  Enter "Book" (without the quotes) as the name of the class and click the Finish button.  This creates an empty class definition.  Inside the curly braces of the class definition, declare three properties or private data members: Strings for the title and for the author of the book, and an Integer for the number of pages of the book (use the names title, author and numPages for these three properties.

    4. Now we need to write methods for the class that should be a part of pretty much every class we write:  a constructor, getters and setters, and a toString method.  This is such a basic task that NetBeans includes code generation features to do this automatically for a class once we have declared the properties that the class will contain.  We will use this code completion feature to save ourselves some time and work in getting the Book class to be operational.

      Make sure there is at least one blank line between the last variable declaration in your class definition and the closing brace, and position the cursor on one of those blank lines.  Then from the top-level NetBeans menu (the one with the words "File", "Edit", etc. at the top of the application), choose "Source | Insert Code", as seen here:



      A small pop-up menu will appear that is titled "Generate"; from the choices, select "Constructor...".  Now you will see a dialog box that has a line for each of the properties that you defined for the class.  Select all of the properties (you can either check all the checkboxes, or use the "Select All" button at the bottom), and then click the "Generate" button.  Wow, how nice is this?  Notice that NetBeans uses the same name for the properties and for the parameters of the constructor method, so the code must use the "this." notation that was discussed in class to resolve the naming confusion.  This is a standard technique in Java programming.

    5. NetBeans can also automatically generate getters and setters for us.  Choose "Source | Insert Code" again, and this time choose "Getter..." from the Generate pop-up menu.  Let's say for this program we will only need a getNumPages method; so that means that we should only check one checkbox in the dialog that appears, beside the numPages entry, and then when we click the Generate button only one getter is added to the class definition.

      Choose "Source | Insert Code" another time, and choose "Setter...".  We should add a setter for all three properties of the class, so do that now.

    6. We need one more method to complete the Book class definition so we can run the starter program for Lab 9b — a toString method.  NetBeans will generate a very generic toString for us, and then we can edit the method so that the output is exactly the way we want it.  Choose "Source | Insert Code" now, then choose "toString..." from the pop-up menu.  Be sure the checkboxes for all of the fields of the class are checked, and click "Generate".  Notice that the code that NetBeans writes displays the name of the class, and then the name of each field followed by the value of the field.  Let's see what the result of this toString method looks like....

    7. Run the MUPanel.jave file now, and if you chose the proper names for the fields in the Book class, you should see the following output:



      If you examine the code in MUPanel.java, you will see that it uses an array of Book objects that contains 7 books, and that the output shows the contents of each array element on a separate line.  But what if we want a different format for the lines of our output?  To change the output, we need to edit the code that NetBeans generated for our toString method in the Book class.

    8. Switch in the editor back to the Book.java file, and edit the body of the toString method so that the value returned has the following format:
      title_of_book, by author_of_book, has number_of_pages pages.
      When you have correctly re-written the toString method, you should obtain output that looks like this (don't forget to put a period at the end):



    9. Now, let's add two more pieces of information to the Book class: an Integer value representing the copyright year for the book, and a Boolean value representing whether the book is a hardback book or not.  Now we will need a different constructor method for the class that accepts parameters for all five of the properties the class now contains.  In the Book class, delete the current constructor method, then use "Source | Insert Code" to insert a new constructor that instantiates all five properties of the class.

      The toString() method for the Book class should be edited as follows:  first of all, replace the single return statement in the method body with this code:
          String answer = title + ", by " + author + ", has " + numPages + " pages.";
              
          return answer;
      
      This would do the same thing as the original code, but now we have a variable named answer that we can use in an if/else statement, which we will do right now.  At the end of the first line above, concatenate the string " (c) ", followed by the copyright year for the book, followed by the string "; ". Then, using an if/else statement, concatenate the string "Hardback" to the answer String if the hardback private data member is true, or the string "Paperback" if the hardback private data member is false.

      Also, be sure to add "getter" methods for both the copyright year and the hardback status value to the class.

    10. The code in the constructor method of the MUPanel class (as well as displayArray and fillEntireArray) will also need some modifications. For each book, you'll need to specify two more pieces of information when the book is instantiated. Use these values:

      Title Copyright Year Hardback?
      War and Peace 1864 true
      Gone With The Wind 1936 true
      Using Java 2011 false
      Autobiography of a Yogi 1953 false
      Computing Unbound 1992 false
      Heaven Help Us 1996 false
      Computer Systems 2010 true

      When you have correctly modified the Book class definition and MUPanel, you should obtain output that looks like this when you run the program:



    11. Uncomment the append() method calls in displayEntireArray and add if statements to the for loop so that the program calculates the totals and produces the output shown in this final screen shot:



  3. Make .zip files from both your Lab9a project and your Lab9b project, and submit both .zip files to the Lab09 Dropbox in D2L.