Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java A frog object is defined wtih two values name and value. These two peices o

ID: 3583292 • Letter: J

Question

Java

A frog object is defined wtih two values name and value. These two peices of information are used as the search criteria for finding a frog. The frog object also has an id that is not used in search, but is helpful to keep track of which frog is which. Implement functionality to perform a search on a singly linked list and return the correct results of a query. Any null references for frogs are skipped over. The list can contain duplicates. You are already provided with a minimal Linked List implementation and the definition of a Frog object. Please see them before answering the question.

Frog.java (GIVEN CLASS);

public class Frog
{
   private static int frogid = 0;
   private String name;
   private int value;
   private int id;
  
   public Frog(String name, int v) { this.name = name; value = v; init(); }
   private void init() { id = frogid++; }

   // clone
   public Frog(Frog f) {
       value = f.getValue();
       name = f.getName();
       id = getID();
   }
  
   public String getName() { return name; }
   public int getID() { return id; }
   public int getValue() { return value; }
  
   public void ribbit() {
       System.out.printf("id: %d name: %s value: %d ",
           getID(), getName(), getValue());
       for (int i = 0; i < getValue(); i++)
           System.out.printf("ribbit");
       System.out.println("");
   }
}

FrogNode.java (GIVEN CLASS):

/*
* Minimal frog list
*/
public class FrogNode
{
   public Frog frog;
   public FrogNode next;

   public FrogNode(Frog f) {
       frog = f;
   }
  
   // adds the node n after the current. If the node n has futher links they are removed
   public void addAfter(FrogNode n) {
       if (next != null)
           n.next = next;
       next = n;
   }
}

FrogSearch.java (WHAT WE NEED TO IMPLEMENT):

/*
* Implement basic Linked list functionality to perform a search and return the
* correct results of a query.
* any null references for frogs are skipped over
*/
public class FrogSearch
{  
   /* searches list for the all frogs with the same value found
   * returns results as a **new Linked List**. The new linked list is ordered
   * in reverse of when they are found.
   * e.g. search for value 3
   * input ("Froggy", 3), ("Toad", 2), ("Slimebiscuit", 3)
   * output ("Slimebiscuit", 3) ("Froggy", 3)
   */
   public static FrogNode search(FrogNode node, int value) {

       //////////////////////
       // //
       // your code here! //
       // //
       //////////////////////

   }
  
   /* returns true/false if at least one frog with the value is in list
   */
   public static boolean foundInList(FrogNode node, int value)
   {

       //////////////////////
       // //
       // your code here! //
       // //
       //////////////////////

   }

   /* returns true/false if the frog object f appears in the list at
   * least once (matching id)
   */
   public static boolean foundInList(FrogNode node, Frog f)
   {  

       //////////////////////
       // //
       // your code here! //
       // //
       //////////////////////

   }
  
   /* returns true/false if the values of the given frog f match exactly with
   * any frog (name and value) which is in the list.
   */
   public static boolean foundSimilarInList(FrogNode node, Frog f)
   {  

       //////////////////////
       // //
       // your code here! //
       // //
       //////////////////////

   }

}

Explanation / Answer

PROGRAM CODE:

package samplepro;

/*
* Implement basic Linked list functionality to perform a search and return the
* correct results of a query.
* any null references for frogs are skipped over
*/
public class FrogSearch
{
/* searches list for the all frogs with the same value found
* returns results as a **new Linked List**. The new linked list is ordered
* in reverse of when they are found.
* e.g. search for value 3
* input ("Froggy", 3), ("Toad", 2), ("Slimebiscuit", 3)
* output ("Slimebiscuit", 3) ("Froggy", 3)
*/
public static FrogNode search(FrogNode node, int value) {
   FrogNode newNode = null;
   while(node != null)
   {
       Frog f = node.frog;
       if(f.getValue() == value)
       {
           if(newNode == null)
           {
               newNode = new FrogNode(f);
           }
           else {
               FrogNode f_node = new FrogNode(f);
               f_node.addAfter(newNode);
           }
       }
       node = node.next;
   }
   return newNode;
  
}
  
/* returns true/false if at least one frog with the value is in list
*/
public static boolean foundInList(FrogNode node, int value)
{
   while(node != null)
   {
       if(node.frog.getValue() == value)
           return true;
       else
           node = node.next;
   }
return false;
}
/* returns true/false if the frog object f appears in the list at
* least once (matching id)
*/
public static boolean foundInList(FrogNode node, Frog f)
{
   while(node != null)
   {
       if(node.frog.getID() == f.getID())
           return true;
       else
           node = node.next;
   }
return false;
  
}
  
/* returns true/false if the values of the given frog f match exactly with
* any frog (name and value) which is in the list.
*/
public static boolean foundSimilarInList(FrogNode node, Frog f)
{
   while(node != null)
   {
       if(node.frog.getName().equals(f.getName()) && node.frog.getValue() == f.getValue())
           return true;
       else
           node = node.next;
   }
return false;
  
}

public static void main(String args[])
{
   Frog f = new Frog("Froggy", 3);
   Frog f1 = new Frog("Toad", 2);
   FrogNode node = new FrogNode(f);
   FrogNode node1 = new FrogNode(f1);
   node1.addAfter(node);
   System.out.println(foundInList(node1, f));
   System.out.println(search(node1, 3).frog.getName());
   System.out.println(foundInList(node1, 2));
   System.out.println(foundSimilarInList(node1, f1));
   System.out.println(foundInList(node1, 4));
}
}

OUTPUT:

true
Froggy
true
true
false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote