Question

Step 1 Your text has an example of LinkedList class. Convert the class so that the...

Step 1 Your text has an example of LinkedList class. Convert the class so that the Link objects have a single long instance variable -- call it "data" -- and the Link instance variable.. Step 2 Then add the following capabilities to the class. A constructor that takes an array of longs as an argument. The contents of the array are use to create Link objects that become objects in the LinkedList. A "search" method that takes a long argument and returns a boolean value. The method searches the LinkedList object for Link objects with a data value that matches the long argument. It returns a true or false based on whether or not a matching value is present in the LinkedList. A "numberOfValues" method that takes a long argument and returns an int value. The method searches the LinkedList object for Link objects with a data value that matches the long argument. It returns a numeric value that is the number of times the long argument is present in the LinkedList. A "replace" method that takes two long arguments and returns a void. The method finds all occurrences of the first long value in the Links of the LinkedList and replaces their data value with the method's second long value. A "zero" method that replaces all values in the LinkedList's Link objects with zeros. A "getArray" method that returns a long array. The method returns long array that contains the long data values in the Link objects of the LinkedList Step 3 Give the project a LinkedListDriver class that contains your main method. Instantiate a LinkedList object, using the array constructor. Demonstrate the isEmpty, insertFirst, deleteFirst, and displayList methods. Also demonstrate the methods from Step 2. Remember that your output should clearly state what you are doing in your code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class LinkedListDriver {

    public static void main(String[] args) {

        long arr[] = new long[]{10L, 20L, 30L, 40L, 10L};
        LinkedList list = new LinkedList(arr);

        list.displayList();

        System.out.println("num of values: " + list.numberOfValues(10));

        list.replace(10, 30);
        list.displayList();
    }

}


=================================



public class LinkedList {

    class Link {
        long data;
        Link next;
        public Link(long data, Link next) {
            this.data = data;
            this.next = next;
        }
        public Link(long data) {
            this.data = data;
        }       
    }

    private Link head;

    public LinkedList(long arr[]) {
        for(int i=arr.length-1; i >=0; i--) {
            head = new Link(arr[i], head);
        }
    }

    public boolean search(long x) {
        Link start = head;
        while(start != null) {
            if(start.data == x) {
                return true;
            }
            start = start.next;
        }
        return false;
    }

    public int numberOfValues(long x) {
        int count = 0;
        Link start = head;
        while(start != null) {
            if(start.data == x) {
                count++;
            }
            start = start.next;
        }
        return count;
    }

    public void replace(long a, long b) {
        Link start = head;
        while(start != null) {
            if(start.data == a) {
                start.data = b;
            }
            start = start.next;
        }
    }

    public boolean isEmpty() {
        return head == null;
    }

    public void insertFirst(long x) {
        head = new Link(x, head);
    }

    public void deleteFirst() {
        if(!isEmpty()) {
            head = head.next;
        }
    }

    public void displayList() {
        Link start = head;
        while(start != null) {
            System.out.print(start.data + " ");
            start = start.next;
        }
        System.out.println();
    }

    public int size() {
        int count = 0;
        Link start = head;
        while(start != null) {
            count++;
            start = start.next;
        }
        return count;
    }

    public void zero() {
        Link start = head;
        while(start != null) {
            start.data = 0;
            start = start.next;
        }
    }

    public long[] getArray() {
        long result[] = new long[size()];
        Link start = head;
        int count = 0;
        while(start != null) {
            result[count++] = start.data;
            start = start.next;
        }
        return result;
    }

}

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Step 1 Your text has an example of LinkedList class. Convert the class so that the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Your text has an example of LinkedList class. Convert the class so that the Link objects have a s...

    Your text has an example of LinkedList class. Convert the class so that the Link objects have a single long instance variable -- call it "data" -- and the Link instance variable.. Step 2 Then add the following capabilities to the class. A constructor that takes an array of longs as an argument. The contents of the array are use to create Link objects that become objects in the LinkedList. A "search" method that takes a long argument and returns...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • information and technology

    Create an EmployeeLinkedList class.  However, instead of generic Link class for the link objects, you should use objects created from an Employee class as the objects stored in the list.The Employee class represents an employee at a company.  It should have instance variables for the employee's first name, last name, id number (use an integer), date of birth (us a String), and rate of pay.  Also, any other variable(s) needed to make an Employee object work in a linked list should...

  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...

    put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following: A private double 1D array instance variable named numbers. A constructor that takes one parameter, a 1D double array. The constructor should throw an IllegalArgumentException with the message "Array length of zero" if the parameter has a length of 0. If the exception is not thrown, the constructor should create the numbers array and copy every element from the parameter into the numbers instance...

  • text format separate by 1) 2) Point3D Class Write a class named Point3D that extends the...

    text format separate by 1) 2) Point3D Class Write a class named Point3D that extends the generic Point class you wrote in Tuesday's lab. The Point3D class is generic but restricted to Number and its subclasses. The Point3D class has a z field that contains the z coordinate. Provide a constructor for the Point3D class. Provide an accessor and mutator for field Z. Demonstrate in a main method. PointList Class Write a class named PointList that keeps a list of...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT