In this assignment, you are required to specify and implement a Java class namde
ID: 3844944 • Letter: I
Question
In this assignment, you are required to specify and implement a Java class namded IntSortedList. The instances of IntSortedList are sorted lists of integers. There should be no limit on the length of the list. The operations on sorted lists of integers supported by IntSortedList are • A default constructor that creates an empty list of integers. • An operation named empty that determines if the sorted list is empty. The operation takes no argument and returns a Boolean value. • An operation named length that returns the number of items on the sorted list. The operation takes no argument and returns a positive integer. • An operation named insert that inserts an integer to the sorted list while maintaining its sorted-ness. The operation takes the integer to insert as its only argument. It should always succeed. • An operation named delete that deletes an integer from the sorted list. The operation takes the integer to delete as its only argument. It returns true if the integer was in the sorted list. Otherwise, it returns false. Only one occurrence of the target is deleted by this operation. • An operation named show that displays the integers in the sorted list on screen. Every row except the last one contains 8 integers separated by spaces. The last row may contain less than 8 integers.
You are required to provide a Linked List based implementation of IntSortedList and write a program that makes use of IntSortedList.
Follow the following steps. 1. Refine the above specification of IntSortedList by writing method prototypes for its operations and their pre/post conditions. 2. Write a class definition for IntSortedList. The only public methods in the class definition are those operations. The private members are instance variables that represent a sorted list of integers. Write an invariant on these instance variables as comments. You can have private methods that are auxiliary to public ones. 3. Write a program that reads from keyboard a sequence of positive integers that is ended with a negative integer. The positive integers are inserted into a sort list of integers when they are read. When the program finishes reading, it displays the following information on screen: (a) the number of positive integers that have been read; (b) the positive integers.
Explanation / Answer
Here is Implementation using Java LinkedList
LinkedListModifed.java
import java.util.*;
class IntSortedList
{
private LinkedList<Integer> list;
public IntSortedList()
{
list = new LinkedList<Integer>();
}
public void insert(int item)
{
list.add(item);
Collections.sort(list);
}
public boolean empty()
{
if(list.isEmpty())
return true;
else
return false;
}
public int length()
{
return list.size();
}
public boolean delete(int item)
{
if(list.isEmpty())
{
System.out.print(" List is Empty ");
return false;
}
if(list.contains(item))
{
list.remove(new Integer(item));
Collections.sort(list);
return true;
}
else
return false;
}
public void show()
{
System.out.print("OUTPUT ");
int x=0;
for (Object list1 : list) {
if(x==8)
{
System.out.print(" ");
x=0;
}
else
{
System.out.print(list1+" ");
x++;
} }
}
}
public class LinkedListModifed {
public static void main(String[] args) {
int inputNumber=0;
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
IntSortedList list = new IntSortedList();
System.out.println("Sorted Integers using linked list ");
char ch;
/* Perform list operations */
while(true)
{
System.out.println(" 1. INSERT ITEM");
System.out.println("2. DELETE ITEM");
System.out.println("3. IS EMPTY");
System.out.println("4. SIZE OF LIST");
System.out.println("5. EXIT");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integers elements and put negative value to end");
////////Here are lines for reading sequence of integers ended with negative number///////////////////
while (inputNumber>=0) {
inputNumber = scan.nextInt();
if(inputNumber>=0)
list.insert( inputNumber );
}
/////////////////////////////////////////////////////////////
break;
case 2 :
System.out.println("Enter Item:");
int p = scan.nextInt();
if(list.delete(p))
System.out.println("Deleted Item:" + p);
else
System.out.println("Item " + p + " Not Found" );
break;
case 3 :
System.out.println("Empty status = "+ list.empty()+" ");
break;
case 4 :
System.out.println("Size = "+ list.length()+" ");
break;
case 5 :
return;
default :
System.out.println("Wrong Entry ");
break;
}
list.show();
}
}
}
OR
This is using core class implementation, follow link below to get complete assignment project Uisng Netbeans(8.0.1)
https://drive.google.com/open?id=0By9p2YlwSGU3cWhpaTV0UDJLbG8
or
Use Following Code classes below and execute:
IntItem.java
public class IntItem
{
protected int data;
protected IntItem link;
/* Constructor for IntItem class */
public IntItem()
{
link = null;
data = 0;
}
/* Constructor for passed value */
public IntItem(int d,IntItem n)
{
data = d;
link = n;
}
/* Function to set link to next item Node */
public void setLink(IntItem n)
{
link = n;
}
/* Function to set data to current item Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next item node */
public IntItem getLink()
{
return link;
}
/* Function to get data from current item Node */
public int getData()
{
return data;
}
}
IntSortedList.java
public class IntSortedList {
protected IntItem start;
public int size;
public IntSortedList()
{
start=null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to check size of list */
public int getSize()
{
return size;
}
/* Function to insert an element */
public void insert(int val)
{
IntItem nptr, ptr, tmp = null;
nptr = new IntItem(val, null);
boolean ins = false;
if (start == null)
start = nptr;
else if (val <= start.getData())
{
nptr.setLink(start);
start = nptr;
}
else
{
tmp = start;
ptr = start.getLink();
while(ptr != null)
{
if (val >= tmp.getData() && val <= ptr.getData())
{
tmp.setLink(nptr);
nptr.setLink(ptr);
ins = true;
break;
}
else
{
tmp = ptr;
ptr = ptr.getLink();
}
}
if (ins == false)
{
tmp.setLink(nptr);
}
}
size++;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
IntItem ptr = start;
for (int i = 1; i < size - 1; i++)
ptr = ptr.getLink();
ptr.setLink(null);
size --;
return;
}
IntItem ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
IntItem tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print("OUTPUT with 8 Columns ");
if (size == 0)
{
System.out.print("Sorry List is Empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
IntItem ptr = start;
System.out.print(start.getData()+" ");
ptr = start.getLink();
int x=0;
boolean isFisrtLine=true;
while (ptr.getLink() != null) {
//Here is logic to display output in 8 columns only
if(isFisrtLine)
{
if(x<7)
{System.out.print(ptr.getData()+ " ");
ptr = ptr.getLink();
x++;
}
else
{
System.out.print(" ");
isFisrtLine = false;
x=0;
}
}
else if(x<8)
{
System.out.print(ptr.getData()+ " ");
ptr = ptr.getLink();
x++;
}
else
{
System.out.print(" ");
x=0;
}
}
if(x==8)
System.out.println(" " + ptr.getData()+ " ");
else
System.out.print(ptr.getData()+ " ");
}
}
SortedListUsingLinkedList.java
import java.util.Scanner;
public class SortedListUsingLinkedList {
public static void main(String[] args)
{
int inputNumber=0;
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
IntSortedList list = new IntSortedList();
System.out.println("Sorted Integers using linked list ");
char ch;
/* Perform list operations */
do
{
System.out.println("1. INSERT ITEM");
System.out.println("2. DELETE ITEM");
System.out.println("3. IS EMPTY");
System.out.println("4. SIZE OF LIST");
System.out.println("5. EXIT");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integers elements and put negative value to end");
//list.insert( scan.nextInt() );
////////Here are lines for reading sequence of integers ended with negative number///////////////////
while (inputNumber>=0) {
inputNumber = scan.nextInt();
if(inputNumber>=0)
list.insert( inputNumber );
}
/////////////////////////////////////////////////////////////
break;
case 2 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position ");
else
list.deleteAtPos(p);
break;
case 3 :
System.out.println("Empty status = "+ list.isEmpty()+" ");
break;
case 4 :
System.out.println("Size = "+ list.getSize() +" ");
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* Display List */
list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}