The new class should look something like this?
public class Author { String authors; public void Authorss(){ } }
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
The new class should look something like this?
public class Author { String authors; public void Authorss(){ } }
Something like that. I see your confidence in writing a basic class structure is low, because you don't understand them, and you probably haven't done much practicing. More practicing will improve your understanding and your confidence.
If Authorss() was meant to be a constructor, remember that constructors have the same name as the class and don't have a return type. For the field 'authors', I would rename that lastName and add other fields for firstName and middleName or middleInitial (you choose). Then, as I suggested earlier, a field to collect all books written by the author, maybe an ArrayList publishedBooks. Again, you choose the collection and the name.
Now you're starting to get into the OOP part of this design, and you're floundering a bit, maybe even backsliding into using parallel arrays. You can use an array instance variable as the collection in which each Author object's books will be stored. In the real world, you wouldn't know how big the array will someday have to be, so a real ArrayList would probably be the collection of choice, but in this project, let's say an array of 5 elements is enough. In that case, you'd declare it something like:
public class Author { Book[] books; String firstName; String lastName; // other instance variables // two-argument constructor that sets the author's name public Author( String firstName, String lastName ) { // set the author's name this.firstName = firstName; this.lastName = lastName; // initialize the collection of books books = new Book[5]; // continue the constructor as needed } // end constructor // other methods, getters, setters, etc. } // end class Author
Lukas (November 3rd, 2013)
You said, that in this project 5 elements is enough. But can you say me, how to create as much as I want to insert Array elements?
Of course. I speculated you had some confusion about or inability to use the actual ArrayList, so I steered away, but that would look like:
Note: The above construction will work in Java 7 because of its type inference but may require minor adjustments for earlier versions.public class Author { List<Book> books; String firstName; String lastName; // other instance variables // two-argument constructor that sets the author's name public Author( String firstName, String lastName ) { // set the author's name this.firstName = firstName; this.lastName = lastName; // initialize the collection of books books = new ArrayList<>(); // continue the constructor as needed } // end constructor // other methods, getters, setters, etc. } // end class Author
Lukas (November 3rd, 2013)
Except that Book doesn't need an array called books, at least I don't think so. How would that array be used? What purpose would it serve? Other than that, it looks fine.
I assume you'll add a getter and setter for the bookName, and it may grow to include other instance variables with their getters and setters, but that's a reasonable start.
Now good?
And how to use class Author and class Book in Class Authors2. When I write in class Authors2 this:
And how can I instead of authors change to firstName? When I change authors to firstName, the program don't understand what is this..System.out.println("Please write the author name: "); authors = scan.nextLine();
Here's an example of how it could look all put together. Notice that because all classes have been included in a single file called LibraryManager.java, only the top-level class with the main() method can be public. The other classes could be public if they were stored in their own files, but they wouldn't have to be. You need to make progress from here. I've mentioned getter and setter methods several times, but you've not added any to the skeleton classes you've constructed. I added one to the Author class so you could work from an example, and there are more for you to write.
Here's an example run:import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class LibraryManager { Scanner scan; // constructor and currently drives the program public LibraryManager() { scan = new Scanner( System.in ); } public static void main( String[] args ) { // create a LibraryManager instance for demo use LibraryManager libraryManager = new LibraryManager(); // get the author's name and . . . String authorsName = libraryManager.getAuthorsName(); // . . . create an author instance Author author = new Author( authorsName, "Blank" ); // show how to use the Author's getter, getAuthorsWholeName System.out.println( "The Author's name is: " + author.getAuthorsWholeName() ); } // end method main() // method getAuthorsName() asks the user for the author's name and // returns the user's input. no error checking is done. private String getAuthorsName() { System.out.println("Please write the author's name: "); String authors = scan.nextLine(); return authors; } // end method getAuthorsName() } // end class LibraryManager class Author { List<Book> books; String firstName; String lastName; // other instance variables // two-argument constructor that sets the author's name public Author( String firstName, String lastName ) { // set the author's name this.firstName = firstName; this.lastName = lastName; // initialize the collection of books books = new ArrayList<>(); // continue the constructor as needed } // end constructor // other methods, getters, setters, etc. public String getAuthorsWholeName() { return firstName + " " + lastName; } // end method getAuthorsWholeName() } // end class Author class Book { String bookName; // constructor that specifies the book's name public Book(String bookName) { this.bookName = bookName; } } // end class Book
Please write the author's name:
Lukas
The Author's name is: Lukas Blank
Lukas (November 3rd, 2013)
Thank you too much
I try to do something:
But all time I don't understand one thing. I write how many books I want to insert, and when I'm writing the books, my result are the last book or error...import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class LibraryManager { Scanner scan; // constructor and currently drives the program public LibraryManager() { scan = new Scanner( System.in ); } public static void main( String[] args) { // create a LibraryManager instance for demo use LibraryManager libraryManager = new LibraryManager(); // get the author's name and . . . String authorsName = libraryManager.getAuthorsName(); String authorsSurname = libraryManager.getAuthorsSurname(); String getAuthorshowbooks = libraryManager.getAuthorshowbooks(); // . . . create an author instance Author author = new Author( authorsName, authorsSurname ); // show how to use the Author's getter, getAuthorsWholeName System.out.println( "The Author's name and surname is: " + author.getAuthorsWholeName() ); } // end method main() // method getAuthorsName() asks the user for the author's name and // returns the user's input. no error checking is done. private String getAuthorsName() { System.out.println("Please write the author's name: "); String authors = scan.nextLine(); return authors; } // end method getAuthorsName() private String getAuthorsSurname() { System.out.println("Please write the author's surname: "); String authors = scan.nextLine(); return authors; } // end method getAuthorsName() private String getAuthorshowbooks() { System.out.println("Please write how many author's books you want to insert: "); String authors = scan.nextLine(); return authors; } } // end class LibraryManager class Author { List<Book> books; String firstName; String lastName; // other instance variables // two-argument constructor that sets the author's name public Author( String firstName, String lastName ) { // set the author's name this.firstName = firstName; this.lastName = lastName; // initialize the collection of books books = new ArrayList<>(); // continue the constructor as needed } // end constructor // other methods, getters, setters, etc. public String getAuthorsWholeName() { return firstName + " " + lastName; } // end method getAuthorsWholeName() } // end class Author class Book { String bookName; // constructor that specifies the book's name public Book(String bookName) { this.bookName = bookName; } } // end class Book
But now, I don't understand at all how to create this...
You've made some progress, but if you want to get the user's desired NUMBER of something, then you'll need to either scan for a NUMBER or parse the scanned String into a NUMBER. This is basic stuff you should have some concept of, even if you don't know how exactly to do it.
I don't know.. What can I do more..
I don't know.. What can I do more.. I'm confused in my program.
In the previous program I was able to write those books .. But the program give me the last book.. And now I do not know .. Somehow differently here..
GregBrannon help... I do not know how to convert the scanned line in number... Write me that code if you can... Then I will understand what's going on.