0

I've been assigned the task of developing a simple Java program based on following:

Write an algorithm to search for reserved book copies, from a list (array) of books using Linear Search, and then store them at the beginning of a newly created array, using an insert algorithm.

I have written the algorithm, but I'm struggling to demonstrate it due to a problem with the last line of the following code:

package javaapplicationadt2;

public class Books
{

    public String ISBN;               // class properties
    public String title;
    public int copies;
    boolean reserved = true;

    public static void main(String[] args)
    {

        Books[] Book1 = new Books[1];
        Books[] Book2 = new Books[1];

        Book1[0].ISBN = "ISBN-00001";
        Book1[0].title = "Algorithms that torture minds";
        Book1[0].copies = 2;
        Book1[0].reserved = true;

        Book2[0].ISBN = "ISBN-00002";
        Book2[0].title = "Lecture Slides that defied practical Java Programming !";
        Book2[0].copies = 1;
        Book2[0].reserved = false;

        for (int i = 0; i < Book1.length; i++)
            if (Book1[i].reserved = true)
                for (int j = Book2.length - 1; j > 0; j++)
                    Book2[j] = Book2[j - 1];
     *Book2[0] = Book1[i].title;*   **(Incompatible types:
    Books cannot be converted to a String)**

    }
}
Emanuel
  • 37
  • 8

1 Answers1

1

There are lot's of errors in your logic and code conventions

Java uses CamelCase as a practice for writing names of methods, variables, classes, packages and constants. Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Interfaces name should also be capitalized just like class names. Take a look at Java Code conventions

Book[] bookArray = new Book[1];

Back to your code. When you do

Book[] book1 = new Book[1]; //Create an Array of Books -> all elements are NULL
book1[0].ISBN = "ISBN-00001"; // book1[0] is NULL so a NullPointException will be throw

You must to initialize your array and/or assign an Object to it

Book[] bookArray = new Book[1];
Book book = new Book();
Book[0] = book; //Now you have an Object Book at Book[0] and you can set the properties    
bookArray[0].ISBN = "ISBN-00001";

For and If...if you don't define a block, only the next line will be executed. i.e.

 if(true == bookArray[0].reserved)
     System.out.println("1");//This will be executed only if condition is true
     System.out.println("2"); //This will be executed no matter what

If you want to execute code only if the condition is true you must:

if(true == bookArray[0].reserved){
     System.out.println("1");//This will be executed only if condition is true
     System.out.println("2"); //This will be executed only if condition is true
}
Graciano
  • 508
  • 4
  • 11
  • I tried both these as the last line of my code (written above) and it was showing syntax error.... I don't know what to do... Thanks for your help though. – Emanuel Mar 31 '18 at 13:46
  • So my code is exactly the same as originally posted, except the last line of the code *Book2[0] = Book1[i].title;* **(Incompatible types: Books cannot be converted to a String)** I replaced it with either Book2[0] = Book1[I] OR Book[0].title = Book[I].title and both present errors. – Emanuel Mar 31 '18 at 15:17
  • The other issue I am having is that when I run this code, the Console outputs answer from another program that I have created within the same package. So, I changed the package, but the output is still the same !! (I feel very stupid at this stage) – Emanuel Mar 31 '18 at 15:20
  • Thanks Graciano - Coming back to the Last Line of my Original Code, (a) If I write Book2[0].title = Book1[1].title; the error message is as follows "Exception in thread "main" java.lang.NullPointerException at javaapplicationadt2.Books.main(Books.java:15) C:\Users\Emanuel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)" – Emanuel Mar 31 '18 at 20:37
  • (b) If I write Book2[0] = Book1[1]; the error is Exception in thread "main" java.lang.NullPointerException at javaapplicationadt2.Books.main(Books.java:15) C:\Users\Emanuel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) – Emanuel Mar 31 '18 at 20:43
  • Just to add ... I have changed Boolean Reserved = true to public Boolean Reserved; – Emanuel Mar 31 '18 at 21:02
  • btw if (Book1[i].reserved = true) -> you're not comparing, you're always assigning true. To compare a boolean use == – Graciano Mar 31 '18 at 22:16