Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For my Java program, my isInAscendingOrd() method (method to check if user input is in ascending order or not) is not working correctly. How can

For my Java program, my isInAscendingOrd() method (method to check if user input is in ascending order or not) is not working correctly.

How can I change below codes to make the method work correctly?

Example output

nput a list of integers: 5 9 101 183 4893

Your list of integers is in ascending order.

Do you want to continue (y/n): y

My output

Input a list of integers: 5 9 101 183 4893

Your list of integers is not ascending order. Do you want to continue (y/n): y

**Driver program******************************************

import java.util.*;

public class ToRunLab2 {

public static void main(String[] args) {

ListComparableListArrayBased aList = new ListComparableListArrayBased();

String[] numList;

char ans = 'y';

Scanner input = new Scanner(System.in);

while ( ans == 'y'){//while ans is y, go into below loop

do{//ask for user input

System.out.print(" "+"Input a list of integers: ");

String numbers = input.nextLine();

numList = numbers.split(" "); //separates input with spaces

for(int i=0; i

aList.add(i,numList[i]);

} while(!input.nextLine().isEmpty());//while input is empty, execute do loop

if(aList.isInAscendingOrd())//if the input is in ascending order, print out below

System.out.println("Your list of integers is in ascending order.");

else // else print out below

System.out.println("Your list of integers is not ascending order.");

System.out.print("Do you want to continue (y/n): " );//ask user to continue or not

ans = input.nextLine().charAt(0);

aList.removeAll();//remove all the input before going into the next input

}

}

}

**Classes******************************************

public class ListComparableListArrayBased

implements ComparableListInterface {

private static final int MAX_LIST = 50;

private Comparable items[];

private int numItems;

//constructor

public ListComparableListArrayBased(){

items = new Comparable[MAX_LIST];

numItems = 0;

}

public boolean isEmpty(){//check if value is empty or not

return (numItems == 0);

}

public int size(){

return numItems;

}

public void removeAll(){//remove all the values

items = new Comparable[MAX_LIST];

numItems = 0;

}

public void add(int index, Comparable item)//add all the values imputed

throws ListIndexOutOfBoundsException,

ListException{

if (numItems >= MAX_LIST){ // >= instead of > insert -1 after numItems

throw new ListException("ListException on add");

}//end if

if(index >=0 && index <=numItems){

for (int pos = numItems-1; pos >= index; pos--){

items[pos+1] = items[pos];

}//end for

items[index] = item;

numItems++;

}// end for

else{

throw new ListIndexOutOfBoundsException(

"ListIndexOutOfBoundsException on add");

}//end else

}

public T get(int index) throws ListIndexOutOfBoundsException{

if(index >= 0 && index < numItems){

return (T)items[index];

}//end if

else{

throw new ListIndexOutOfBoundsException(

"ListIndexOutOfBoundsException on get");

}//end else

}//end get method

public void remove(int index) throws ListIndexOutOfBoundsException{

if(index >= 0&& index

//correction on textbook code: < numItems instead of <= size done

for (int pos = index+1; pos

items[pos-1] = items[pos];

}//end for

numItems--;

}//end if

else {//index out of range

throw new ListIndexOutOfBoundsException(

"ListIndexOutOfBoundsException on remove");

}//end else

}//end remove

//compare if array is in ascending order by compare to

public boolean isInAscendingOrd(){

for(int i = 0; i < size()-1; i++)//check until i = size-1

{

if(size() > 0){ //if size is greater than 0, compare the first value to the 2nd one

if(items[i].toString().compareTo(items[i+1].toString()) < 0)

return false;

}

}

return true;

}

public int compareTo(Object obj){

return -1;

}

//end class

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions