ulrail.gif

Lab Quiz 3

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Pledged Lab Quiz

This lab quiz is pledged.  You may use the course textbook, the Java documentation for String, and NOTHING ELSE.  This includes any files in your home directory, previous assignments, slides, etc.  However, you should still save your file into your home directory.

You will need to submit a single file called Book.java.  You may download the skeleton code here: LabQuiz3.java, Chapter.java,  and  Book.java (right click on the link and select "save as" (or "save target as")).

The TAs cannot help you with Java-related issues on this quiz.  If you are having problems getting JCreator to work, can't log in to the machines, etc., then you are welcome to ask the TAs for assistance.  If you do not understand what the lab is asking, the TAs may be able to help you (meaning if it's a problem with our explanation, then they can help you -- if you've been skipping the lectures so far and have no idea what any of this means, you are on your own).

Advice

  • To maximize your points, make sure your program compiles successfully.
     
  • If towards the end things are not going as well as you would want (i.e., the program does not compile), it might be best to try commenting out errant statements (i.e., put a // in front of them)
     
  • If you run out of time, submit what you have -- there will be partial credit for this quiz.  Also remember that you can submit a file as many times as you want -- only the most recent submission is graded.  So once you have it working, submit what you have.

Writing a Book

This lab quiz involves writing a simple Book class.  A Book consists of a title (a string) and an array of Chapters.  We have provided the implementation for Chapter.java for you to use and a tester file, LabQuiz3.java.

Class LabQuiz3

This class is straightforward -- it:

  1. Creates an array of Chapters
  2. Adds the Chapters to a Book object called myBook.
  3. Prints the myBook's contents and the number of pages.
  4. Finds and removes a chapter from myBook and prints the book contents and number of pages again.

A few important things to note:

  • You will need to uncomment lines in LabQuiz3.java as you complete the methods in the Book class.
  • You don't have to submit the LabQuiz3.java file, so you can modify it any way you choose.  In particular, you will probably want to add additional testing code to make sure the methods required below work properly.

Class Book

The Book class has two properties: a string title and an array of Chapters.  It has one constructor, a specific constructor.  The specific constructor uses the String parameter passed to set the title of the book, and the integer passed as the maximum number of chapters allowed. 

The methods you need to provide are:

  • public Book(String theTitle, int maxChapters):  This specific constructor will initialize the title and initialize the array of Chapters to be an array of maxChapters elements which are initially null.
     
  • public String getTitle(): This is an accessor method which returns title.
     
  • public void setTitle(String theTitle): This is a mutator method which sets title to theTitle.
     
  • public int getNumPages(): This is a method which goes through the myChapters array and sums the total number of chapter pages by calling the Chapter class' getNumPages() method on any element in the array which contains a valid (non-null) chapter. and returns the total number of pages when all the valid chapters in the book have been examined.
     
  • public void addChapter(Chapter anotherChapter): This method searches through the myChapters array for an available location to add a chapter.  An available location is any location which is null.  If there is space available, then the chapter passed as a parameter is added to myChapters.  If there is no available spot, then the method need not do anything (in reality, it should do something, but we aren't requiring anything for this situation in the lab quiz).
     
  • public void removeChapter(String theTitle): This method searches through myChapters for the index of a chapter whose title matches the parameter passed.  If a matching element in the array is found, then  that array element is set to null.
     
  • public String toString(): This method returns a string that represents the table of contents of the book.  The string returned will contain the title of the book and a list of the chapter titles and their corresponding page lengths.  See below for details on what the toString() returns.

A few important notes and hints:

  • For String comparison, remember that using == will not work -- you have to use the .equals() method.
  • If you run into a null pointer exception, it could be because of your .equals() methods on the strings.  Note that a cell in the 1-D arrays could be null -- so you can't call .equals() on them (you can, however, call .equals() on a valid object and have null be the parameter).
  • All your methods must be public!  This is needed for testing.
  • Don't worry about case-insensitive searching.  Meaning if the Chapter to remove is "Introduction", your method will not ever be given the parameter "introduction" (notice the capitalization is different).

Sample Output

Your output doesn't have to look exactly like this (i.e. you can format it differently), but it should print the same information.  The output from LabQuiz3 is:

Welcome to Lab Quiz 3!

Step 1: Creating 4 chapters.

The chapters that were created are:
Introduction (10 pages)
The Rules for Playing Jeopardy (20 pages)
What is How to Win at Jeopardy? (20 pages)
Conclusion (30 pages)

Step 2: Creating a book called Jeopardy for Dummies.

Step 3: Printing the book contents:
Book Title: Jeopardy for Dummies
Chapters:
        Introduction (10 pages)
        The Rules for Playing Jeopardy (20 pages)
        What is How to Win at Jeopardy? (20 pages)
        Conclusion (30 pages)

myBook has 80 pages.

Step 4: Removing the Introduction

Book Title: Jeopardy for Dummies
Chapters:
        The Rules for Playing Jeopardy (20 pages)
        What is How to Win at Jeopardy? (20 pages)
        Conclusion (30 pages)

myBook has 70 pages.

The blue text is what is returned by the toString() method.

How to Proceed

Get the steps working in order!  Although we give you the LabQuiz3 class, you can modify it to test things. 

  1. Get getTitle() and setTitle(), and the specific constructor working first working first, and test them. 
  2. Then code up addChapter(), and test it with a few Chapters to make sure that it is working. 
  3. Then get removeChapter() working.
  4. Then get getNumPages() and toString() working. 

You don't have to submit LabQuiz3.java, so modify away!  Note that if you do not have enough time to complete all the methods, completing the first few properly will earn more partial credit points than working on all the methods, but not getting any to work.

Other Requirements

You are only required to follow a few of the good programming practices discussed in HW J1 (yes, you may follow that link during the lab quiz).  If you have time, feel free to do the others -- but due to the time constraints of the quiz, you don't have to do them all.  The ones you must do are listed below.

  • Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.
     
  • Proper variable names, if you use them in your methods.
     
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.

Submission

When you are finished, submit your Book.java file.  You will have to "sign" a pledge on the submission.

Lab 12

If you finish early, you can work on lab 12.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif