-1

This code was questioned in an exercise I had to solve. Although my code shows the right result (version one), I'm not sure if it is as good as the solution provided (version two) or if it is correct at all. The task is to write a class which is representing a book and methods to page forward and backward using self-references.

public class Book {

    private int page;
    private Book nextPage;

    public Book(int page) {
        setPage(page);
        setNextPage(null);
    }

//Getter and Setter

    public void setPage(int page){
        this.page = page;   
    }

    public int getPage() {
        return this.page;
    }

    public void setNextPage(Book nextPage) {
        this.nextPage = nextPage;
    }

    public Book getNextPage() {
        return nextPage;
    }

/*The following methods are getting two int values and should page backward, showing each page number. */

Version one:

    public static void pageBackward1(int currentPage, int goalPage) {
        Book page = new Book(currentPage);
        System.out.print("Page " + page.getPage() + " ");
        while(goalPage != page.getPage()) {
            currentPage--;
            page.nextPage = new Book(currentPage);
            page = page.nextPage;
            System.out.print("Page " + page.getPage() + " ");
        }
    }

Version two:

    public static void pageBackward2(int currentPage, int goalPage) {
        Book previousPage = null;
        for(int i = currentPage; i <= goalPage; i++) {
            Book page = new Book(i);
            page.setNextPage(previousPage);
            previousPage = page;
        }
        Book page = previousPage;
        while(page != null) {
            System.out.print("Page " + page.getPage() + " ");
            page = page.getNextPage();
        }
        System.out.println();
     }
}

The following demo-class is simple and shows the execution of the methods:

public class BookDemo {

    public static void main(String[] args) {
        Book.pageBackward1(500, 455);
        System.out.println();
        Book.pageBackward2(500, 455);
    }
} 
Patrick Na
  • 13
  • 3
  • 7
    did you run it and check? – Stultuske Jan 27 '16 at 11:58
  • 1
    What's the semantic meaning (also in the "provided solution"!) of calling Book a class representing a page? – Matteo Di Napoli Jan 27 '16 at 12:03
  • @Stultuske I ran and checked the program. The output looks for both versions like this: page 500 page 499 page 498 ... page 455 – Patrick Na Jan 28 '16 at 00:16
  • @Matt The semantic of this is also for me a little strange. The class Book is defined by the page number, that's why the instance is called page or previousPage or nextPage. If I would define this exercise I would do it different, but I just copied it. – Patrick Na Jan 28 '16 at 00:21

1 Answers1

0

The solutions, as you've said, are equivalent (literally! Upon the same input data they output the same data, despite through stdout instead of a return value).

I believe due to the supplied solution that the exercise's proposal was to pre-fill/pre-generate the Books before stepping through them using the getNextPage() getter. I believe you've reached the same conclusion since you did almost the same thing but in a different order, stepping through the instances once but as you create them (as opposed to, for example, stepping through all of the existing Books to arrive at the final one before creating the next).

In my opinion this exercise simply fails to teach much due to these wonky semantics and unclear goals, and it would be much more productive for students to start working straight out with more explicitly named and generic linked list data structures.

paolobueno
  • 1,678
  • 10
  • 9