Suppose that you are asked to modify the Stack class to add a new operation max() that returns the current maximum of the stack comparable objects. Assume that pop and push operations are currently implemented using array a as follows, where item is a String and n is the size of the stack. Note: if x andy are objects of the same type, use x.compareTo(y) to compare the objects x and y public void push String item ) { [n++] = iten; } public String pop { return al--n]; } Implement the max operation in two ways, by writing a new method using array a (in 8.1), or updating push and pop methods to track max as the stack is changed (in 8.2). Q8.1 Implement method maxi 5 Points Write a method max() using Out) space and Oin) running time. public String max() {...} Enter your answer here Q8.2 Update push() and popo 5 Points Write a method max() using On) space and 011) run time. You may update the push and pop methods as needed public void push {...} public String pop() {...} public String max() {...}

Answers

Answer 1

Answer:

Following are the code to the given points:

Explanation:

For point 8.1:

public String max()//defining a method max

{

   String maxVal=null;//defining a string variable that holds a value

   for(int x=0;x<n;x++)

   {

       if(maxVal==null || a[i].compareTo(maxVal)>0)//defining if blok to comare the value

       {

           maxVal=a[i];//holding value in maxVal variable

       }

   }

   return maxVal;//return maxVal variable value

}

For point 8.2:

public void push(String item)//defining a method push that accepts item value in a parameter

{

       a[n]=item;//defining an array to hold item value

       if(n==0 || item.compareTo(maxVals[n-1])>0)//use if to comare item value

       {

               maxVals[n]=item;//holding item value in maxVals variable

       }

       else

       {

               maxVals[n]=maxVals[n-1];//decreasing the maxVals value

       }

       n++;//incrementing n value

}

public String pop()//defining a method pop

{

       return a[--n];//use return value

}

public String max()//defining a method max

{

       return maxVals[n-1];//return max value

}

In the first point, the max method is declared that compares the string and returns its max value.In the second point, the push, pop, and max method are declared that works with their respective names like insert, remove and find max and after that, they return its value.

Related Questions

Multiply the following two Tom's Tiny floating-point format numbers (8-bit: sign bit, 3-bit 2's complement exponent, 4-bit fraction). Make sure to correctly round the result before putting the answer back in the 8-bit format.

A = 10010011
B = 10101000
AxB = _______

Answers

Answer:

A*B =  00111100

Explanation:

Given data :

A = 10010011

B = 10101000

Find A * B

For A = 10010011  express in sign bit , exponent and number

repeat same process for B

attached below is a detailed solution

A*B =  00111100

Whatever programming language you use will ultimately need to be translated into binary in order for the computer to understand it.

a. True
b. False

Answers

Answer:

Javascript:

function translate(what) {

if (what) {

return 1;

} else if (!what) {

return 0;

}

}

translate(true);

Explanation:

True is 1;

False is 0;

In the following cell, we've loaded the text of Pride and Prejudice by Jane Austen, split it into individual words, and stored these words in an array p_and_p_words. Using a for loop, assign longer_than_five to the number of words in the novel that are more than 5 letters long. Hint: You can find the number of letters in a word with the len function.

Answers

Answer:

Explanation:

Since the array is not provided, I created a Python function that takes in the array and loops through it counting all of the words that are longer than 5. Then it returns the variable longer_than_five. To test this function I created an array of words based on the synapse of Pride and Prejudice. The output can be seen in the attached picture below.

def countWords(p_and_p_words):

   longer_than_five = 0

   for word in p_and_p_words:

       if len(word) > 5:

           longer_than_five += 1

   return longer_than_five

what are the events?

Answers

Answer:

a thing that happens or takes place, especially one of importance.

Other Questions
Why are physical and behavioral adaptations important for a sunflowers survival? I am a 3-digit number divisible by 7, but not 2. The sum of my digits is 4. Hint it is not 121 Write the point-slope form of an equation of the line through the points (-7, 7) and (4, 1).A. y4=611(x1)B. y+1=611(x+4)C. y+7=611(x7)D. y7=611(x+7) Helppppppp what's the answer?????? The two main groupings of nations during World War II were the Allied Powers and the Axis Powers True / False Indoor air pollution has increased as improvements have been made inA) heating and air-conditioning systemsB) radon-resistant construction techniquesC) energy conservation in the homeD) green building constructionE) all of these choices contribute to indoor air pollution Tahari is shopping for a new car. She is making her decision based on brand, color, and style. She can choose from 4 brands, 5 colors, and 3 styles. First, Tahari chooses the color. Which explains the number of options that she has left after she chooses the color? Tahari has 60 options left because Tahari has 12 options left because she has options left. Tahari has 12 options left because . Tahari has 7 options left because she has options left. Evaluate the expression when a = -4 and x=4.4x-a HELPPPPP PLSSSS!!!!!! Using the picture above Find the measure of arc EF Self regulation is also known as How is Cuba's government different from that of the Dominican Republic?OA.Cuba is a communist dictatorship, the Dominican Republic is a democracy.Cuba has a stable government, the Dominican Republic has an unstable government.C. Cuba has a democracy, the Dominican Republic has a monarchy.OD. Cuba is independent; the Dominican Republic is ruled by a European country. 21. All the following are differences between arteries and veinsexceptarteries are thick and veins are thinO arteries do not have valves and veins have valvesarteries connect to capillaries and veins do not connect to capillariesarteries carry blood away from the heart and veins carry blood back to the heart22. All the following are the 3 major parts of the cardiovascular systemVA Which describes the relationship between EG and DF 2 Select all that apply.tyFE21o24AperpendicularB bisect each otherC. parallelD. congruent Atoms in group 18 are inert (chemicalally unreactive) because what is 7/3 because it wont work for me pls help What political impact does racism have in the U.S? In The Outsiders, what kind of place does johnny long for? Make a research paper about Susan. B Anthony. Ill mark you brainiest which of the following is an example of an informational text?a. A magazine article about how to build your own solar ovenb. A story about human colonists on Venusc. A story about a teenage girl who plans to run a marathond. An article about what dogs might say if they could talk