• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating classes

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you start the list? There are at least two nouns in your description that leap out...
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[removed]
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we were asked to do it using java programming
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, but that's not important during the design process.
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yh. i was wondering if it's possible to use just one class for everything.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. so which is better? to create only one class or several classes?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch , Anita Odiete.
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also forgot to ask...what does it mean for a book to have cataloging details?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't understand. what does LC number and MARC record mean?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay
 
Vineet Kakati
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh. ok.
 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think it's substantially different than the previously-asked and answered book question?
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags as asked previously.
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hopefully you can see that didn't work ;)
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't know how to do it
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uncheck the "disable BB code" checkbox.
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Book.java


BookStore.java


IBookStore.java
 
Martha Franklyn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic