SlideShare a Scribd company logo
Write a java class "LIST" that outputs:
//main:
public class AssignmentThree
{
public static void main(String[] args)
{
List myList = new List(15);
// Cause List Empty Message
myList.removeFront();
myList.removeRear();
myList.removeItem("a");
// Cause Not found message
myList.addToFront("x");
myList.removeItem("y");
myList.removeItem("x");
myList.addAfterItem("x", "z");
myList.addBeforeItem("x", "z");
// Normal behavior
myList.addToFront("not.");
myList.addToFront("or");
myList.addToRear("is");
myList.addToRear("try.");
myList.addAfterItem("is", "no");
myList.addBeforeItem("is", "There");
myList.addToFront("Do");
myList.addAfterItem("or", "do");
myList.print("Original list");
myList.printSorted("Sorted Original List");
sop(" Front is " + myList.getFront());
sop("Rear is " + myList.getRear());
sop("Count is " + myList.askCount());
sop("Is There present? " + myList.isPresent("There"));
sop("Is Dog present? " + myList.isPresent("Dog"));
myList.addToFront("junk");
myList.addToRear("morejunk");
myList.addAfterItem("or", "moremorejunk");
myList.print("List with junk");
sop("Count is " + myList.askCount());
myList.removeFront();
myList.removeRear();
myList.removeItem("moremorejunk");
myList.print("List with junk removed");
sop("Count is " + myList.askCount());
sop("");
// Cause List Full message
for(int ii = 0; ii < 10; ++ii)
{
myList.addToFront(DUMMY);
}
myList.addToRear(DUMMY);
myList.addBeforeItem("no", DUMMY);
myList.addAfterItem("There", DUMMY);
myList.print("After filling List");
sop("Count is " + myList.askCount());
while(myList.isPresent(DUMMY)) myList.removeItem(DUMMY);
myList.print("After removing " + DUMMY );
sop("Count is " + myList.askCount());
}
private static void sop(String s)
{
System.out.println(s);
}
private static final String DUMMY = "dummy";
}
//Class List:
public class List {
public List(int size)
{
}
public void addToFront(java.lang.String item)
{
}
public void addToRear(java.lang.String item)
{
}
public void addBeforeItem(java.lang.String beforeItem,
java.lang.String item)
{
}
public void addAfterItem(java.lang.String afterItem,
java.lang.String item)
{
}
public java.lang.String getFront()
{
}
public java.lang.String getRear()
{
}
public boolean isPresent(java.lang.String item)
{
}
public int askCount()
{
}
public void removeFront()
{
}
public void removeRear()
{
}
public void removeItem(java.lang.String item)
{
}
public void print(java.lang.String title)
{
}
public void printSorted(java.lang.String title)
{
}
}
Solution
The List is the base interface for all list types, and the ArrayList and LinkedList classes are two
common List’s implementations.
Besides ArrayList and LinkedList, Vector class is a legacy collection and later was retrofitted to
implement the Listinterface. Vector is thread-safe, but ArrayList and LinkedList are not. The
following class diagram depicts the inheritance tree of the List collections.
It’s a good practice to declare a list instance with a generic type parameter, for example:
1
2
3
4
List listAnything = new ArrayList();
List listWords = new ArrayList();
List listNumbers = new ArrayList();
List linkedWords = new LinkedList();
Since Java 7, we can remove the type parameter on the right side as follows:
1
2
List listNumbers = new ArrayList<>();
List linkedWords = new LinkedList<>();
The compiler is able to infer the actual type parameter from the declaration on the left side.
When creating a new ArrayList using the empty constructor, the list is constructed with an initial
capacity of ten. If you are sure how many elements will be added to the list, it’s recommended to
specify a capacity which is large enough. Let’s say, if we know that a list contains around 1000
elements, declare the list as follows:
1
List listNumbers = new ArrayList<>(1000);
It’s also possible to construct a list that takes elements from an existing collection, for example:
1
2
3
List listNumberOne; // existing collection
List listNumberTwo = new ArrayList<>(listNumberOne);
The listNumberTwo constructed with copies of all elements from the listNumberOne.The
methods add(Object), add(index, Object) and addAll() are used to add elements to the list. It
requires to add elements of the same type (or sub type) as the type parameter declared by the list.
For example:
1
2
3
4
5
6
7
8
9
List listStrings = new ArrayList();
// OK to add Strings:
listStrings.add("One");
listStrings.add("Two");
listStrings.add("Three");
// But this will cause compile error
listStrings.add(123);
Adding elements of sub types of the declared type:
1
2
3
4
5
6
List linkedNumbers = new LinkedList<>();
linkedNumbers.add(new Integer(123));
linkedNumbers.add(new Float(3.1415));
linkedNumbers.add(new Double(299.988));
linkedNumbers.add(new Long(67000));
We can insert an element into the list at a specified indexThe simplest way to sort out elements
in a list is using the Collections.sort() static method which sorts the specified list into ascending
order, based on the natural ordering of its elements. Here’s an example:
1
2
3
4
5
6
7
8
9
10
11
12
List listStrings = new ArrayList();
listStrings.add("D");
listStrings.add("C");
listStrings.add("E");
listStrings.add("A");
listStrings.add("B");
System.out.println("listStrings before sorting: " + listStrings);
Collections.sort(listStrings);
System.out.println("listStrings after sorting: " + listStrings);
Output:
1
2
listStrings before sorting: [D, C, E, A, B]
listStrings after sorting: [A, B, C, D, E]
1
2
3
4
List listAnything = new ArrayList();
List listWords = new ArrayList();
List listNumbers = new ArrayList();
List linkedWords = new LinkedList();

