In: Computer Science
Okay so I already completed steps A - C listapi is now a library for project 4 the trouble I am having is how to do part D.
Use ListAPI as a library.
a) Create a Intellij project and name it “p4_2_your-id”.
b) Create a JAR file of “ListAPI”.
Download “ListAPI.zip” from Moodle and extract the NetBeans project folder “ListAPI” from the archive file.
In NetBeans, open “ListAPI”. Under the Project tab, right click on ListAPI and select Clean. Under the Project tab, right click on ListAPI and select Build. The created jar file is in “dist” folder.
Move the JAR file to the folder that contains your project folder. Navigate to the “ListAPI” NetBeans project folder and open it. Open the folder “dist”. Copy the file “ListAPI.jar” and paste it in the same folder that contains the “p5_2_your-id” project folder.
c) Attach the “ListAPI.jar” to your project.
Under the NetBeans Project tab, open the project created in step (a).
Right click on Libraries and select “Add JAR/Folder...”, and then navigate to “ListAPI.jar”. In the Add JAR/Folder dialog box, select “ListAPI.jar”, select Relative Path (which, if you have followed the preceding instructions, will be “../ListAPI.jar”) and click OK.
The classes in the JAR file are now available either by adding the following import statement to classes in your project
import listapi.*;
or by entering the full name of a class as in the following example.
listapi.TestGenericQueue tgq = new listapi.TestGenericQueue();
d) In your project “p4_2_your-id”, add code to enqueue 5 integer values in a listapi.GenericQueue, displaying the queue as each value is added. Then, dequeue each element in the queue, adding each value to a listapi.GenericStack, displaying the queue and stack as each value is moved from the stack to the queue. Finally, pop all values of the stack and add them to a listapi.MyArrayList, displaying the stack and the list as each value is moved from the stack to the list.
Solution:
The main idea behind the question is how to use the classes from this ListAPI..
At first, complete till this: "Attach the “ListAPI.jar” to your project."
In this step you will right-click on the project, then go in its properties ,.... if you follow the steps written, you will have a jar file ListAPI.jar reference in your project and you can see it by reloading the project. This is done then the jar file will be displayed along with the libraries defined in-built in your project.
Now comes the last part. How you will access the class from that library you just included...
The simplest way is to write this :
import listapi.*;
This is mentioned in above question. But, ideally, you should not use this approach because this needs to add all the class definitions from listapi.jar. Suppose that it contains 1000s of classes, then using ' * ' over here means import each and every class definition in this program, irrespective of whether it will be used or not. This increases the size of byte code and also it is not necessary.
So, instead of that you should use the following approach:
(1).
For example, as mentioned, you are supposed to use the class GenericQueue. In your main() method, write this :
GenericQueue gq1 = new GenericQueue();
So, this will give an error in which as you are using NetBeans, you will get a red underline below GenericQueue. Place your cursor in between this word, just move the cursor in this word, then press Alt + Enter.
It will give you the suggestions to correct the error. If you have inserted the Jar file and if it is successfully inserted, it will give the first suggestion as : Add import for import listapi.GenericQueue.
Click on that and the error is gone. Now, if you see the import section, NetBeans has automatically added the import listapi.GenericQueue; statement.
So, whenever you use some class for the first time, import it using this way. This helps in reducing the number of classes required to import and does the required task also.
(2).
There is 1 alternative as well.. Use all your classes you need from listapi.jar like GenericStack, GenericQueue and MyArrayList ... etc. Don't worry about the red underlines. Now,if you have used all the classes at least once, press Ctrl + Shift + i . This will display all the required class imports to be done in a dialog box. From here, you can easily import them.
Once they are imported in your class, you can code them as you require. So, you can access them to fulfill the task after importing.
Please comment if there is any query. Thank you. :)