Java Write a complete method to find the range of numbers contained in a list. T
ID: 3581294 • Letter: J
Question
Java
Write a complete method to find the range of numbers contained in a list. The range is defined as number of numbers between the minimum and maximum, including both the minimum and maximum.
The method header is: public int findRange(ListInterface<Integer> list)
The list should not be altered when the method completes.
Use only the methods listed below from ListInterface.
Note that toArray is not listed and should not be used in your solution.
You are writing code at the client level, which means you do not know how the list is implemented.
Here are some examples:
list (1, 3, 6, 2, 3, 4, 9): range is 9
list (4, 2, -1, 6, 8, 3): range is 10
list (4): range is 1
the range of an empty list is 0
ListInterface methods:
public boolean add(T newEntry)
public boolean add(int newPosition, T newEntry)
public boolean isEmpty()
public boolean contains(T anObject)
public boolean remove(int givenPosition)
public boolean replace(int givenPosition, T newEntry)
public T getEntry(int givenPosition)
public int getLength()
public void clear()
Explanation / Answer
public void findRange(ListInterface<Integer> list)
{
int ln = list.getLength();
if(ln==0)
{
return 0;
}
int mn,mx,tmp;
mn = list.getEntry(0);
mx=mn;
for(int i=1;i<ln;i++)
{
tmp = list.getEntry(i);
if(mx<tmp)
{
mx=tmp;
}
if(mn>tmp)
{
mn=tmp;
}
}
return mx-mn+1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.