Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Note that you do not need recursion for any of these methods. 1. public void add(Object o) - This method adds the Person object to

Note that you do not need recursion for any of these methods.

1. public void add(Object o) - This method adds the Person object to the end of the list. Use prev and curr pointers to walk to the end of the list and add it.

2. public void addFirst(Object o) - This method adds the Person object as the first object in the list.

3. public int indexOf(Object o) - This method walks the list using prev and curr pointers to find the index of the first occurrence of the Person object passed into the method. It returns -1 if the Person is not in the list. Remember to use the equals method to make the comparisons.

4. public int lastIndexOf(Object o) - This method walks the list using prev and curr pointers to find the index of the last occurrence of the Person object passed into the method. It returns -1 if the Person is not in the list. Remember to use the equals method to make the comparisons.

5. public boolean remove(Object o) - This method walks the list using prev and curr pointers to find and remove the first occurrence of the Person object passed into the method. It returns true if it removes a Person, false otherwise. Remember to use the equals method to make the comparisons.

public boolean removeLast() - This method walks the list using prev and curr points to remove the last item. It returns true unless the list is empty.

6. public boolean removeLast(Object o) - This method walks the list using prev and curr pointers to find and remove the last occurrence of the Person object passed into the method. It returns true if it removes a Person, false otherwise. Remember to use the equals method to make the comparisons.

7. public int removeAll(Object o) - This method walks the list using prev and curr pointers to find and remove all occurrences of objects matching the Person object passed into the method. It returns a count of removed objects, which could be zero if no objects match. Again, remember to use the equals method to make the comparisons.

8. public void set(int index, Object o) - This method walks the list to the indexed position and replaces the node with a new one that contains the Person passed into the method. This one is a little tricky because you must remove the node at the index and insert a new node there. Note, you can use a for loop to walk the list because you already know the index.

9. public void setAll(Object find, Object replacement) - This method walks the list and replaces each node with a person matching the find Person passed into the method with the replacement Person. Like above, this one is a little tricky because you walk the list removing and adding nodes. Use prev and curr pointers, not index positioning.

10. public Object[] toArray() - This method creates an array of Objects and then walks the list adding each Person object to the array. Note that it is the Person object, not the node it adds to the array. The array should be the same size as the list, and you can use a for loop to walk the list.

11. Test the toArray() method on the list using Arrays.toString(). For example, my list is named people, and the toArray() method returns an array that I assign to peeps. The results should look similar to the results you have seen above.

Object[] peeps = people.toArray();

System.out.println(Arrays.toString(peeps));

Edit the driver program to demonstrate each of the methods at least once. However, remember that you should test the methods under different situations to ensure they work. Consider all the possibilities, like manipulating first, last, interior, multiple nodes, etc., and edit the data file as needed. After each test, print the list with a loop. It should be ID number followed by the name (not that you will have several, one for each test case):

here is the Driver.java code that needs to be filled in with the 11 methods to their respective places using the code provided below

package driver;

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

import Person.Person; import list.RList;

public class Driver { public static void main(String[] args) throws FileNotFoundException { File file = new File("files/people.txt"); Scanner scnr = new Scanner(file); RList people = new RList(); while(scnr.hasNext()) { int ID = scnr.nextInt(); String name = scnr.next(); Person p = new Person(ID, name); people.add(people.size(), p); } scnr.close(); people.display(); Person p = new Person(46950, "Gilberto"); people.testComparison(p); }}

This is the Rlist.java code below

package list;

public class RList { private Node head; // points to first item private int k; // number of items in list

public RList() { k = 0; head = null; }

public boolean isEmpty() { return (k == 0); }

public int size() { return k; }

public void add(int index, Object o) throws ListIndexOutOfBoundsException { if(index >= 0 && index < k+1) { if(index == 0) { Node newNode = new Node(o, head); head = newNode; } else { Node prev = find(index-1); Node newNode = new Node(o, prev.next); prev.next = newNode; } k++; } else { throw new ListIndexOutOfBoundsException("List index out of bounds on add"); } } private Node find(int index) { Node curr = head; for(int skip = 0; skip < index; skip++) { curr = curr.next; } return curr; } public Object get(int index) throws ListIndexOutOfBoundsException { if(index >= 0 && index < k) { Node curr = find(index); Object item = curr.item; return item; } else { throw new ListIndexOutOfBoundsException("List index out of bounds on get"); } }

public void remove(int index) throws ListIndexOutOfBoundsException { if(index >= 0 && index < k) { if(index == 0) { head = head.next; } else { Node prev = find(index-1); Node curr = prev.next; prev.next = curr.next; } k--; } else { throw new ListIndexOutOfBoundsException("List index out of bounds on remove"); } } public void clear() { head = null; k = 0; } public void display() { Node curr = head; while(curr != null) { System.out.printf("%-18s %-22s %s ", curr, curr.item, curr.next); curr = curr.next; } }

public void testComparison(Object o) { Node curr = head; if(curr.item.equals(o)) { System.out.println("Matches"); } else { System.out.println("Does not match"); }

} public void add(Object o) {

} public void addFirst(Object o) {

} public int indexOf(Object o) { return -1; }

public int lastIndexOf(Object o) { return -1; } public boolean remove(Object o) { return false; } public boolean removeLast() {

return false; } public boolean removeLast(Object o) { return false; } public int removeAll(Object o) { return 0; } public void set(int index, Object o) {

} public void setAll(Object find, Object replacement) {

} public Object[] toArray() { Object[] array = null; return array; } }

This is the Person.java code below

package Person;

public class Person {

private int ID; private String name; public Person(int ID, String name) { this.ID = ID; this.name = name; } public int getID() { return this.ID; } public String getName() { return this.name; }

@Override public boolean equals(Object o) { if(o == null || !(o instanceof Person)) { return false; } else if(o == this) { return true; }

Person p = (Person) o; return (this.ID == p.ID && this.name.equals(p.name)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions