(Solved Homework): Add the following methods to the LinkedIntList : –size Returns size of the list.

Add the following methods to the LinkedIntList

: –size Returns size of the list.

–isEmpty Returns true/false if the list is empty.

–clear Clear the entire list

–toString Prints the whole list

–indexOf Returns the index of that value (First Occurrence)

–contains Returns true/false if the value given by user exist in the list.

This is the continuation of LinkedIntList class.

Add these methods in your LinkedIntList class.

Expert Answer

 import java.util.*;

import com.sun.xml.internal.bind.v2.model.core.ID;

//Class definition
public class LinkedListDemo
{
// create a Linked List class object
LinkedList list = new LinkedList();
//Method to return size
int size()
{
//Initialize the counter to zero
int count = 0;
//Creates an iterator
Iterator<String> itr = list.iterator();
//Moves till data available
while(itr.hasNext())
{
//Increase the counter by one
count++;
//Moves to next data
itr.next();
}//End of while
//Returns the counter
return count;
}//End of method

//Method to clear the list
void createList()
{
//To store data entered by the user
String data;
//Scanner class object created to accept data from the user
Scanner sc = new Scanner(System.in);
//Accept data till end entered by the user
System.out.println(“Enter string data to add to the list. Enter end to stop.”);
do
{
//Accept data
data = sc.next();
//Checks for end
if(data.equalsIgnoreCase(“end”))
//Come out of the loop
break;
else
//If not end then add it to the list
list.add(data);
}while(true);//End of while
}//End of method

//Method to return true if list is empty
boolean isEmpty()
{
//Initialize the counter to zero
int count = 0;
//Calls the size method and store the size in counter
count = size();
//Checks if the count is zero then return true
if(count == 0)
return true;
//Otherwise return false
else
return false;
}//End of method

//Method to clear all the data in the list
void clear()
{
//Calls the size method and store the size in c
int c = size();
//Loops till c
for(int i = 0; i < c; i++)
{
//removes the first element till condition becomes false
list.removeFirst();
}//End of for
}//End of method

//Overrides the toString method
public String toString()
{
//Initializes the result string to null
String result = “”;
//Creates an iterator class object
Iterator<String> itr = list.iterator();
//Moves till data available
while(itr.hasNext())
{
//Concatenates the data available in the list with the result
result += itr.next() + “, “;
}//End of while
//Return the result
return result;
}//End of method

//Returns the index position of the string given as parameter
int indexOf(String str)
{
//Initially ind is -1
int ind = -1;
//To store current string of the list
String current;
//Initializes the flag to -1 and counter to zero
int flag = -1, counter = 0;
//Creates an iterator class object
Iterator<String> itr = list.iterator();
//Moves till data available
while(itr.hasNext())
{
//Stores the current data from the list
current = itr.next();
//Increase the counter by one
counter++;
//Checks if the current item is equal to the string given as parameter (without considering the case)
if(current.equalsIgnoreCase(str))
{
//Set the flag value to the counter value
flag = counter;
//Come out of the loop
break;
}//End of if
}//End of while
//Returns the flag value
return flag;
}//End of method

//Return true if string is available otherwise false
boolean contains(String str)
{
//To store current string
String current;
//Initializes the flag to -1
int flag = -1;
//Creates an iterator class object
Iterator<String> itr = list.iterator();
//Moves till data available
while(itr.hasNext())
{
//Stores the current data from the list
current = itr.next();
//Checks if the current item is equal to the string given as parameter (without considering the case)
if(current.equalsIgnoreCase(str))
{
//Sets the flag to zero
flag = 0;
//Come out of the loop
break;
}//End of if
}//End of while
//Checks if the flag value is -1 return false
if(flag == -1)
return false;
//Otherwise return true
else
return true;
}//End of method

//Driver method
public static void main(String args[])
{
//To store string entered by the user
String data;
//Scanner class object created to accept data
Scanner sc = new Scanner(System.in);
//Creates LinkedListDemo class object
LinkedListDemo ld = new LinkedListDemo();
//Calls the method to add string to the list
ld.createList();
//Display the list using toString()
System.out.println(“List contents: ” + ld);
//Displays the size of the list
System.out.println(“Size: ” + ld.size());
//Displays the empty status
System.out.println(“Empty Status: ” + ld.isEmpty());

//Accepts a string to check the availability
System.out.println(“Enter a string to check exist or not: “);
data = sc.next();
//If returns true display available
if(ld.contains(data))
System.out.println(data + ” is available”);
//Otherwise display not available
else
System.out.println(data + ” is not available”);
//Accepts a string to display the index position
System.out.println(“Enter a string to find index position: “);
data = sc.next();
//Calls the method and stores the index position
int position =ld.indexOf(data);
//If the position is -1 display not available
if(position == -1)
System.out.println(data + ” is not available”);
//Otherwise display its index position
else
System.out.println(data + ” is available at ” + position);
//Clear the contents of the list
ld.clear();
//Display the size after clear
System.out.println(“After clear Size: ” + ld.size());
}//End of main
}//End of class

Output:

Enter string data to add to the list. Enter end to stop.
This
is
demo
end
List contents: This, is, demo,
Size: 3
Empty Status: false
Enter a string to check exist or not:
demo
demo is available
Enter a string to find index position:
is
is is available at 2
After clear Size: 0

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents