CSC207 Lab 3 — Java Classes 1 Overview You are going to implement three Java classes. Use your new repository – the one that you are using for A1. https://markus.teach.cs.toronto.edu/git/csc207-2016-09/YOUR USERNAME 2 Log in and get things set up in Git and Eclipse s1 drives and s2 navigates. In this section, you will create the directory structure for lab 3 using the command line, create a Java project in Eclipse, create a package and three classes inside the lab 3 directory structure using Eclipse, and then add it all to the repository using the command line. 1. Using the command line, change the current directory to your repository. (You are welcome to check out a fresh copy, or you can use the copy you have checked out for A1.) 2. In your repository, make a lab3 directory and a lab3/src directory. 3. Start Eclipse and use the default workspace. The next set of instructions all happen within Eclipse. (a) Create a new Java Project called Week 3 Lab. Don’t use the default location; instead, use the lab3 directory. (b) Create a package called week3lab. (c) Create a new empty Java class called Author. You’ll fill it in below. In the New Java Class dialog, set the Source folder to Week 3 Lab/src and the Package to week3lab. (d) Create two more classes, Book and Store, in that package. 4. In the command line, add week3lab to your repository and then run git status. You should see this: On branch master Your branch is up-to-date with ’origin/master’. Changes to be committed: (use "git reset HEAD..." to unstage) new file: week3lab/Author.java new file: week3lab/Book.java new file: week3lab/Store.java There will also be an Untracked files section, which you can ignore. 5. Commit (using a commit message such as "Setup for lab 3") and then push the changes. 3 Implement class Author Implement class Author. This class should have two private instance variables of type String — one for the author’s first name and another for the author’s last name. It should also have the following methods (use Eclipse to generate these for you!): 1. A constructor that takes a first name as the first argument and a last name as the second argument. 1 2. Getters and setters for the instance variables. Eclipse will generate these names: getFirstName(), setFirstName(), getLastName(), and setLastName(). 3. Override toString() so that it returns a String representation of this Author. For example, for an author whose first name is Russel and whose last name is Winder it should return the string "Winder, Russel". 4. Add, commit, and push your changes. Because you are still learning your way around Java and Eclipse, you can skip writing Javadoc. Don’t skip it on your assignments, though! 4 Implement class Book Switch roles! Implement class Book. This class should have four private instance variables: a String to store the book title, an array of Authors to keep track of the book’s authors, a String for the book’s ISBN, and an int for the book’s price in cents. It should also have the following methods: 1. A constructor that takes a book’s title, authors, ISBN number, and price, in this order. 2. Getters and setters for the instance variables: getTitle(), setTitle(), getAuthors(), setAuthors(), getISBN(), setISBN(), getPrice(), and setPrice(). 3. Override toString() so that it returns a string representation of the Book as follows: for a book titled “Developing Java Software” with authors Russel Winder and Graham Roberts, ISBN number 978-0470090251, and price $79.75 (represented as 7995 cents), it returns the string "Developing Java Software (978-0470090251, $79.75, by Winder, Russel and Roberts, Graham)". Call (implic- itly or explicitly) method toString in class Author. Add, commit, and push your changes. 4.1 Why int for the price? Java’s floating point types are no good for representing money. Two standard basic ways of managing cash are to keep track of cash values in cents using type int and by using java.math.BigDecimal. (There are other approaches.) To find out more, read here: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency We will also discuss the basics of floating-point representation toward the end of the term. 5 Implement class Store Switch roles! Implement class Store. Write a main method that creates a Book titled “Developing Java Software” with authors Russel Winder and Graham Roberts, ISBN number 978-0470090251, and price $79.75, and then prints the Book’s string representation to standard output (using System.out.println). Also create another Book, with a different Author, and print its representation. Add, commit, and push your changes. Show your work to your TA. 2