Java A frog object is defined wtih two values name and value. These two peices o
ID: 3583291 • 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
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("");
}
}
/*
* 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;
}
}
/*
* 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 current = node;
FrogNode fnew =new FrogNode(null);
while (current.next != null) {
Frog f = current.frog;
if (f.getValue() == value) {
fnew.addAfter(fnew);
}
current = current.next;
}
return fnew;
}
/* returns true/false if at least one frog with the value is in list
*/
public static boolean foundInList(FrogNode node, int value) {
FrogNode current = node;
while (current.next != null) {
Frog f = current.frog;
if (f.getValue() == value) {
return true;
}
current = current.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) {
FrogNode current = node;
while (current.next != null) {
Frog fnew = current.frog;
if (fnew.getID() == f.getID()) {
return true;
}
current = current.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) {
FrogNode current = node;
while (current.next != null) {
Frog fnew = current.frog;
if (fnew.getValue() == f.getValue() && fnew.getName().equals(f.getName())) {
return true;
}
current = current.next;
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.