More Related Content

PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PDF
Please help me to make a programming project I have to sue them today- (1).pdf
PDF
Implementation The starter code includes List.java. You should not c.pdf
PPT
12_-_Collections_Framework
PDF
please i need help Im writing a program to test the merge sort alg.pdf
DOCX
Collections framework
PPTX
collection framework.pptx
DOCX
ArrayList.docx
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Please help me to make a programming project I have to sue them today- (1).pdf
Implementation The starter code includes List.java. You should not c.pdf
12_-_Collections_Framework
please i need help Im writing a program to test the merge sort alg.pdf
Collections framework
collection framework.pptx
ArrayList.docx

Similar to Write a java class LIST that outputsmainpublic class Ass.pdf (20)

PPTX
Dr.EEM(Java Collection Jrameworkaa).pptx
PDF
For this lab you will complete the class MyArrayList by implementing.pdf
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
PPT
A2003822018_21789_17_2018_09. ArrayList.ppt
PDF
A popular implementation of List is ArrayList- Look up how to instanti.pdf
PDF
Java ArrayList Tutorial | Edureka
PPTX
Nature Activities Binder _ by Slidesgo.pptx
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PPT
Java10 Collections and Information
PPTX
Collections - Lists & sets
DOCX
Lecture 18Dynamic Data Structures and Generics (II).docx
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
DOC
Advanced core java
PDF
5 collection framework
PPT
10-linked-list.ppt
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
PDF
Please and Thank youObjective The purpose of this exercise is to .pdf
Dr.EEM(Java Collection Jrameworkaa).pptx
For this lab you will complete the class MyArrayList by implementing.pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
A2003822018_21789_17_2018_09. ArrayList.ppt
A popular implementation of List is ArrayList- Look up how to instanti.pdf
Java ArrayList Tutorial | Edureka
Nature Activities Binder _ by Slidesgo.pptx
STAGE 2 The Methods 65 points Implement all the methods t.pdf
Java10 Collections and Information
Collections - Lists & sets
Lecture 18Dynamic Data Structures and Generics (II).docx
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
Advanced core java
5 collection framework
10-linked-list.ppt
public class MyLinkedListltE extends ComparableltEgtg.pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Please and Thank youObjective The purpose of this exercise is to .pdf
Ad

More from ebrahimbadushata00 (20)

