Question

In: Computer Science

This is based on LinkedLists. Avneet Pandey, please do not answer this. Both your previous answers...

This is based on LinkedLists. Avneet Pandey, please do not answer this. Both your previous answers were wrong.

Please complete the methods max() and threshold(). I'd greatly appreciate it. There is a type mismatch in my first method and I dont know how to get around it. I've commented on the line with the type mismatch. Please write a correct method in the answer so I can compare it to my wrong method

public class Node {
   public T info;
public Node link;
public Node(T i, Node l) {
   info = i; link = l;
   }
}

class LinkedList> {
  
protected Node head = null;
public LinkedList add(T el) {
head = new Node(el, head);
return this;
}
public void print() {
for(Node node = head; node!=null; node=node.link) {
System.out.print(node.info+" ");
}
System.out.println("");
}
  
public T maxValue() { // Note: Recursive methods not allowed, using new key word not allowed, no iterators allowed
   if (head == null) {
   return null;
   }
   else {
   Node temp = head;
   if (temp.link == null) {
   return temp.info;
   }
   else {
   T max = temp.link; //type mismatch
   if (temp.info.compareTo(max) > 0)
   max = temp.info;
   return max;
   }
}
}
  
public void threshold(T thres) {//Note: Recursive methods not allowed, using new key word not allowed, no iterators allowed

}

public static void main(String args[]) {
  
LinkedList list = new LinkedList();
System.out.println(list.maxValue()); // should be null
list.add(20).add(30).add(10);
System.out.println(list.maxValue()); // should be 30
list.threshold(40);
list.print(); // should print out all elements
list.threshold(30);
list.print(); // should print out 10 20
list.threshold(10);
list.print(); // should print nothing
}
}

Solutions

Expert Solution

For max function just one line needed to be changed:

public T maxValue() { // Note: Recursive methods not allowed, using new key word not allowed, no iterators allowed
    if (head == null) {
        return null;
    }
    else {
        Node temp = head;
        if (temp.link == null) {
            return temp.info;
        }
        else {
        Node max = temp.link; //type mismatch was because link of Node is of Node type only and you tried to assign it to T type
        if (temp.info.compareTo(max) > 0)
            max = temp.info;
        return max;
        }
     }
}

For threshold function. Appropriate comments have been put to explain the code


 public void threshold(T thres) {//Note: Recursive methods not allowed, using new key word not allowed, no iterators allowed
    // if no elements in list
    if(head == null)
        return;
    //to iterate through the list
    Node prev = head; 
    Node cur  = prev.link;
    while(cur != null){
        //when threshold value is reached
        if(cur.info.compareTo(thres) >=0){
            prev.link = cur.link; //Skip the current node
            cur = cur.link;
        }
    }
    // check if fist element is greater than threshold
    if(head.info.compareTo(thres)>=0)
        head = head.link;
        
 }

Related Solutions

Can you please answer the questions as well as checking my previous answers to see if...
Can you please answer the questions as well as checking my previous answers to see if ti correct You love the new elliptical machines your gym added several months ago, but because they are so popular with other members too, you usually have to wait for a machine to become available. Out of curiosity, each day you write down how many minutes you have to wait for an elliptical machine. Using a confidence level of 80%, is the average wait...
Please do not plagiarize or use a previous posted answer!! What is the purpose of the...
Please do not plagiarize or use a previous posted answer!! What is the purpose of the business? Does this answer depend upon whether or not you are the business owner, an employee of the business, or simply someone affected by the business in some manner? Why or why not? 250 words
please be DETAILED with the answers! this is a ruminant nutrition question do not answer if...
please be DETAILED with the answers! this is a ruminant nutrition question do not answer if unsure A complete dairy diet contains 55% alfalfa, 40% corn and 5% cotton seed meal. List all of the chemical compounds in the diet (e.g. starch). For each compound you have listed, describe the fermentation process in the rumen. For each compound identify what % is fermented in the rumen and what % is digested in the intestine
Answer true or false to the following questions please explain your answers and answer all please  ...
Answer true or false to the following questions please explain your answers and answer all please   1.The cytoplasmatic side of the integral membrane proteins is often glycosylated. 2. Cholesterol is more enriched at the outer leaflet of the plasma membrana 3.Inner leaflet of the plasma membrane is enriched with glycolipids. 4.Membrane proteins that pomp ions in and out of the cell have specific Km values. 5. A symport would function as an antiport if its orientation in the membrane were...
Please note that the previous answers were blurred, so can you please make the answers readable....
Please note that the previous answers were blurred, so can you please make the answers readable. Thanks so much, i really appreciate your help. God bless. Question: Elliot Karlin is a 35-year-old bank executive who has just inherited a large sum of money. Having spent several years in the bank's investments department, he's well aware of the concept of duration and decides to apply it to his bond portfolio. In particular, Elliot intends to use $1million of his inheritance to...
The title of the course is OS. All answers should be based on that. Please do...
The title of the course is OS. All answers should be based on that. Please do not copy and paste answers on chegg or on google for me. All answers should be based on your understanding on the course. Please try as much to answer the questions based on what is asked and not setting your own questions and answering them. Let it be if you want copy and paste answers. ********************************************************************************************************************************************************************************************************************** (1) Operating systems protect the computer hardware and...
#2 **I POST THIS TWICE, DO NOT DUPLICATE YOUR ANSWER TO BOTH POST PLEASE, THANKS The...
#2 **I POST THIS TWICE, DO NOT DUPLICATE YOUR ANSWER TO BOTH POST PLEASE, THANKS The Ethical Dilemma of Designer Babies What to do: Please watch the TED Talk The Ethical Dilemma of Designer Babies Links:https://www.ted.com/talks/paul_knoepfler_the_ethical_dilemma_of_designer_babies and post you find most disturbing or intriguing about the thought of tinkering with humans.
Please do not use any article that were used in previous answers. Thank you When dealing...
Please do not use any article that were used in previous answers. Thank you When dealing with issues such as professional ethics, the stakes can be high. This is why such care is taken to painstakingly clarify terms such as integrity and independence in the AICPA Professional Code of Conduct, as they could otherwise be open to interpretation. In this week's discussion, you will find illustrative examples of these key principles to share and discuss with your peers. First, review...
Case Study: The Impact of Covid-19 on Event Cinemas (Please DO NOT COPY the PREVIOUS ANSWER)!!...
Case Study: The Impact of Covid-19 on Event Cinemas (Please DO NOT COPY the PREVIOUS ANSWER)!! Event Cinemas plans to make staff redundant as it copes with the impact of Covid-19 on its business. It said it had been affected by: the lockdown, changes in the future film line-up; the impact of coronavirus and the public health response to the pandemic. It was starting a consultation process with staff and unions to reduce its workforce. Unite Union said the cinema...
Please compelete long detailed answer and be specific please draw graphs with your answers of available,...
Please compelete long detailed answer and be specific please draw graphs with your answers of available, but they are not required. If you use graph, you can also explain what information you want me to get from the graph. Please Limit all answers upto words of 300. Suppose the American Medical Association (AMA) wants to improve the quality of physicians. Give TWO examples of how they could change their licensing policies to accomplish this. Using moral hazard as the main...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT