aspose file tools*
The moose likes Beginning Java and the fly likes Creating classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating classes" Watch "Creating classes" New topic
Author

Creating classes

Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
If you are asked to write a program to handle a book collection which will allow for adding and deleting books, what classes do you need?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

Why don't you start the list? There are at least two nouns in your description that leap out...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
rogel garcia
Ranch Hand

Joined: Dec 19, 2003
Posts: 41
[removed]


SCJP 1.4
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Rogel: please don't provide direct answers--instead point people in the right direction. We try to encourage people to do their own thinking as much as possible.
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
we were asked to do it using java programming
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Okay, but that's not important during the design process.
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
Yh. i was wondering if it's possible to use just one class for everything.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Possible? Of course: we could program Java just like it's Fortran-77 if we wanted to. Doesn't mean we *should*.
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
ok. so which is better? to create only one class or several classes?
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
I would suggest using 3 classes.

1st class containing main() function;

You can create 2nd class called book with which contains instance variables storing details of particular book;

3rd class would probably create an array or list which stores the books in form of objects of class Book.It would have functions for inserting book into list and deleting book from list.

I think the above description is enough to get you started.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Vineet: please don't provide direct answers--instead point people in the right direction. We try to encourage people to do their own thinking as much as possible.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
And welcome to the Ranch , Anita Odiete.
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
so i was thinking of it this way...having one class for books, another three classes for different types of books...for example; fiction, comic, trilogy e.t.c. but if this way is correct, where would i put the name class? and the arrays too?
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
also forgot to ask...what does it mean for a book to have cataloging details?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Same thing as a book having a title. Books have properties: title, author(s), and could potentially have other cataloging details (LC number, MARC record, etc.)
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
i don't understand. what does LC number and MARC record mean?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It doesn't matter what they stand for--they are arbitrary properties a book might have that would fit under the "cataloging details" umbrella.

(LC = Library of Congress. MARC = MAchine-Readable Cataloging.)
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
okay
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
MARC stands for MAchine-Readable Cataloging.
LC numbers are used for cataloging in libraries.
Although they aren't related to java.


For a book to have cataloging details means a detail by which book can be grouped,for eg:books can be grouped by genre as fiction,comedy etc.

I would also recommend against making different classes for book according to genre.
Instead you can add an instance variable genre to class Book;

refer my earlier post for creating array of books;
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Vineet Kakati wrote:
I would also recommend against making different classes for book according to genre.
Instead you can add an instance variable genre to class Book;

refer my earlier post for creating array of books;


I heartily second this recommendation.
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
these are the two classes i have so far. this is just a sketch of how the program will look like. is there any other class i could add? plus, what class should contain the code for adding and deleting book?

public class Book {
String title;
String author;
int bookNo;
String genre;
}

public class BookStore {

public static void main(String[] args) {
// TODO Auto-generated method stub
Book [] myBook = new Book[5];
int x = 0;
myBook[0] = new Book();
myBook[1] = new Book();
myBook[2] = new Book();
myBook[3] = new Book();
myBook[4] = new Book();
myBook[0].title = "The White Queen";
myBook[1].title = "206 Bones";
myBook[2].title = "Sapphire";
myBook[3].title = "The Gladiator";
myBook[4].title = "Blondies";
myBook[0].author = "Gregory Phillips";
myBook[1].author = "Reichs Kathy";
myBook[2].author = "Price Katie";
myBook[3].author = "Scarrow Simon";
myBook[4].author = "Kinsella Sophie";
myBook[0].bookNo = 10001;
myBook[1].bookNo = 10002;
myBook[2].bookNo = 10003;
myBook[3].bookNo = 10004;
myBook[4].bookNo = 10005;
myBook[0].genre = "Fiction";
myBook[1].genre = "Trilogy";
myBook[2].genre = "Comic";
myBook[3].genre = "Fiction";
myBook[4].genre = "Comic";

while (x<5) {
System.out.print(myBook[x].title);
System.out.print(" by ");
System.out.print(myBook[x].author);
System.out.print(" with book number ");
System.out.print(myBook[x].bookNo);
System.out.print(" and of genre ");
System.out.println(myBook[x].genre);
x = x + 1;
}

}
}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags.

