Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking
public static void replace( List aList, int newValue, int position)
public class ArraybasedList implements MyListInterface{
private static final int DEFAULTSIZE = 25;
// Data members:
private int currentSize;
private int maxSize;
private S[] elements;
//default constructor has NO parameters
public ArraybasedList(){
this.currentSize =0;
this.maxSize = DEFAULTSIZE;
this.elements = (S[]) new Object[maxSize];
}
// non-default constructor
public ArraybasedList(int maxElements) {
if (maxElements <= 0)
throw new ListException("The size of the list must be > zero.");
this.currentSize =0; // set the current size to zero
this.maxSize = maxElements; // store the size in maxSize
// allocate storage for elements
this.elements = (S[] ) new Object[this.maxSize];
}
// begin implementing methods
/**
* This method returns true if the current
* size of the list is zero.
*
*
*
*/
public boolean isEmpty() {
return ( this.currentSize == 0);
}
/**
* This method returns true if the current
* size of the list equals the maximum size
* of the list.
*
*
*
*/
public boolean isFull(){
return ( this.currentSize == this.maxSize);
}
/**
* This method returns the maximum number
* of elements the list can hold.
*
*
*
*/
public int getMaxSize(){
return this.maxSize;
}
/**
* This method returns the current number
* of elements in the list.
*
*
*
*/
public int size(){
return this.currentSize;
}
/**
* This method inserts the value at the given position.
*
*
* @param position location where new value is to be inserted, 0<=position<=current size
* @param value new value to be added to the list
*
*/
public void add( int position, S value){
//� If the list is full EROR
//� if the position given is < 0 OR > current size, ERROR
//� If position given equals current size, add new element to the end
//� Otherwise, shift all elements whose position is equal to or greater than the given logical position, forward (up) on position in the list and add the new value
if (this.isFull())
throw new ListException("Invalid operation, can't add to a full list");
if (position < 0 || position > this.currentSize)
throw new ListException("Invalid operation, position must be between 0 and " + this.currentSize);
if (position == this.currentSize)
this.elements[currentSize] = value;
else{
for( int i= this.currentSize-1; i >= position; i--)
this.elements[i+1] = this.elements[i];
this.elements[position] = value;
}
this.currentSize++;
}// end add at position
/**
* This method adds a new value to the end of a list.
*
*
* @param value new value to be added to the list
*
*/
public void add( S value){
this.add( this.currentSize, value);
}// add at end
public S remove(int position) {
// Precondition: The is list not empty.
// Precondition: 0<= position <= current size-1 of list
S tempValue;
if ( this.isEmpty() || position > currentSize-1 || position < 0 )
throw new ListException("This delete can not be performed "
+ "an element at position " + position
+ " does not exist " );
//1. Remove the desired value, and shift elements
// Store the value in the list to be deleted
// NOTE: it must be cast to type S
tempValue = (S)this.elements[position];
// Shift existing elements down one position
// The logical position, is translated to the physical location of
// position -1
for( int i = position; i < this.currentSize-1; i++)
this.elements[i] = this.elements[i+1];
//2. Decrement the element count.
// Reinitialize the �old� end element to null, and decrement element count
this.elements[currentSize-1] = null;
currentSize--;
return tempValue;
} // end remove
/**
* This method removes all occurrences of elements in the list argument from the list object
*
*
*
*
* */
public boolean removeAll(MyListInterface list){
// iterate through the list parameter removing each occurrence of
// the values it contains
boolean result=false;
return result;
}
/**
* This method returns the value at a specific
* position in the list.
*
*
* @param position: location of element to return 0<=position
List interface:
public interface MyListInterface<S>{
/**
* This method returns true if the current
* size of the list is zero.
*
*
*
*/
public boolean isEmpty();
/**
* This method returns true if the current
* size of the list equals the maximum size
* of the list.
*
*
*
*/
public boolean isFull();
/**
* This method returns the maximum number
* of elements the list can hold.
*
*
*
*/
public int getMaxSize();
/**
* This method returns the current number
* of elements in the list.
*
*
*
*/
public int size();
/**
* This method searches the list for the
* specified value and returns the index
* number of the first element containing
* the value or -1 if the value is
* not found.
*
*
* @param value: the search value
* @return index of element containing value or -1
*
*/
public int find( S value);
/**
* This method returns the value at a specific
* position in the list.
*
*
* @param position: location of element to return 0<=position<current size
*
*/
public S get( int position);
/**
* This method inserts the value at the given position.
*
*
* @param position location where new value is to be inserted, 0<=position<=current size
* @param value new value to be added to the list
*
*/
public void add( int position, S value);
/**
* This method adds a new value to the end of a list.
*
*
* @param value new value to be added to the list
*
*/
public void add( S value);
/**
* This method removes all occurrences of elements in the list argument from the list
*
*
*
*
* */
public boolean removeAll(MyListInterface<? extends S> list);
/**
* This method removes and returns the value at position. 0<= position < currentSize
*
*
*
*
* */
public S remove(int position);
/**
* This method deletes all of the list's contents.
*
*
*
*/
public void clear();
/**
* This method display the contents of the list
*
*
*
*/
public String toString();
}In: Computer Science
An experiment is to flip a coin until a head appears for the first time. Assume the coin may be biased, i.e., assume that the probability the coin turns up heads on a flip is a constant p (0 < p < 1). Let X be the random variable that counts the number of flips needed to see the first head.
(a) Let k ≥ 1 be an integer. Compute the probability mass function (pmf) p(k) = P(X = k).
(b) If p = 1/3 compute P(2 ≤ X < 4) and P(1 < X < 3).
(c) If p = 1/3 compute P(X > 2).
(d) If p = 1/2 compute P(X is even).
In: Statistics and Probability
A start-up company has 2000 investors, that company loses investors at a rate of 10 per year. Every time the company loses an investor, the company gets a loss of $200,000. For every investor that remains the company makes a profit of $2,000. Let F be the total earnings the company makes in a year, and X be the number of investors the company loses.
1)Write a function that calculates yearly earnings F as a function of X
2)Find P(F < 0), the probability that earnings are negative
3)E[F]
4)What is the probability that the company loses exactly 5 investors in a given year, given that they have not lost any investors in the first half of the year
In: Statistics and Probability
A leading magazine (like Barron's) reported at one time that the average number of weeks an individual is unemployed is 27 weeks. Assume that for the population of all unemployed individuals the population mean length of unemployment is 27 weeks and that the population standard deviation is 9 weeks. Suppose you would like to select a random sample of 32 unemployed individuals for a follow-up study.
Find the probability that a single randomly selected value is less than 28. P(X < 28) =
Find the probability that a sample of size n = 32 is randomly selected with a mean less than 28. P(M < 28) =
Enter your answers as numbers accurate to 4 decimal places.
In: Statistics and Probability
In: Statistics and Probability
In: Statistics and Probability
There is an urn containing 9 balls, which can be either green or red. Let X be the number of red balls in the urn, which is unknown. As a prior, assume that all possible values of X from 0 to 9 are equally likely.
(a) One ball is drawn at random from the urn, and it is red. Compute the Bayes box to give the posterior probability distribution of X. Calculate the posterior mean of X.
(b) Suppose that a second ball is drawn from the urn, without replacing the first, and it is green. Use the posterior distribution of X from part a) as the prior distribution for X and compute the Bayes box and to give the posterior probability distribution of X. Calculate the mean of X.
In: Statistics and Probability
A leading magazine (like Barron's) reported at one time that the
average number of weeks an individual is unemployed is 33 weeks.
Assume that for the population of all unemployed individuals the
population mean length of unemployment is 33 weeks and that the
population standard deviation is 3 weeks. Suppose you would like to
select a random sample of 35 unemployed individuals for a follow-up
study. Find the probability that a single randomly selected value
is less than 34. P(X < 34) = Find the probability that a sample
of size n = 35 is randomly selected with a mean less than 34. P( ¯
x < 34) =
Enter your answers as numbers accurate to 4 decimal places.
In: Statistics and Probability
A manufacturer knows that their items have a normally
distributed lifespan, with a mean of 3.7 years, and standard
deviation of 1.1 years. Round all answers to 3 decimals.
(a) If you randomly purchase one item, what is the probability it
will last longer than 4 years?
Show your work. Show me what you entered into the calculator.
(b) If you randomly select one item, what is the probability it
will last less than 5 years?
Show your work. Show me what you entered into the calculator.
(c) Find the number of years that corresponds to the 46 th
percentile.
Show your work. Show me what you entered into the calculator.
In: Statistics and Probability
When a new machine is functioning properly, only 3% of the items produced are defective. Assume that we will randomly select ten parts produced on the machine and that we are interested in the number of defective parts found. Compute the probability associated with:
a. [4 points] No defective parts
b. [6 points] At least 1 defective parts.
2. [12 points] Over 500 million tweets are sent per day (Digital Marketing Ramblings website, December 15, 2014). Bob receives on average 9 tweets during his lunch hour. What is the probability that Bob receives no tweets during the first 15 minutes of his lunch hour?
In: Statistics and Probability