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.

Page 2 of 2 FirstFirst 12
Results 26 to 40 of 40

Thread: What's wrong?

  1. #26
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    The new class should look something like this?
    public class Author {
    	String authors;
     
    	public void Authorss(){
     
    	}
     
    }

  2. #27
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong?

    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.

  3. #28
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Now I corrected:
    public class Book {
    	String books2;
     
    	public Book(){
    	}
    }

  4. #29
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    This is my ArrayList. It's good?
    		String[] shelf;
    		shelf = new String[1];
    		shelf[0] = books2;
            System.out.println("Book on shelf 0: "+ shelf[0]);

  5. #30
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong?

    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

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    Lukas (November 3rd, 2013)

  7. #31
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    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?

  8. #32
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong?

    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:
    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
    Note: The above construction will work in Java 7 because of its type inference but may require minor adjustments for earlier versions.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    Lukas (November 3rd, 2013)

  10. #33
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    And in Book class write:
    public class Book
    {
        Book[] books;
        String bookName;
     
        public Book(String bookName)
        {
            this.bookName = bookName;
        } 
    }
    Or change something or what?

  11. #34
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong?

    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.

  12. #35
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Now good?
    public class Book
    {
        String bookName;
     
        public Book(String bookName)
        {
            this.bookName = bookName;
        } 
    }
    And how to use class Author and class Book in Class Authors2. When I write in class Authors2 this:
    System.out.println("Please write the author name: ");
    authors = scan.nextLine();
    And how can I instead of authors change to firstName? When I change authors to firstName, the program don't understand what is this..

  13. #36
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong?

    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.
    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
    Here's an example run:
    Please write the author's name:
    Lukas
    The Author's name is: Lukas Blank

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    Lukas (November 3rd, 2013)

  15. #37
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    Thank you too much
    I try to do something:
    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 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...
    But now, I don't understand at all how to create this...

  16. #38
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong?

    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.

  17. #39
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    I don't know.. What can I do more..
    private String getAuthorshowbooks()
    	  {
          System.out.println("Write how many author's books you want to insert: ");
          how = scan.nextLine();
     
    	return how;
      } 
    	private String getAuthorsbooks()
    		{
    			System.out.println("Write the author's books: " + how);
    			String authors = scan.nextLine();
     
    			return authors;
    		}
    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..

  18. #40
    Member
    Join Date
    Oct 2013
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong?

    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.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. what wrong with this??
    By sephskie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 07:50 PM
  2. Not sure what is wrong
    By dremn2004 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 18th, 2011, 10:49 AM
  3. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM
  4. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM
  5. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM