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

I need help writing a program that uses linked list hash set and treemaps 110% S

ID: 3664390 • Letter: I

Question

I need help writing a program that uses linked list hash set and treemaps


110% Search in Document Home Layout Document Elements Tables Charts SmartArt Review Font Styles Insert Calibri (Body) aBbCcDdEeAaBbCcDdEe AaBbCcD AaBbCcDd Aa Bb( AaBbCcDdE AoBbCcDdEG AaBbCcDdE AaBbCcDdEe Normal No Spacing Heading 1 Heading 2 Subtitle ubtle E Text Box Shape Picture Themes 2l 3l The Assignment The colonists for our Mars mission will need to establish three camps once they arrive. The members of each camp were decided upon before they launched but it was understood that they could be changed as requirements dictate. Each camp will have a number of colonists assigned to it to form a camp group. This is only one program. Each Part will require you to prompt the user to continue with the part to execute. Each part will only be executed once. Your program will Part I Before launch, each camp group provided a list of provisions that would be needed for the Colony and their camp. The lists were created separately by each camp group. Each list contains a list of item:s needed and a count of how many of each item would be needed. The lists needed to be combined to gather the items needed for the supply ship to transport to Mars. 1. Read in a single file of colonists and create a linked list for each camp 2. Each linked list will contain only the members designated for that camp 3. Output the list of members for each camp Part II The probes that were sent to Mars earlier detected a strange bacterial life form. The soil samples were not conclusive in determining the rate that these bacteria reproduce. The colonists need some way to determine that rate. The possibilities are.exponential or additive. You will need to be able to provide models for each of these given a single pair of values that came from the probe 4. A second file will be input that changes the membership of the camps. (changes must be made within the linked list) 5. Output the list of members for each camp after the updates Part III Caveats and Notes 1) 6. Once you have completed 1-5 you will copy all of the linked lists into a single You MAY NOT use any data structures other than those described in this assignment. (three linked lists, one array, one bashMap, and one treeSet) Single objects are not considered a data structure for the purpose of this requirement. Parameters that are used in methods are not considered separate data structures either. The list of supply items will likely have duplicate entries. You will need to keep track of the total count of entries There are three camps; Alpha, Beta, and Gamma. When a member is added to a camp list, that member must also be removed from their current camp list. DO NOT create your own sorting algorithm or code DO NOT read any of the files more than one time. DO NOT output any files You will be graded on proper use of the methods of the Linked List Class, the Set Class, and the Map Class. array 7. Sort the array by ID number 8. Output the array Part IV 2) 9. Read three lists of supply items and store the lists in a bashHaR 18. Use anteratera Key set to output the hashMaR contents 3) 4) Part V 5) 6) 7) 8) 11, Transfer the hashMaR contents to a ce set (If the items are duplicated, the count must be increased for that item so that the total number of items is accounted for.) 12. Use an iterator to output the tceeSet contents Part VI 9) I will provide output formats later. In version 1.1 10) The hashMap Key will be the item and the value will be the item count 11) The treeSet will be of objects that have the item and count as their data values. 13. Prompt the user for a double and an int value X and Y 14. Recursively calculate the exponential value of X AY 15. Recursively calculate the additive value of X,Y where a. X is doubled the second time b. X is tripled the third time c. X is quadrupled the fourth time d. And so on for X e. Y 1imits how many times to add the X component 16. Output the X, Y and resultant value for 14 and 15 Print Layout View Sec 1 Pages: 1 of 4 Words: 401892 110%

Explanation / Answer

This is a Java Program to implement a Singly Linked List. A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence. In a singly linked list each node has only one link which points to the next node in the list.

/*
* Java Program to Implement Singly Linked List
*/

import java.util.Scanner;

/* Class Node */
class Node
{
protected int data;
protected Node link;

/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}

/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size ;

/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void insertAtStart(int val)
{
Node nptr = new Node(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void insertAtEnd(int val)
{
Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ "->");
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
}
}

/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{   
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test ");
char ch;
/* Perform list operations */
do
{
System.out.println(" Singly Linked List Operations ");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );   
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );   
break;   
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position ");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position ");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;   
case 6 :
System.out.println("Size = "+ list.getSize() +" ");
break;   
default :
System.out.println("Wrong Entry ");
break;   
}
/* Display List */
list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');   
}
}

Output:
Singly Linked List Test


Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = true

Singly Linked List = empty

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
5

Singly Linked List = 5

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
7

Singly Linked List = 7->5

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
4

Singly Linked List = 7->5->4

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
2

Singly Linked List = 7->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
9

Singly Linked List = 9->7->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
3
Enter position
3

Singly Linked List = 9->7->3->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
2
Enter position
2

Singly Linked List = 9->2->7->3->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
6
Size = 7


Singly Linked List = 9->2->7->3->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
4

Singly Linked List = 9->2->7->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2

Singly Linked List = 9->7->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Singly Linked List = 7->5->4->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
3

Singly Linked List = 7->5->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Singly Linked List = 5->2

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2

Singly Linked List = 5

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1

Singly Linked List = empty

Do you want to continue (Type y or n)

y

Singly Linked List Operations

1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = true

Singly Linked List = empty

Do you want to continue (Type y or n)

n

Store linked list in HashMap:

import java.util.ArrayList;

import java.util.Comparator;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.TreeMap;

import com.hmkcode.vo.Person;

public class MapApp

{

    public static void main( String[] args )

    {

        // ( 1 ) Map

        // A. Initiate

        Map<String,Person> hashMap = new HashMap<String,Person>();

        Map<String,Person> treeMap = new TreeMap<String,Person>();

        // B. Populate

        int k = 0 ;

        for(Person person:getPersons()){

            hashMap.put(""+k,person);

            treeMap.put(""+k++,person);

        }

        // --> print

        System.out.println("--------- Print All -------------");     

        System.out.println("HashMap: "+hashMap);

        System.out.println("TreeMap: "+treeMap);

        // C. Iterate

        // --> print

        System.out.println("--------- Print Iterate by get(key) -------------");    

        for(String key:treeMap.keySet()){

            System.out.println("treeMap: [key: "+key+" , value: "+treeMap.get(key));

        }

        // --> print

        System.out.println("--------- Print Iterate by Entry -------------");

        for(Entry<String, Person> entry:treeMap.entrySet()){

            System.out.println("treeMap: [key: "+entry.getKey()+" , value: "+entry.getValue());

        }  

        // D. Sort by value

        TreeMap<String,Person> sorted_map = new TreeMap<String,Person>(new ValueComparator());

        sorted_map.putAll(hashMap);

        // --> print

        System.out.println("--------- Print Sorted Map by Value -------------");      

        System.out.println("Sorted HashMap: "+sorted_map);

        // E. Convert Map to List

        List<Person> persons = new ArrayList<Person>(sorted_map.values());

        // --> print

        System.out.println("--------- Print List<Person> -------------");

        System.out.println("List<Person>: "+persons);

    }

    private static Person[] getPersons(){

        Person[] persons = new Person[5];

        persons[0] = new Person("Brit", 29);

        persons[1] = new Person("John", 32);

        persons[2] = new Person("Jack", 27);

        persons[3] = new Person("Jenifer", 24);

        persons[4] = new Person("Brit", 37);

        return persons;

    }

}

class ValueComparator implements Comparator {

      Map map;

      public ValueComparator(){

      }

      public int compare(Object keyA, Object keyB){

        return ((String) keyA).compareTo((String) keyB);

      }

}

ATreeSet is a set implementation which provides total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time.

A TreeSet is typically used when we want to keep the elements sorted all times. A TreeSet is a NavigableSet implementation based on a TreeMap. A TreeMap has Red-Black tree implemented which insures log(n) time cost for the basic operations (add, remove and contains).

Below is the example to compare salary of employees:


import java.util.Iterator;

import java.util.Set;

import java.util.TreeSet;


public class JavaTreeSetExample {

  
public static void main(String[] args) {
      
      
//Putting Integers in sorted order
      
      
Set<Integer> integerSet = new TreeSet<Integer>();
      
integerSet.add(new Integer(17));
      
integerSet.add(new Integer(1));
      
integerSet.add(new Integer(4));
      
integerSet.add(new Integer(9));
      
      
System.out.println(integerSet.toString());
      
      
//Putting Custom Objects in Sorted Order
      
Set<User> userSet = new TreeSet<User>();  
      
populateUser(userSet);
      
      
System.out.println("** Users based on first name **");
      
System.out.println(userSet.toString());
      
      
//Iterating over TreeSet using for loop
      
System.out.println("** Iterating using for loop **");
      
for(User user : userSet){
          
System.out.println(user.getFirstName());
      
}
      
      
      
//Iterating over TreeSet using Iterator
      
System.out.println("** Iterating using Iterator **");
      
Iterator<User> iterator = userSet.iterator();
      
while(iterator.hasNext()){
          
System.out.println(iterator.next());
       }
      
      
Set<User> userSetBasedOnSalary = new TreeSet<User>(new UserSalaryComparator());
      
populateUser(userSetBasedOnSalary);
      
System.out.println("** Users based on salary **");
      
System.out.println(userSetBasedOnSalary.toString());
      
  
}

  
private static void populateUser(Set<User> userSet) {
      
userSet.add(new User("Anirudh","Bhatnagar",100));
      
userSet.add(new User("Jack","Daniel",150));
      
userSet.add(new User("Kate","Xyz",120));
      
userSet.add(new User("Bosco","Ceasor",140));
  
}

}

User.java:
public class User implements Comparable<User>
{

  
private String firstName;
  
private String lastName;
  
private int salary;

  
public User(String firstName, String lastName, int salary) {
      
super();
      
this.firstName = firstName;
      
this.lastName = lastName;
      
this.salary = salary;
  
}

  
public String getFirstName() {
      
return firstName;
  
}

  
public void setFirstName(String firstName) {
      
this.firstName = firstName;
  
}

  
public String getLastName() {
      
return lastName;
  
}

  
public void setLastName(String lastName) {
      
this.lastName = lastName;
  
}

  
public int getSalary() {
      
return salary;
  
}

  
public void setSalary(int salary) {
      
this.salary = salary;
  
}

  
@Override
  
public String toString() {
      
return firstName + " " + lastName + " " + salary;
  
}

  
@Override
  
public int compareTo(User o) {
      
return this.firstName.compareTo(o.firstName);
   }

}

UserSalarayComparator.java:

import java.util.Comparator;


public class UserSalaryComparator implements Comparator<User> {

  
// This compares employees based on salaries
  
@Override
  
public int compare(User o1, User o2) {
      
if (o1.getSalary() >= o2.getSalary()) {
          
return 1;
      
}
else
{
          
return -1;
      
}
  
}


}