Which class do you think it should belong to? Think about the relationship between the two classes you've created: to what would you add a book?
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
the first class is the book class to store the data types of the books. the second class is the book store class which contains the main class and creates an arraylist and stores books in it.
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
you have put almost your entire code in main() which is not recommended.
main() should ideally contain only function calls to functions which actually do the tasks.

As for insertion,deletion etc.,I believe I suggested what I think is an efficient solution to your problem in my first post.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Vineet Kakati wrote:you have put almost your entire code in main() which is not recommended.
main() should ideally contain only function calls to functions which actually do the tasks. . . .
Agree you should not have lots in the main method. I prefer to see a single statement in main methods!
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
oh. ok.
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

To shorten your main, I believe constructors can take different parameter and you might want to take advantage of toString that Object has. I hope that helps. Have fun!!


star chaser..
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
On creating a program to define employee roles in an organization such as a hospital, factory or university and assigning individuals to those roles, how would i go about doing this? The program should output the names of people who should attend certain types of meeting.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What makes you think it's substantially different than the previously-asked and answered book question?
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
So far, i've made these three classes. I want to create the main class last after all my other codes are correct. I was wondering where exactly i am supposed to put the 'to string' and 'add book' method and is there any need to have a 'displaybook' method in the book class? Plus, how do i do the 'remove book' method? Sorry if i'm asking too many questions.

BOOK CLASS:
public class Book {
private String title = null;
private String author = null;
private String edition = null;
private String pubName = null;
private Date pubDate = null;
private int isbnNo = 0;
private String genre = null;
private int copyrightYr = 0;

public Book(){
}

public Book (String title, String author, String edition, String pubName, Date pubDate,
int isbnNo, String genre, int copyrightYr){
super();
this.title = title;
this.author = author;
this.edition = edition;
this.pubName = pubName;
this.pubDate = pubDate;
this.isbnNo = isbnNo;
this.genre = genre;
this.copyrightYr = copyrightYr;

}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getEdition() {
return edition;
}

public void setEdition(String edition) {
this.edition = edition;
}

public String getPubName() {
return pubName;
}

public void setPubName(String pubName) {
this.pubName = pubName;
}

public Date getPubDate() {
return pubDate;
}

public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}

public int getIsbnNo() {
return isbnNo;
}

public void setIsbnNo(int isbnNo) {
this.isbnNo = isbnNo;
}

public String getGenre() {
return genre;
}

public void setGenre(String genre) {
this.genre = genre;
}

public int getCopyrightYr() {
return copyrightYr;
}

public void setCopyrightYr(int copyrightYr) {
this.copyrightYr = copyrightYr;
}

public void displayBook(){
System.out.println("Title: " + this.title);
System.out.println("Author: " + this.title);
System.out.println(this.title + "edition");
System.out.println("Published By " + this.pubName + " in " + this.pubDate);
System.out.println("ISBN NO " + this.isbnNo);
System.out.println("Genre: " + this.genre);
System.out.println("Copyright Year: " + this.copyrightYr);

}

@Override
public String toString() {
return this.title + " " + this.author + ", " + this.edition + ", " + this.pubName + ", " + this.pubDate + ", " + this.isbnNo + ", " + this.genre + ", " + this.copyrightYr + " ";
}

}

BOOKSTORE CLASS
public class BookStore implements IBookStore {
public List<Book> books = null;

public BookStore(){
super();
this.books = new ArrayList<Book>();
}

public void addBook (String title, String author, String edition, String pubName, Date pubDate, int isbnNo, String genre, int copyrightYr){
this.books.add(new Book (title, author, edition, pubName, pubDate, isbnNo, genre, copyrightYr));
}

public List<Book> getBooks(){
return this.books;
}

}

