Question: JAVA implemment the following: public boolean isSorted() : this method check if the elements in the list is in ascending order, return true if yes,
JAVA implemment the following:
public boolean isSorted(): this method check if the elements in the list is in ascending order, return true if yes, otherwise return false. An empty list is considered as sorted. public NumList divisibleBy(int x): Return a new NumList that contains all elements in the original list that are divisible by x. For example, if the list contains [1,2,3,4,5], then calling disibleBy(2) should return a new NumList containing [2, 4] public NumList sublist(int indexFrom): Return a new NumList containing all elements in original list whose index in the list is greater than or equals to indexFrom. For example, if the list contains [1,2,3], calling sublist(1) should return a new NumList that contains[2, 3] public NumList sublist(int indexFrom, int indexTo): Return a new NumList containing all elements in original list whose index in the list is greater than or equals to indexFrom but less than indexTo. For example, if the list contains [1,2,3], calling sublist(1,300000) should return a new NumList that contains[2, 3] public void sort(): Sort the list in ascending order
GIVEN___________
NumList.java
public class NumList { protected NumNode front; public NumList(){ front=null; } public void add(int val){ NumNode node=new NumNode(val); if(front==null) front=node; else{ NumNode tmp=front; while(tmp.getNext() !=null) tmp=tmp.getNext(); tmp.setNext(node); } } public int size(){ int count=0; NumNode tmp=front; while(tmp!=null){ tmp=tmp.getNext(); count++; } return count; } public boolean isEmpty(){ return front ==null; } public void remove(int index){ if(index=0){ if(size()==1) front=null; else{ int loc=0; NumNode tmp=front; while(loc=0) remove(index); } public int indexOf(int val){ int index=-1; NumNode tmp=front; int loc=-1; while(tmp!=null && index == -1){ loc++; if(tmp.getVal() == val){ index=loc; }else{ tmp=tmp.getNext(); } } return index; } public int indexOf(NumNode node){ return indexOf(node.getVal()); } public boolean contains(int val){ return indexOf(val) != -1; } public NumNode elementAt(int index){ if(index<0 || index>=size()) return null; int loc=0; NumNode tmp=front; while(loc0) tmp = tmp.substring(0, tmp.length()-2); return "["+tmp+"]"; } /*DO NOT CHANGE THE CODES ABOVE*\ public boolean isSorted() { //this method check if the elements in the list is in ascending order, return true if yes, otherwise return false. // An empty list is considered as sorted. } public NumList divisibleBy(int x) { //Return a new NumList that contains all elements in the original list that are divisible by x. // For example, if the list contains [1,2,3,4,5], then calling disibleBy(2) should return a new NumList containing [2, 4] } public NumList sublist(int indexFrom) { //Return a new NumList containing all elements in original list whose index in the list is greater than or equals to indexFrom. // For example, if the list contains [1,2,3], calling sublist(1) should return a new NumList that contains[2, 3] } public NumList sublist(int indexFrom, int indexTo) { //Return a new NumList containing all elements in original list whose index in the list is greater than or equals to indexFrom but less than indexTo. // For example, if the list contains [1,2,3], calling sublist(1,300000) should return a new NumList that contains[2, 3] } public void sort() { //Sort the list in ascending order } } NumNode.java
/*For refernce, no changes necessary*/ public class NumNode{ private int val; private NumNode next; public NumNode(int val){ setVal(val); next=null; } public int getVal(){ return val; } public NumNode getNext(){ return next; } public void setVal(int val){ this.val = val; } public void setNext(NumNode next){ this.next = next; } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
