Library PROJECT JAVA : Object not found

java

#1

On row 29 it keeps giving me an error saying:

Library.java:29: error: cannot find symbol
getFinishedBooks.myLibrary(myBooks);
^
symbol: variable getFinishedBooks
location: class Library
1 error

CODE BELOW

import java.util.HashMap;

public class Library {
public Library() {
}

public void getFinishedBooks() {
HashMap library = new HashMap();
if (library.size() < 1) {
System.out.println("Your HashMap is empty");
} else {
for(String book : library.keySet()) {
if(library.containsKey(true)) {
System.out.println(library.get(book));
}
}
}
}

public static void main (String[] args){
HashMap myBooks = new HashMap();
myBooks.put("Road Down The Funnel", true);
myBooks.put("Rat: A Biology", false);
myBooks.put("Timein", true);
myBooks.put("3D Food Printing", true);

Library myLibrary = new Library();
getFinishedBooks.myLibrary(myBooks);

}
}

Any help is greatly appreciated. I can't figure out why it's not accepting it..


#2

The signature of the method getFinishedBooks() implies that it would not return anything, it does not take in any arguments and it is not static(which means it can be called on an instance of the class)
Check out the example person class that I have written below.
Take note of the static methods and fields that are in the class and how they are called in the main method.

public class Person{
    
  //private fields of the class
  private String name;
  private final int age;
  
  //static variable to keep count of objects
  //created
  private static int personCount = 0;
  
  public Person(String myName, int myAge){
    name = myName;
    age = myAge;
    personCount++;
  }
  //This is an instance method
  //meaning it can be called using an instance of this class
  public void setName(String newName){
    name =  newName;
    System.out.println("This person's name has now been set to "+name);
  }
  
  //this a void method
  //does not return anything
  //this will be used  to print the person's name
  public void printName(){
    System.out.println("Name: "+name);
  }
  
  //this is static method to print the number of
  //of Person Objects created so far
  //to invoke(call) this method you would need to 
  //use the class it self to call it
  public static void getPersonCount(){
      String add = "been created.";
      System.out.println(personCount==1? personCount+" person has "+add:personCount+" persons have "+add);
  }
  public static void main(String[] args) {
    //create a person object
    Person me = new Person("Code Kidda", 18);
    
    //using the instance of the class to 
    //invoke the instance methods
    me.printName();
    me.setName("Io Calypso");
    me.printName();
    
    //using the class to call the static method
    Person.getPersonCount();
    Person another = new Person("Script Kiddie", 45);
    Person.getPersonCount();
  }
}

Now to your code

public class Library {
  //There is no need to write this constructor here because
  //the compiler will automatically create a default constructor with no argument
  //for your
  //class
  public Library() {}
}

The JVM will create a default constructor automatically if there is no constructor given.Therefore you can just create an instance of the class with this statement as you wrote in your main

Library myLibrary = new Library();

Design proposal

instead of creating the Map to store your books in he method(technically bound to that instance method) why don't you create a private attribute for the class of type HashMap<String, Boolean>
Then let the class constructor take in one argument(i.e a HashMap<String, Boolean>) then we copy/save that passed HashMap to the class attribute's HashMap
Take a look below

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
 * 
 * @author study by Eric et all
 *A library class to store  personal library books
 */
public class Library{
	//private fields for the class
	private HashMap<String, Boolean> library;
  
	/**
	 * The constructor for the library.It takes in a HashMap which is a library of 
	 * books owned
	 * @param library the Map of all books owned
	 */
	public Library(HashMap<String, Boolean> library) {
                //copy the passed HashMap to the class' HashMap
		this.library = library;
	}

This way we can change the Library anytime we desire and also we can add more methods to manipulate the library


In your getFinishedBooks() method you designed it take no argument and also return nothing.In that method tou are iterating the HashMap the wrong way lets take a look at a possible corrrection

	/**
	 * This method will print all the keys in the HashMap that have
	 * a value of  true as read books
	 *
	 */
	public void getFinishedBooks() {
		//if there is nothing in the HashMap
		//alert an empty HashMap Message
		if (this.library.size() < 1) {
			System.out.println("Your HashMap is empty"); 
		} 
		else {
			
			//if the HashMap has entries
			//iterate it using a Map.Entry<String, Boolean> object
			for(Map.Entry<String, Boolean> book : library.entrySet()) {
				
				//if the current entry key has  a value of true
				//print it out as read
		        if(book.getValue()) {
		            System.out.println(book.getKey());
		          }
			}//end for loop
		}// end if
	}//end method

Here You can use a Map.Entry<String, Boolean> object to iterate the HashMap then check the value of each entry and print out the key if it is true.

you can also use an Iterator<Entry<String, Boolean>> object to iterate your HashMap. What this will do is it will create an iterator over the elements in the HasMap and then you can run through check for the entries with a value of true. Take a look below

	/**
	 * This method will print all the keys in the HashMap that have
	 * a value of  true as read books
	 *
	 */
	public void getFinishedBooks() {
		//if there is nothing in the HashMap
		//alert an empty HashMap Message
	    if (this.library.size() < 1) {
	        System.out.println("Your HashMap is empty"); 
	      } 
	      
	      //you can also use an Iterator<Entry<String, Boolean>> object to 
	      //iterate your HashMap
	      //when you use it you would have to create an iterator object over the
	      //elements
	      Iterator<Entry<String, Boolean>> it = library.entrySet().iterator();
	        //while thereare more elements in the HashMap
	  	  while(it.hasNext()){
	  	      //store the current entry in a Map.Entry<String,Boolean> variable
	  		  Map.Entry<String,Boolean>  s = it.next();
	  		  
	  		  //if the entry's value is true then print it out as read
	  		  if (s.getValue() ){
	  			  System.out.println(s.getKey());
	  		  }  
	  	}
	}//end method

Now you can write a program like this in your main method to test the code

	public static void main (String[] args){
	 
		HashMap<String, Boolean> myBooks = new HashMap<String, Boolean>();
		myBooks.put("Road Down The Funnel", true);
		myBooks.put("Rat: A Biology", false);
    	        myBooks.put("Timein", true);
    	        myBooks.put("3D Food Printing", true);
    
    	       //Here we initialize the constructor's argument with the created HashMap myBooks
    	       Library myLibrary = new Library(myBooks);
    
    	       //Now you can call the getFinishedBooks()
    	       //on the instance of the class myLibrary
    	       myLibrary.getFinishedBooks();
	 }//end Main

Hope this helped?


#3

Thank you so much four extensive and extremely constructive criticism! This has helped heaps in understanding, as opposed to just providing an answer. I see exactly why I went wrong here!!!!