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
______________________ can run on a workstation or server and is at the heart of all business applications.
Paladinen [302]

Answer:

I believe the correct answer is Application Software.

Explanation:

7 0
2 years ago
Describe how learning can be accessed in your class?​
SpyIntel [72]

There are four main learning styles used in classrooms today: Visual, auditory, reading/writing, and kinesthetic.

8 0
3 years ago
Gauthmath https://s.tutorus.xyz/lp/invite?code=GGQGBL​
sammy [17]

Answer:

Umm

Explanation:

3 0
2 years ago
Insecurely attached infants who are left my their mothers in an unfamiliar setting often will
Temka [501]

Insecurely attached infants who are left my their mothers in an unfamiliar setting often will Hold fast in their mothers in their return.

A. Hold fast in their mothers in their return

<u>Explanation:</u>

It is in the conscience of the infants to have the company of their parents no matter the place or time and do not generally like to be left alone. Moreover, the questions says, insecurely attached infants which further add up to this behavior.

The infant would not explore the surroundings due to lack of confidence in an unfamiliar setting. They would rather be uncomfortable and most probably weep the time until their mother arrives and hold fast on to them on their return.  

8 0
3 years ago
Write a program that will ask user to enter your name and count number of character ?
Svet_ta [14]

Answer:

672

Explanation:

because

8 0
3 years ago
Other questions:
  • Univariate linear regression Note: Solutions to this problem must follow the method described in class and the linear regression
    7·1 answer
  • The color band that represents tolerance on a resistor is the?
    11·1 answer
  • PLEASE  HELPPPP!!!!!
    14·1 answer
  • Type the correct answer in the box. Spell all words correctly.
    12·1 answer
  • Assume you have a int variable n that has already been declared and initialized. Its value is the number of integers that need t
    13·1 answer
  • Nonverbal communication includes _____.
    9·2 answers
  • Can a idler gear increase or decrease torque?
    7·1 answer
  • What is the definition of trouble shooting.
    12·1 answer
  • Write a class called TextProcessor that implements the methods listed below. Your implementation may use the charAt() and length
    14·1 answer
  • Write a program that inputs numbers and keeps a running sum. When the sum is greater than 100, output the sum as well as the cou
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!