Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: DONT USE java.util.linkedlist rather use POI object that is linked to POI.

ID: 3721981 • Letter: N

Question

Note: DONT USE java.util.linkedlist rather use POI object that is linked to POI.next (POI = POI.next)

Note: DONT USE arrayList

Objective: The goal of this assignment is to practice linked list.

Background: The national Security Agency (NSA) maintains databases of incidents, field reports, publicly available blogs, and news articles to track suspicious activities of persons of interest (PoI). By analyzing the data, intelligence analysts build connection profiles for the PoIs. Once connection profiles are built, each of the profile is saved in a file. NSA has hired you to develop a software that reads a connection profile and allows the analyst to make necessary modifications.

Assignment: The NSA will provide any profile connection in the form of a text file. Every three lines in the file is reserved for the record of a person. These three lines respectively represent a long integer ID of a person, the name of the person, and an integer ranging from 0 to 5 indicating the threat level posed by a person. A sample connection profile file is provided below, which gives a connection profile between two PoIs, Dan Carte and Osama Laden. The full connection is: Dan Carte -> Prio Notim -> Hons Nohish -> Diran Egrac -> Osama Laden.

Your task is to read the file content, construct a singly linked list to store the records in a sequence they appear in the file, and allow flexibility to the analysts so that she/he can perform the following operations.

Operation 1. Print the linked list content on the terminal (ID, name, and threat level of the people in a sequence they appear in the linked list)

Operation 2. Search a person in the linked list with ID

Operation 3. Insert a new person in a particular location index

Operation 4. Swap two people in two specific location indices

Operation 5. Remove a record containing a specific ID 389114 Dan Carte 5 399012 Prio Notim 0 685015 Hons Nohish 3 179318 Diran Egrac 2 284139 Osama Laden 5

Operation 6. Remove all records with a certain threat level

Operation 7. Write the content of the linked list in an output file that has the same format as the input file.

Operation 8. Quit program.

Your program must start by prompting the user to provide the filename of the input connection profile. Then the program should load the information in a linked list and prompt the user to perform one of the eight operations listed above. Operations 2 to 7 will require more prompting to the user for operation-specific information.

Other requirements:

• You must write three classes: POI, POIList, and AnalyzePOI.

o POI.java: The POI class must hold the variables for the ID (long), name (String), and threat level (int). All these variables must be private. POI must also have a variable of POI type that can be used to point to the next POI object. This variable should be public.

o POIList.java: This class must contain a data variable named head of POI type, which should contain the head address of the constructed linked list. Each of the operations from 1 to 7 must be implemented in a method of this class.

o AnalyzePOI.java: This is the only class that will contain a main method. This class will be responsible for prompting the user for the input filename, creating the linked list (a POIList object), and prompting for the seven listed operations. AnalyzePOI must call appropriate methods associated with the POIList object to perform operations 1 to 7. Prompts for operation specific-information must be implemented in the POIList class, not in AnalyzePOI.

• As stated earlier, your program must start by prompting the user to provide the filename of the input connection profile. You must handle the following exceptions. Do not quit the program if you have these exceptions, rather give an error message and prompt the user again for a correct input file.

o If the file does not exist

o If an expected ID in the file is not a long integer

o If threat level is not in integer

o If threat level is not an integer between 0 to 5 (threat level can be 0, or 5, or any integer between 0 and 5)

• Make sure that your program is not vulnerable to user’s incorrect input regarding the operations.

Deliverables: You must submit three Java files for this assignment (POI.java, POIList.java, and AnalyzePOI.java) using Blackboard. You have to demo your programs within one week after the due date. Your demo will be based on your last submission in the Blackboard. Your TA will instruct you with further details.

inputFile.txt:

389114

Dan Carte

5

399012

Prio Notim

0

685015

Hons Nohish

3

179318

Diran Egrac

2

284139

Osama Laden

5

Explanation / Answer

POI.java

//create Node
public class POI {
  
