Lab 6 Comp 170-201 -- Dordal/Nabicht -- Wed, October 2, 2002 Final version due Fri Oct 11 In this lab we will explore classes Author and Book; the latter uses the former. As usual, this file is available at www.cs.luc.edu/~pld/courses/170; cut and paste from there rather than retyping. Here is class Author, used to keep track of information about an author. In our case, that information is the first and last name. class Author { private String _first; private String _last; public void setFirst(String s) { _first =s; } public void setLast(String s) { _last =s; } public String first() { return _first; } public String last(){ return _last; } public String toString(){ return _first + " " + _last; } } Here is a program to test the above. Note that it uses the Keyboard class too: public class AuthorTest { public static void main(String[] args){ Author a1 = new Author(); a1.setFirst("Chris"); a1.setLast("Colby"); System.out.println(a1.toString()); Author a2 = new Author(); System.out.print("Enter Author First name: " ); a2.setFirst(Keyboard.readString()); System.out.print("Enter Author Last name: " ); a2.setLast(Keyboard.readString()); System.out.println(a2.toString()); } } Demo1 ----- Your first step is to create a project containing the Author, AuthorTest, and Keyboard classes. Run it to verify that everything works. Note that from the AuthorTest class, in the class-view window, are *two* arrows indicating class dependencies: one to Author, and one to Keyboard. Now, we'll try using the "object bench" in BlueJ. To do this, right-click on the Author class icon, and select "new Author". You will be asked for a name for the new object you're creating; the default "author_1" is fine. The object should now appear in the box below the classes (this box is called the "object bench"). Next, test out some of the instance methods from the Author class. You run the methods by first selecting an object, right click to get the menu, and select the method to run. Do the following in sequence: (a) setFirst: You will then get a dialog box where you can choose the value for the parameter s. You need to supply a value for the parameter s. Bluej reminds you that the value should be of type String. Type in a String value, for example "James". This is a String value, so you have to type the double quotes around the characters. (b) first: You will then get a box indicating that the result is "James". (c) setFirst (again): Invoke this again, but this time supply the parameter "George". (d) first: You will then get a box indicating that the result is "George". (e) setLast: Use this to set the last name to "Sand" In a similar fashion, explore the methods last and toString. Also try the "inspect" option, which lets you view the entire class. Exercise 1 ---------- Complete the following program so that the two authors' names appear in alphabetical order. You will need to use the s1.compareTo(s2) function for Strings; see the book's table of String-class methods (page 89 in 3rd ed) Essentially, if Author a1 comes after Author a2, you just swap them. Be sure that you check the first names if the last names are equal. public class MoreAuthorTest { public static void main(String[] args){ Author a1 = new Author(); System.out.print("Enter Author First name: " ); a1.setFirst(Keyboard.readString()); System.out.print("Enter Author Last name: " ); a1.setLast(Keyboard.readString()); Author a2 = new Author(); System.out.print("Enter Author First name: " ); a2.setFirst(Keyboard.readString()); System.out.print("Enter Author Last name: " ); a2.setLast(Keyboard.readString()); Author temp; (if you need to swap a1 and a2) // Your code goes here System.out.println("Authors in alphabetical order are"); System.out.println(a1.toString()); System.out.println(a2.toString()); } } Test it with two authors with different last names, and two authors with the same last name, both entered in alphabetical order and entered in reverse order. //============================================================================ Now we'll consider class Book: class Book { private Author _author; private String _title; public void setAuthor(Author a) { _author =a;} public void setTitle(String s) {_title =s;} public Author author(){return _author;} public String title(){ return _title;} public String toString(){ return "Author = " + _author.toString() + ", Title = " + _title; } } Demo 2 ------ What happens now when you try to create a Book object on the object bench? What happens when you call setAuthor? Try this with strings. Try this with author_1, the author object you created in demo1, entered *without* quotes. Question (give a written answer to this in your program): Why are names entered with quotes, but not author_1? What determines when quotes are used? Exercise 2 ---------- Add *constructors* to each class. The Author constructor should take two strings, Author (String fname, String lname); And the Book constructor should take parameters Author and String: Book (Author a, Title t); Use your constructors to eliminate the calls to setFirst, setLast, setTitle, setAuthor, etc. in the following. Note that with the constructor, you won't be able to construct the author object until after the first and last names are read in, etc. public class BookTest{ public static void main(String[] args){ Author a = new Author(); a.setFirst("Stephen"); a.setLast("Hawking"); Book b1 = new Book(); b1.setAuthor(a); b1.setTitle("A Brief History of Time"); System.out.println(b1.toString()); Book b2 = new Book(); b2.setAuthor(new Author()); System.out.print("Input author first name: "); String afname = Keyboard.readString(); b2.author().setFirst(afname); System.out.print("Input author last name: "); String alname = Keyboard.readString(); b2.author().setLast(alname); System.out.print("Input book name: "); String title = Keyboard.readString(); b2.setTitle(title); System.out.println(b2.toString()); } } Turn in the two Exercise programs, and the answer to the question in Demo 2.