PDF
irktors (lcloding his accoueting instructor thmivenity c. All student.pdf
PDF
Is there a solution manual to group dynamics for team (fourth Editio.pdf
PDF
IntroductionFor this program, you will implement an interface that.pdf
PDF
In Python,Create a program that asks the user for a number and the.pdf
PDF
In contrast to sexual reproduction in animals, sexually-reproducing .pdf
PDF
Ignore what I have written because Im pretty sure its wrong. Thank.pdf
PDF
How can crisis leadership be learnedSolutionAn organization n.pdf
PDF
Given the following information on a project develop early and la.pdf
PDF
Global Economy, National Economies, and CompetitionIn the first pa.pdf
PDF
Explain why owners equity includes common stock as a liability eve.pdf
PDF
Evaluate the statements below and determine which is the best reason.pdf
PDF
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdf
PDF
Conclusion Phases of Oxidative Phosphorylation Focus your attention.pdf
PDF
Computer Forensics Process Please respond to the followingThe.pdf
PDF
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
PDF
Can someone solveexplain this I thought I was understanding this, .pdf
PDF
C The ame compound componda with F Souls . E Difluut eoupou ds with.pdf
PDF
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
PDF
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
PDF
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
irktors (lcloding his accoueting instructor thmivenity c. All student.pdf
Is there a solution manual to group dynamics for team (fourth Editio.pdf
IntroductionFor this program, you will implement an interface that.pdf
In Python,Create a program that asks the user for a number and the.pdf
In contrast to sexual reproduction in animals, sexually-reproducing .pdf
Ignore what I have written because Im pretty sure its wrong. Thank.pdf
How can crisis leadership be learnedSolutionAn organization n.pdf
Given the following information on a project develop early and la.pdf
Global Economy, National Economies, and CompetitionIn the first pa.pdf
Explain why owners equity includes common stock as a liability eve.pdf
Evaluate the statements below and determine which is the best reason.pdf
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdf
Conclusion Phases of Oxidative Phosphorylation Focus your attention.pdf
Computer Forensics Process Please respond to the followingThe.pdf
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
Can someone solveexplain this I thought I was understanding this, .pdf
C The ame compound componda with F Souls . E Difluut eoupou ds with.pdf
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
master seminar digital applications in india
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Lesson notes of climatology university.
PPTX
Presentation on HIE in infants and its manifestations
PDF
Computing-Curriculum for Schools in Ghana
Classroom Observation Tools for Teachers
Microbial diseases, their pathogenesis and prophylaxis
human mycosis Human fungal infections are called human mycosis..pptx
RMMM.pdf make it easy to upload and study
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH
master seminar digital applications in india
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Microbial disease of the cardiovascular and lymphatic systems
Chinmaya Tiranga quiz Grand Finale.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
A systematic review of self-coping strategies used by university students to ...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
Presentation on HIE in infants and its manifestations
Computing-Curriculum for Schools in Ghana

Write a java class LIST that outputsmainpublic class Ass.pdf

  • 1. Write a java class "LIST" that outputs: //main: public class AssignmentThree { public static void main(String[] args) { List myList = new List(15); // Cause List Empty Message myList.removeFront(); myList.removeRear(); myList.removeItem("a"); // Cause Not found message myList.addToFront("x"); myList.removeItem("y"); myList.removeItem("x"); myList.addAfterItem("x", "z"); myList.addBeforeItem("x", "z"); // Normal behavior myList.addToFront("not."); myList.addToFront("or"); myList.addToRear("is"); myList.addToRear("try."); myList.addAfterItem("is", "no"); myList.addBeforeItem("is", "There"); myList.addToFront("Do"); myList.addAfterItem("or", "do"); myList.print("Original list"); myList.printSorted("Sorted Original List"); sop(" Front is " + myList.getFront()); sop("Rear is " + myList.getRear());
  • 2. sop("Count is " + myList.askCount()); sop("Is There present? " + myList.isPresent("There")); sop("Is Dog present? " + myList.isPresent("Dog")); myList.addToFront("junk"); myList.addToRear("morejunk"); myList.addAfterItem("or", "moremorejunk"); myList.print("List with junk"); sop("Count is " + myList.askCount()); myList.removeFront(); myList.removeRear(); myList.removeItem("moremorejunk"); myList.print("List with junk removed"); sop("Count is " + myList.askCount()); sop(""); // Cause List Full message for(int ii = 0; ii < 10; ++ii) { myList.addToFront(DUMMY); } myList.addToRear(DUMMY); myList.addBeforeItem("no", DUMMY); myList.addAfterItem("There", DUMMY); myList.print("After filling List"); sop("Count is " + myList.askCount()); while(myList.isPresent(DUMMY)) myList.removeItem(DUMMY); myList.print("After removing " + DUMMY ); sop("Count is " + myList.askCount()); }
  • 3. private static void sop(String s) { System.out.println(s); } private static final String DUMMY = "dummy"; } //Class List: public class List { public List(int size) { } public void addToFront(java.lang.String item) { } public void addToRear(java.lang.String item) { } public void addBeforeItem(java.lang.String beforeItem, java.lang.String item) { } public void addAfterItem(java.lang.String afterItem, java.lang.String item) { } public java.lang.String getFront() { }
  • 4. public java.lang.String getRear() { } public boolean isPresent(java.lang.String item) { } public int askCount() { } public void removeFront() { } public void removeRear() { } public void removeItem(java.lang.String item) { } public void print(java.lang.String title) { } public void printSorted(java.lang.String title) { } } Solution
  • 5. The List is the base interface for all list types, and the ArrayList and LinkedList classes are two common List’s implementations. Besides ArrayList and LinkedList, Vector class is a legacy collection and later was retrofitted to implement the Listinterface. Vector is thread-safe, but ArrayList and LinkedList are not. The following class diagram depicts the inheritance tree of the List collections. It’s a good practice to declare a list instance with a generic type parameter, for example: 1 2 3 4 List listAnything = new ArrayList(); List listWords = new ArrayList(); List listNumbers = new ArrayList(); List linkedWords = new LinkedList(); Since Java 7, we can remove the type parameter on the right side as follows: 1 2 List listNumbers = new ArrayList<>(); List linkedWords = new LinkedList<>(); The compiler is able to infer the actual type parameter from the declaration on the left side. When creating a new ArrayList using the empty constructor, the list is constructed with an initial capacity of ten. If you are sure how many elements will be added to the list, it’s recommended to specify a capacity which is large enough. Let’s say, if we know that a list contains around 1000 elements, declare the list as follows: 1 List listNumbers = new ArrayList<>(1000); It’s also possible to construct a list that takes elements from an existing collection, for example: 1 2 3 List listNumberOne; // existing collection List listNumberTwo = new ArrayList<>(listNumberOne); The listNumberTwo constructed with copies of all elements from the listNumberOne.The methods add(Object), add(index, Object) and addAll() are used to add elements to the list. It requires to add elements of the same type (or sub type) as the type parameter declared by the list. For example:
  • 6. 1 2 3 4 5 6 7 8 9 List listStrings = new ArrayList(); // OK to add Strings: listStrings.add("One"); listStrings.add("Two"); listStrings.add("Three"); // But this will cause compile error listStrings.add(123); Adding elements of sub types of the declared type: 1 2 3 4 5 6 List linkedNumbers = new LinkedList<>(); linkedNumbers.add(new Integer(123)); linkedNumbers.add(new Float(3.1415)); linkedNumbers.add(new Double(299.988)); linkedNumbers.add(new Long(67000)); We can insert an element into the list at a specified indexThe simplest way to sort out elements in a list is using the Collections.sort() static method which sorts the specified list into ascending order, based on the natural ordering of its elements. Here’s an example: 1 2 3 4 5
  • 7. 6 7 8 9 10 11 12 List listStrings = new ArrayList(); listStrings.add("D"); listStrings.add("C"); listStrings.add("E"); listStrings.add("A"); listStrings.add("B"); System.out.println("listStrings before sorting: " + listStrings); Collections.sort(listStrings); System.out.println("listStrings after sorting: " + listStrings); Output: 1 2 listStrings before sorting: [D, C, E, A, B] listStrings after sorting: [A, B, C, D, E] 1 2 3 4 List listAnything = new ArrayList(); List listWords = new ArrayList(); List listNumbers = new ArrayList(); List linkedWords = new LinkedList();