Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help Write a complete method to find the range of numbers contained in a list. The range is defined as number of numbers between

Please help

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. There are examples below.

The method header is: public int findRange(ListInterface 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 can also refer to BagInterface and ListInterface Files for the full details about the list.

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: the minimum value is 1 and the maximum value is 9; there are 9 numbers between 1 and 9 (including the 1 and 9)

list (4, 2, -1, 6, 8, 3): range is 10: the minimum value is -1 and the maximum value is 8; there are 10 numbers between -1 and 8 (including the -1 and 8)

list (4): range is 1: the minimum value is 4 and the maximum value is 4; this is just 1 number

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()

/** An interface for the ADT list. Entries in a list have positions that begin with 1.

@author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public interface ListInterface { /** Adds a new entry to the end of this list. Entries currently in the list are unaffected. The list's size is increased by 1. @param newEntry The object to be added as a new entry. */ public void add(T newEntry); /** Adds a new entry at a specified position within this list. Entries originally at and above the specified position are at the next higher position within the list. The list's size is increased by 1. @param newPosition An integer that specifies the desired position of the new entry. @param newEntry The object to be added as a new entry. @throws IndexOutOfBoundsException if either newPosition < 1 or newPosition > getLength() + 1. */ public void add(int newPosition, T newEntry); /** Removes the entry at a given position from this list. Entries originally at positions higher than the given position are at the next lower position within the list, and the list's size is decreased by 1. @param givenPosition An integer that indicates the position of the entry to be removed. @return A reference to the removed entry. @throws IndexOutOfBoundsException if either givenPosition < 1 or givenPosition > getLength(). */ public T remove(int givenPosition); /** Removes all entries from this list. */ public void clear(); /** Replaces the entry at a given position in this list. @param givenPosition An integer that indicates the position of the entry to be replaced. @param newEntry The object that will replace the entry at the position givenPosition. @return The original entry that was replaced. @throws IndexOutOfBoundsException if either givenPosition < 1 or givenPosition > getLength(). */ public T replace(int givenPosition, T newEntry); /** Retrieves the entry at a given position in this list. @param givenPosition An integer that indicates the position of the desired entry. @return A reference to the indicated entry. @throws IndexOutOfBoundsException if either givenPosition < 1 or givenPosition > getLength(). */ public T getEntry(int givenPosition); /** Retrieves all entries that are in this list in the order in which they occur in the list. @return A newly allocated array of all the entries in the list. If the list is empty, the returned array is empty. */ public T[] toArray(); /** Sees whether this list contains a given entry. @param anEntry The object that is the desired entry. @return True if the list contains anEntry, or false if not. */ public boolean contains(T anEntry); /** Gets the length of this list. @return The integer number of entries currently in the list. */ public int getLength(); /** Sees whether this list is empty. @return True if the list is empty, or false if not. */ public boolean isEmpty(); } // end ListInterface

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

Step: 3

blur-text-image

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

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions

Question

what is a peer Group? Importance?

Answered: 1 week ago