IBOOKSTORE CLASS
public interface IBookStore {

public void addBook (String title, String author, String edition, String pubName, Date pubDate,
int isbnNo, String genre, int copyrightYr);

}
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
how do you enter dates in java? i used the import java.util.Date but i don't know the date format to use
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags as asked previously.
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
So far, i've made these three classes. I want to create the main class last after all my other codes are correct. I was wondering where exactly i am supposed to put the 'to string' and 'add book' method and is there any need to have a 'displaybook' method in the book class? Plus, how do i do the 'remove book' method? Sorry if i'm asking too many questions.

Book.java
[code]
import java.util.Date;
public class Book {

private String title = null;
private String author = null;
private String edition = null;
private String pubName = null;
private Date pubDate = null;
private int isbnNo = 0;
private String genre = null;
private int copyrightYr = 0;

public Book(){
}

public Book (String title, String author, String edition, String pubName, Date pubDate,
int isbnNo, String genre, int copyrightYr){
super();
this.title = title;
this.author = author;
this.edition = edition;
this.pubName = pubName;
this.pubDate = pubDate;
this.isbnNo = isbnNo;
this.genre = genre;
this.copyrightYr = copyrightYr;


}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public String getPubName() {
return pubName;
}
public void setPubName(String pubName) {
this.pubName = pubName;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public int getIsbnNo() {
return isbnNo;
}
public void setIsbnNo(int isbnNo) {
this.isbnNo = isbnNo;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public int getCopyrightYr() {
return copyrightYr;
}
public void setCopyrightYr(int copyrightYr) {
this.copyrightYr = copyrightYr;
}

public void displayBook(){
System.out.println("Title: " + this.title);
System.out.println("Author: " + this.author);
System.out.println(this.edition + " edition");
System.out.println("Published By " + this.pubName + " in " + this.pubDate);
System.out.println("ISBN NO " + this.isbnNo);
System.out.println("Genre: " + this.genre);
System.out.println("Copyright Year: " + this.copyrightYr);

}

@Override
public String toString() {
return this.title + " " + this.author + ", " + this.edition + ", " + this.pubName + ", " + this.pubDate + ", " + this.isbnNo + ", " + this.genre + ", " + this.copyrightYr + " ";
}
}
[/code]

BookStore.java
[code]
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BookStore implements IBookStore {

public List<Book> books = null;

public BookStore(){
super();
this.books = new ArrayList<Book>();
}
@Override
public void addBook (String title, String author, String edition, String pubName, Date pubDate, int isbnNo, String genre, int copyrightYr){
this.books.add(new Book (title, author, edition, pubName, pubDate, isbnNo, genre, copyrightYr));
}

public List<Book> getBooks(){
return this.books;
}
}

[/code]

IBookStore.java
[code]
import java.util.Date;
public interface IBookStore {

public void addBook (String title, String author, String edition, String pubName, Date pubDate,
int isbnNo, String genre, int copyrightYr);


}

[/code]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Hopefully you can see that didn't work ;)
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
i don't know how to do it
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Uncheck the "disable BB code" checkbox.
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
Book.java


BookStore.java


IBookStore.java
Martha Franklyn
Greenhorn

Joined: May 04, 2010
Posts: 18
So far, i've made these three classes. I want to create the main class last after all my other codes are correct. I was wondering where exactly i am supposed to put the 'to string' and 'add book' method and is there any need to have a 'displaybook' method in the book class? Plus, how do i do the 'remove book' method? Sorry if i'm asking too many questions.
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
Anita Odiete wrote:I was wondering where exactly i am supposed to put the 'to string' ... method
What is wrong with its current location?

The CodeTags would be much more helpful if you also did some indentation on your code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating classes
 
Similar Threads
compilation error by prompt
Unable to compile class for JSP
Tomcat NoClassDefFound error
ActionForm class
Use of Helper classes