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.

      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. You must write a minimum of three methods for this class: a constructor method (to see the order of the parameters for the constructor, refer to the statements in fillArray that call the constructor), a method named getNumPages() that returns the number of pages in a Book object, and a toString() method that returns a String formatted in this format:
      title_of_book, by author_of_book, has number_of_pages pages.
      When you have correctly written the Book class definition, you should obtain output that looks like this screen shot image. when you run the program.

    4. 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. If we do this, we'll also need to modify the constructor method of the class, so that it accepts two more parameters of the appropriate type, and stores those parameter values in the new private data members.

      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

      The toString() method for the Book class should be changed as follows: after forming the string as you did above, concatenate the string " (c) ", followed by the copyright year for the book, followed by a semicolon and a space. 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.

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

    5. 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 a .zip file from your Lab9a project, and submit it to the Lab09 Dropbox in D2L.

    You do not need to submit a .zip file for your Lab9b project.