   private long ID;
   private String name;
   private int threatLevel;
   POI next;
   public POI(long Id,String name,int tl) {
       this.ID=Id;
       this.name=name;
       this.threatLevel=tl;
       this.next=null;
   }
   public long getID() {
       return ID;
   }
   public void setID(long iD) {
       ID = iD;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public POI getNext() {
       return next;
   }
   public void setNext(POI next) {
       this.next = next;
   }
   public int getThreatLevel() {
       return threatLevel;
   }
   public void setThreat_level(int threat_level) {
       this.threatLevel = threat_level;
   }
}

//create LinkedList

POIList.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;


public class POIList {
   POI head;
   int length;
   public POIList()
   {
       head = null;
       length=0;
   }
   public void print()
{
if (head == null)
{
System.out.print("empty ");
return;
}
POI node = head;
while (node != null)
{
   System.out.println(node.getID());
System.out.println(node.getName());
System.out.println(node.getThreatLevel());
node = node.getNext();
}
}
   public int getLength() {
       return length;
   }
   public void setLength(int length) {
       this.length = length;
   }
   public void insert(long Id,String name,int tl)
{
POI node = new POI( Id, name,tl);
length++ ;
if(head == null)
{
head = node;
}
else
{
node.setNext(head);
head = node;
}
}
   public void search(long ID){
       POI node = head;
   while (node != null)
   {
       if(node.getID()==ID){
           System.out.println("Found");
           System.out.println(node.getID());
   System.out.println(node.getName());
   System.out.println(node.getThreatLevel());
   break;
       }
   node = node.getNext();
   }
   }
   public void insertAtPosition(long Id,String name,int tl, int pos)
   {
   POI newNode = new POI( Id, name, tl);
   POI ptr = head;
   pos = pos - 1 ;
   for (int i = 1; i < length; i++)
   {
   if (i == pos)
   {
   POI temp = ptr.getNext() ;
   ptr.setNext(newNode);
   newNode.setNext(temp);
   break;
   }
   ptr = ptr.getNext();
   }
   length++ ;
   }
   public void removeByID(long ID)
   {
   POI node = head;
   int i=0;
   while (node != null)
   {
       if(node.getID()==ID && i==0){
           head = head.getNext();
   length--;
   break;
       }
       else if(node.getID()==ID){
           POI temp = node.getNext();
   temp = temp.getNext();
   node.setNext(temp);
   length --;
   break;
       }
   node = node.getNext();
   }
   }
   public void removeAllByTl(int tl)
   {
   POI node = head;
   int i=0;
   while (node != null)
   {
       if(node.getThreatLevel()==tl && i==0){
           head = head.getNext();
   length--;
       }
       else if(node.getThreatLevel()==tl){
           POI temp = node.getNext();
   temp = temp.getNext();
   node.setNext(temp);
   length --;
       }
   node = node.getNext();
   }

   }
   public void swap(int pos1,int pos2)
   {
   POI node = head;
   POI newnode1 = null;
   POI newnode2=null;
   for(int i=0;i<length;i++)
   {
       if(i==pos1){
           newnode1 = new POI(node.getID(),node.getName(),node.getThreatLevel());
       }
       if(i==pos2){
           newnode2 = new POI(node.getID(),node.getName(),node.getThreatLevel());
       }
   node = node.getNext();
   }
   insertAtPosition(newnode1.getID(), newnode1.getName(), newnode1.getThreatLevel(), pos2);
insertAtPosition(newnode2.getID(), newnode2.getName(), newnode2.getThreatLevel(), pos2);
   }
   public void writeToFile() throws FileNotFoundException{
         
           PrintWriter out = new PrintWriter(new File("test.out"));
           POI node =head;
           if (head == null)
   {
   return;
   }
   while (node != null)
   {
       out.println(node.getID());
   out.println(node.getName());
   out.println(node.getThreatLevel());
   node = node.getNext();
   }
          
   }
}

AnalysePOI.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class AnalysePOI {
public static void main(String args[]){
   Scanner scan = null;
   try {
       scan = new Scanner(new File("test.in"));
   } catch (FileNotFoundException e1) {
       System.out.println("File not Found");
   }
   /* Creating object of class POIList */
   POIList list = new POIList();
   while(scan.hasNextLine()){
       long ID = scan.nextLong();
       String name = scan.nextLine();
       int threatLevel = scan.nextInt();
       list.insert(ID, name, threatLevel);  
   }
   scan = new Scanner(System.in);
//List Operations
System.out.println(" Person Linked List Operations ");
System.out.println("1. Print the List");
System.out.println("2. Search a person");
System.out.println("3. Insert at position");
System.out.println("4. Swap records at 2 positions");
System.out.println("5. Remove a record with an ID");
System.out.println("6. Remove all records with a certain threatlevel");
System.out.println("7. Print List to a file");   
System.out.println("8. Quit");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
list.print();   
break;
case 2 :
System.out.println("Enter the ID of the person");
list.search( scan.nextInt() );   
break;   
case 3 :
System.out.println("Enter the ID,name & threatLevel of the person to insert");
long id = scan.nextInt() ;
String name = scan.nextLine();
int tl =scan.nextInt();
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getLength() )
System.out.println("Invalid position ");
else
list.insertAtPosition(id,name,tl, pos);
break;
case 4 :
System.out.println("Enter position one");
int p1 = scan.nextInt() ;
System.out.println("Enter position two");
int p2 = scan.nextInt() ;
if (p1 < 1 || p1 > list.getLength()||p2 < 1 || p2 > list.getLength() )
System.out.println("Invalid position ");
else
list.swap(p1,p2);
break;
case 5 :
   System.out.println("Enter the ID of the person");
list.removeByID( scan.nextInt() );
break;
case 6 :
   System.out.println("Enter the threat level of the person");
list.removeAllByTl( scan.nextInt() );
break;
case 7 :
try {
               list.writeToFile();
           } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               System.out.println("File not Found");
           }
break;   
case 8 :
   System.out.println("Enter the threat level of the person");
list.removeAllByTl( scan.nextInt() );
break;   
default :

system.exit();
}   

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote