Question

In: Computer Science

java.lang.NullPointerException    at FirstLastList.swap(FirstLastList.java:85)    at FirstLastAppTest.main(FirstLastAppTest.java:32) Plese correct this error class FirstLastAppTest.Java is for testing...

java.lang.NullPointerException
   at FirstLastList.swap(FirstLastList.java:85)
   at FirstLastAppTest.main(FirstLastAppTest.java:32)

Plese correct this error

class FirstLastAppTest.Java is for testing purposes and not for editing.

class FirstLastAppTest
{
public static void main(String[] args)
{
FirstLastList lst1 = new FirstLastList(); // Start a new FirstLastList called lst1

lst1.insertLast(1); // Add links with data to the last position
lst1.insertLast(3);
lst1.insertLast(7);
lst1.insertLast(4);
System.out.print("\nlst1: "); // print the description for the list
lst1.displayList(); // print the contents of the list

FirstLastList lst2 = new FirstLastList(); // Start a new FirstLastList called lst2

lst2.insertLast(2); // Add links with data to the last position
lst2.insertLast(4);
lst2.insertLast(5);
lst2.insertLast(8);
lst2.insertLast(6);
System.out.print("\nlst2: "); // print the description for the list
lst2.displayList(); // print the contents of the list

System.out.print("\nlst1.join(lst2): "); // print the action to take place: lst1.join(lst2)
lst1.join(lst2); // call the join method for lst1 to add lst2
System.out.print("\nlst1: "); // print the description for the list
lst1.displayList(); // print the contents of the list lst1; post join()
System.out.print("lst2: "); // print the description for the list
lst2.displayList(); // print the contents of the list lst2; post join()

System.out.print("\nlst1.swap(): "); // print the action to take place: lst1.swap()
lst1.swap(); // call the swap method for lst1
System.out.print("\nlst1: "); // print the description for the list
lst1.displayList(); // print the contents of the list lst1; post swap()
} // end main()
} // end class

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class FirstLastList
{
private Link first; // ref to first link
private Link last; // ref to last link
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no links on list yet
last = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link

if( isEmpty() ) // if empty list,
last = newLink; // newLink <-- last
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{
// (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
public void join(FirstLastList otherList)
{
Link current = otherList.first; // start at beginning
while(current != null) // until end of list,
{
this.insertLast(current.dData);
current = current.next; // move to next link
}
otherList.first=null;
}

public void swap()
{
int count = 0;
Link current = first; // start at beginning

while(current != null) // until end of list,
{
count++;
current = current.next; // move to next link

if(count < 2)
{
System.out.println("There is not enough element in the list.");
}
else
{
long firstData = first.dData; // store the data of the first node
long lastData = current.dData; // please use current object of the Link class / store the data of last node
first.dData = lastData; // swap
current.dData = firstData; // swap
}
}
}

}   

class Link
{
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{ dData = d; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
// -------------------------------------------------------------
} // end class Link
////////////////////////////////////////////////////////////////

class FirstLastApp
{
public static void main(String[] args)
{ // make a new list
FirstLastList lst1 = new FirstLastList();
lst1.insertFirst(1);
lst1.insertLast(3);
lst1.insertLast(7);
lst1.insertLast(4);
System.out.print("List 1 is : ");
lst1.displayList();

FirstLastList lst2 = new FirstLastList();
lst2.insertFirst(2);
lst2.insertLast(4);
lst2.insertLast(5);
lst2.insertLast(8);
lst2.insertLast(6);
System.out.print("\nList 2 is : ");
lst2.displayList();

lst1.join(lst2);
System.out.print("\nAfter joining List 1 is : ");
lst1.displayList();

System.out.print("\nList 2 is : ");
lst2.displayList();

try
{
lst1.swap();
System.out.println("\nAfter swap the list 1 is : ");
lst1.displayList();
lst2.swap();
System.out.println("\nAfter swap the list 2 is : ");
lst2.displayList();
}
catch(Exception e)
{
System.out.println( "Exception:" + e);
}
} // end main()
} // end class FirstLastAppd

so swap method works after both the queues have been merged, the first and last element swap places. in this case 1 & 8 should swap places.

Solutions

Expert Solution

Following is the method, which needs fixes in FirstLastList.java :

public void swap()
        {
                int count = 0;
                Link current = first; // start at beginning

                /*
                 * First ensure there there are at least 2 elements.
                 */
                while(current != null) // until end of list,
                {
                        count++;
                        current = current.next; // move to next link
                }
                
                if(count < 2)
                {
                        /*
                         * Display message, that we do not have enough elements.
                         */
                        System.out.println("There is not enough element in the list.");
                }
                else
                {
                        /*
                         * Swap the first and last element values.
                         */
                        long firstData = first.dData; // store the data of the first node
                        long lastData = last.dData; // store the data of last node
                        
                        first.dData = lastData; // swap
                        last.dData = firstData; // swap
                }                       
        }

Following is the run, when FirstLastAppTest.java is run as main :


lst1: List (first-->last): 1 3 7 4 

lst2: List (first-->last): 2 4 5 8 6 

lst1.join(lst2): 
lst1: List (first-->last): 1 3 7 4 2 4 5 8 6 
lst2: List (first-->last): 

lst1.swap(): 
lst1: List (first-->last): 6 3 7 4 2 4 5 8 1 

Related Solutions

Calculate and interpret the margin of error with the sample of 22, confidence 85% and the...
Calculate and interpret the margin of error with the sample of 22, confidence 85% and the sample standard deviation 4.
explain how to legally correct an error a nurse makes in documentation
explain how to legally correct an error a nurse makes in documentation
What is Type I error? How do we correct for Type I error? What happens when...
What is Type I error? How do we correct for Type I error? What happens when we correct for Type I error? What is Type II error? How do we correct for Type II error? What happens when we correct for Type II error? How can we correct for both Type I and Type II error at the same time? Which error is considered the worst type of error to commit?
For the stress data given below with the nearest error of 1: 32 17 38 82...
For the stress data given below with the nearest error of 1: 32 17 38 82 48 5 37 13 19 31 19 a) Construct a frequency distribution table. b) Construct the three types of statistical graphs. c) Determine the (1) Mean, (2) Median, (3) Mode, (4) Range, Variance, and (6) Standard Deviation.
Find the error in each of the following code segments and explain how to correct it....
Find the error in each of the following code segments and explain how to correct it. x = 1; while ( x <= 10 ); x++; } for ( y = .1; y != 1.0; y += .1 System.out.println( y ); switch ( n ) { case 1: System.out.println( “The number is 1” ); case 2: System.out.println( “The number is 2” ); break; default: System.out.println( “The number is not 1 or 2” ); break; } The following code should print...
Which one of the following statements is correct about hypothesis testing?
Which one of the following statements is correct about hypothesis testing?
Im getting an error on the Player.h class. Error reads expected initializer before player. I have...
Im getting an error on the Player.h class. Error reads expected initializer before player. I have put a comment by the line with the error. sample out put is Welcome to Rock Paper Scissors You played: paper Computer played: paper It's a tie Player.h #include<string> using namespace std; class Player{    private:        int play;//variable        public:    string getPlay();//variables    void setPlay(string play);    void setPlay(int play); } string Player = getPlay(){//error    if(this.play == 1){   ...
This class is Global logistics management Calculate the theoretical and the practical (using 85% rule of...
This class is Global logistics management Calculate the theoretical and the practical (using 85% rule of thumb) square footage needed to warehouse 200,000 pallets under the following conditions: Pallet size is 42” x 42” Pallets will be stacked 4 high Aisle width is 9’                                  Theoretical square feet required_________                 Practical square feet required (using 85% rule of thumb) ___________ Pallet size is 42” x 42” Pallets will be stacked 5 high Aisle width is 12’                                  Theoretical...
32.   Which of the following are correct concerning the American political economy of the early 19th...
32.   Which of the following are correct concerning the American political economy of the early 19th century? (List the letters of all the correct answers. If none are correct, write “none”.) (a)   Resources were plentiful. (b)   Government taxation and regulation were minimal. (c)   There seemed to be a strong link between effort and results.
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface member declaration" Can someone please help me. Here is my code: using static System.Console; class LetterDemo {    static void Main()    {      Letter letter1 = new Letter();      CertifiedLetter letter2 = new CertifiedLetter();      letter1.Name = "Electric Company";      letter1.Date = "02/14/18";      letter2.Name = "Howe and Morris, LLC";      letter2.Date = "04/01/2019";      letter2.TrackingNumber = "i2YD45";      WriteLine(letter1.ToString());      WriteLine(letter2.ToString() +       " Tracking number: " + letter2.TrackingNumber);    } } class Letter {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT