2
\$\begingroup\$

It's from a Java-course I'm enrolled into.

The classes "Instructor" and "Book" are already provided.

You got to write the different constructors of "Instructor" as well as the methods "getMostRecentBookTitle" and overloaded "updateBook".

Here's are the final classes expanded with my additional code:

// ------- Class Instructor ----------------------------------------
public class Instructor {
    public long id;
    public String name;
    public String title;
    public String department;
    public Book[] books;

    public Instructor(long id, String name, String title, String department, Book[] books) {
        this(id, name, title, department);
        this.books = books;
    }

    public Instructor(long id, String name, String title, String department) {
        this(id, name, title);
        this.department = department;
    }

    public Instructor(long id, String name, String title) {
        this(id, name);
        this.title = title;
    }

    public Instructor(long id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getMostRecentBookTitle() {
        return books[books.length - 1].title;
    }

    public Book updateBook(int index, String title) {
        Book retBook = books[0];
        Book[] updatedBooks = new Book[books.length + 1];
        int i = 0;
        int j = 0;

        while (i < updatedBooks.length) {
            if (i == index) {
                updatedBooks[i] = new Book(title);
                retBook = books[i];
            } else {
                updatedBooks[i] = books[j];
            }

            i++;

            if (i != index) {
                j++;
            }
        }

        books = updatedBooks;

        return retBook;
    }

    public Book updateBook(int index, Book book) {
        for (int i = 0; i < books.length; i++) {
            if (i == index) {
                Book oldBook = books[i];
                books[i] = book;
                return oldBook;
            }
        }

        return new Book("");
    }

    public static void main(String[] args) {
        Book book1 = new Book("Wind Energy Design");
        Book book2 = new Book("Aerodynamics");
        Book book3 = new Book("Effective Java");

        Instructor instructor =
            new Instructor( 248, 
                            "Walter", "Lecturer", "Mechanical Engineering", 
                            new Book[]{book1, book2, book3});
        System.out.println(instructor.getMostRecentBookTitle());
        System.out.println("Title of old book: " + instructor.updateBook(1, "Vector mechanics").getTitle());

        Book book4 = new Book("Thermodynamics");
        System.out.println("Title of old book: " + instructor.updateBook(1, book4).getTitle());
    }
}

// ------- Class Book ----------------------------------------
public class Book {
    public String title;

    public Book(String title) {
        this.title = title;
    }

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

    public String getTitle() {
        return title;
    }
}

It works well and it has passed the automated tests.

But I'm unsatisfied with the method "updateBook(int index, String title)". That's just too much code for to accomplish such a simple task. In my opinion.

In JavaScript they have these method "splice". I guess that won't work in Java because Array-sizes can't be changed after construction.

Nevertheless: Is there a better way to write that method in Java?

Other hints, comments and recommendations concerning my implementation welcomed too.

\$\endgroup\$
3
\$\begingroup\$

In java, you can just set the value at the given index using that index. Thus, starting out with the simpler update book (which uses the book object), this becomes:

public Book updateBook(int index, Book book) {
    Book oldBook = books[index];
    books[index] = book;
    return oldBook;
}

Alike in the ... string title ... method, but now, you can even use the method from before:

public Book updateBook(int index, String title) {
    return updateBook(index, new Book(title));
}

Disclaimer: this has not been typed in an IDE or compiled, but just done on the fly, thus it might include bugs. But the concept should be clear.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.