Search code examples
javabluej

Object creating object


I'm a very new programmer-in-training, and this problem on my assignment has me absolutely stumped. We are currently working on creating objects with objects, and I'm completely lost. I have a MediaDemo class that's supposed to create an instance from the Book class. I felt I was getting close for a bit but the error keeps saying that it can't accept the variable type... Here's the code from both classes:

    public class Book 
{ 
    // The fields. 
    private String author; 
    private String title;
    private int pages;
    private String refNumber;
    private int borrowed;
    private boolean courseText;

/** 
 * Set the author and title fields when this object 
* is constructed. 
*/ 
    public Book( String bookAuthor, String bookTitle, int bookPages, boolean theCourseText) 
   { 
       author = bookAuthor; 
        title = bookTitle;
        pages = bookPages;
        refNumber = ("");
        courseText = theCourseText;
    }

public String printAuthor()
{
return author;
}

public String printTitle()
{
return title;
}

public void setRefNumber(String ref)
{
if(ref.length() == 3){
refNumber = ref;
}
else{
System.out.println("Please enter a String that's 3 characters long.");
}
}

public String printRef()
{
return refNumber;
}

public void borrow()
{
    borrowed += 1;
}
public int getBorrowed()
{
    return borrowed;
}

public void printDetails()
{
if(refNumber == ("")) {
System.out.println("Book Author: " + author + ". | Book Title: " + title + ". | Book Length: " + pages + " pages. refNumber: ZZZ | Borrowed Books: " + borrowed);
}
else {
System.out.println("Book Author: " + author + ". | Book Title: " + title + ". | Book Length: " + pages + " pages. | Number: " + refNumber + ". | Borrowed Books:     " + borrowed);
}
}
} 

Thanks for any help on this, I'll be glad as soon as I find a solution that prevents me from beating my head on my desk...

--UPDATED--

/**
 * Write a description of class MediaDemo here.
 * 
 * @author ---
 * @version 2-17-16
 */
public class MediaDemo
{
 private Book book;
 private String bookAuthor;
 private String bookTitle;
 private int bookPages;
 private boolean theCourseText;
 public MediaDemo (String author,String title,int pages,boolean courseBook)
 {
     bookAuthor = author;
     bookTitle = title;
     bookPages = pages;
     theCourseText = courseBook;
     createBook();
 }

 private void createBook()

{
  book = new Book(bookAuthor, bookTitle, bookPages, theCourseText);
}
}

Okay, this is the currect version of Book.


Solution

  • The statement book = new Book(); instantiates a new object book and when you say new Book(), it means calling to the Book constructor

    public Book( String bookAuthor, String bookTitle, int bookPages, boolean theCourseText) 
       { 
           author = bookAuthor; 
            title = bookTitle;
            pages = bookPages;
            refNumber = "(\"\")"; \\result : ("")
            courseText = theCourseText;
        }
    

    . In your code, you didn't supply the arguments needed to create a new instance of the object book. So you might try replacing :

    book = new Book();
    

    with

    book = new Book( "Scarlet", "I dunnu", 100, true); 
    

    of course with the corresponding data types for the parameters