Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

The description and requirement is below. I also upload my code here. But my problem is, I do not know how to do a main class for this. So it does not run. Please help me to do a main class so that it can compile. And please help me to check my code. Thank you very much!

Given code:
The Sorted List ADT
Implement the SortedList class. The SortedList class extends the AbstractList class. Both can be seen here. Your assignment is to implement (recursively) all of the abstract methods of the AbstractList class.
They are:
insert (recursive)
iterator
remove (recursive)
retrieve
search (recursive)
You must also implement an Iterator inner class for the SortedList class. You must submit a modified SortedList.java file with your source code.
Do not submit and do not modify the List.java or the AbstractList.java file.
/*
*
* List.java
*
*/

public interface List extends Iterable {
void insert(E data);
void remove(E key);
E retrieve(int index);
boolean search(E key);
}

/*
*
* AbstractList.java
*
*/

public abstract class AbstractList implements List {

protected class Node {

protected Node(T data) {
this.data = data;
}

protected T data;
protected Node next;
}

protected Node head;
}

/*
*
* SortedList.java
*
*/

public class SortedList> extends AbstractList {

}

My written code:
import java.util.Iterator;

public class SortedList> extends AbstractList {

private int size;
Node curr = head;

Node prev = null;

Node temp;
public SortedList() {
head = null;
size=0;
}

//Insert

public void insert(E data) {
if(head == null){
head = new Node(data);
size++;
}
else{
Node curr = head;
Node prev = null;
Node temp = null;
while(curr.next != null && data.compareTo(curr.next.data) >= 0){
prev = curr;
curr = curr.next;
}
if (curr.next != null) {
temp.next = curr;
prev.next = temp;
}
else {
temp.next = head;
head = temp;

}
if (head == null) {
head.data = (E) temp;
head.next = null;
}
curr.next = new Node(data);
size++;
}
}
//Remove
public void remove(E data) {
if(head == null){
head = new Node(data);
size++;
}
else {
Node curr = head;
Node prev = null;
Node temp = null;
while(curr.next != null){
prev = curr;
curr = curr.next;
}
if (curr != null && temp == curr.data) {
prev.next = curr.next;
}
if (prev !=null){
prev.next = curr.next;
}
else{
head = curr.next;
}
if (head == null) {
head.data = (E) temp;
head.next = null;
}
curr.next = new Node(data);
size++;

}
}
//Retrieve
public E retrieve(int index){
int i = 0;

while(curr.next != null && i !=index){

curr = curr.next;
}
if (i == index){
return curr.data;
}
else {
return null;

}
}
//Search
public boolean search(E data){
for(int i = 1; i if(curr.next == null) {
return false;
}
else {
return true;
}
}
return true;
}
//Iterator
public Iterator iterator(){
return new Iterator(){

Node curr=head;

public void remove() {
throw new UnsupportedOperationException();
}


public boolean hasNext() {
return (curr.next != null);
}


public E next() {
E temp = (E) curr.data;
curr = curr.next;
return temp;

}
};

 


}
//Source List
public interface List extends Iterable {
void insert(E data);
void remove(E key);
E retrieve(int index);
boolean search(E key);
}
//Source AbstratList
public abstract class AbstractList implements List {

protected class Node {

protected Node(T data) {
this.data = data;
}

protected T data;
protected Node next;
}

protected Node head;
}

}

Java, Programming

  • Category:- Java
  • Reference No.:- M92400745
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Java

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As