1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
solmaris [256]
3 years ago
8

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() {...}
Computers and Technology
1 answer:
Advocard [28]3 years ago
7 0

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.
You might be interested in
Using a linear search to find a value that is stored in the last element of an array of 20,000 elements, ________ element(s) mus
Klio2033 [76]

Using a linear search to find a value that is stored in the last element of an array of 20,000 elements, 20,000 element(s) must be compared.

6 0
1 year ago
Which of the following is applicable during a hostile act:
Elanso [62]
Your question has been up too long, a moderator may have to delete it.
we will contact moderators to find your question and remove it, and hopefully, they will refund the points, which is not up to me.
<span>thank you for using brainly.</span>
3 0
2 years ago
As a member of the accounting group,
Anastasy [175]

Answer:

rtg

Explanation:

rth

3 0
3 years ago
Jason wants to set up his Wi-Fi’s security. He cannot install a central server for authentication. However, he wants to use a pr
jolli1 [7]

Answer:

Answer = a ig

wpa. ig ig ig ig

4 0
2 years ago
1.A panel that , when active , displays options for the selected tool. *
yKpoI14uk [10]
Is there a question or something
7 0
2 years ago
Read 2 more answers
Other questions:
  • An administrator has noticed that GPO containing new update settings has not yet applied to one of the computers on the network.
    5·1 answer
  • students at a camp can choose between boating and fishing in the morning and between hiking and horseback riding in the afternoo
    11·1 answer
  • Which type of financial institution typically has membership requirements?
    9·2 answers
  • Whenever Jim starts his laptop he sees some commands and numbers appearing on his screen these instructions are being processed
    9·1 answer
  • What is the primary responsibility of the federal reserve bank??
    11·1 answer
  • 7. Which cipher is based on the clues of the physical factors, rather than the hardware or a software cryptosystem
    8·1 answer
  • A musician has recorded some initial ideas for new songs which she wishes to share with her bandmates. As these are initial idea
    7·1 answer
  • Examples of how the development of coding changed the way we live. What type of technology was created as a result of code?
    10·1 answer
  • Jade has to create a workbook for storing information of students participating in the annual state-level sports competition. Th
    5·1 answer
  • You have been trying all day to check your direct messages on social media. The site is really slow to load and sometimes you ca
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!