Question

Need a help with this one FoodListInterface.java specifies a Java interface for an ADT that maintains...

Need a help with this one

FoodListInterface.java specifies a Java interface for an ADT that maintains a list of foods, dishes or meals. This interface includes two operations: add(String food) – add the name of a food, dish or meal to the list onTheMenu(String food) – check if a given food item is on the list Develop three implementations of this interface, each with a different underlying data structure: Array Linked list ArrayList from the java.util package (a Java Collections Framework class) Each implementation is a separate class, and each of these classes is to include a toString() method to list out the food items, one per line. The source code for the interface is provided in the foodList package in the attached Eclipse project export file for this assignment. The linked list implementation must use the LLStringNode.java class provided in the foodList package; do not use the LinkedList class from the java.util package. To earn full credit you must make use of appropriate ArrayList methods to efficiently develop the food list ADT based on that data structure, along with an enhanced for loop to traverse the list of foods for the toString() method. In addition to the three ADT classes, create a single Java program – i.e., a Java class with a main() method – to test each of the implementations. For each implementation: Instantiate a list object and add seven of your favorite things to eat; Using System.out.println(), display the list of food items; Use the onThisList() method to test whether fettuccine alfredo is on your list, and report out the result; and Use the onThisList() method to test another food, and report out that result – make sure to use a food, dish or meal that produces the opposite result from the first test. Submit the following items:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

// FoodListInterface.java

/**

* interface to represent a system to store a list of food and check if a given

* food is present on the list

*/

public interface FoodListInterface {

              public void add(String food); // add the name of a food, dish or meal to the

                                                                                                                           // list

              public boolean onTheMenu(String food); // check if a given food item is on

                                                                                                                                                      // the list

}

// FoodListArray.java

/**

* implementation of FoodListInterface using an Array as underlying data

* structure

*/

public class FoodListArray implements FoodListInterface {

              // array to store foods

              private String foods[];

              // current number of foods in the array

              private int count;

              // default constructor, initializes empty food list

              public FoodListArray() {

                           // using default initial capacity of array

                           foods = new String[10];

                           // setting current number of foods to 0

                           count = 0;

              }

              @Override

              public void add(String food) {

                           // if array is full,doubling its size

                           if (count == foods.length) {

                                         // creating a new array of double capacity, copying all elements and

                                         // replacing old array

                                         String newArray[] = new String[count * 2];

                                         System.arraycopy(foods, 0, newArray, 0, count);

                                         foods = newArray;

                           }

                           // adding to end

                           foods[count] = food;

                           count++;

              }

              @Override

              public boolean onTheMenu(String food) {

                           // looping through array and checking if any object matches the given

                           // food

                           for (int i = 0; i < count; i++) {

                                         if (foods[i].equalsIgnoreCase(food)) {

                                                       // found

                                                       return true;

                                         }

                           }

                           // not found

                           return false;

              }

              @Override

              public String toString() {

                           String str = "[";

                           // appending all food data to a String variable enclosed within square

                           // braces

                           for (int i = 0; i < count; i++) {

                                         str += foods[i];

                                         if (i != count - 1) {

                                                       str += ", ";

                                         }

                           }

                           str += "]";

                           return str;

              }

}

// FoodListArrayList.java

import java.util.ArrayList;

/**

* implementation of FoodListInterface using an ArrayList as underlying data

* structure

*/

public class FoodListArrayList implements FoodListInterface {

              //array list of String to represent foods

              private ArrayList<String> foods;

              // default constructor, initializes empty food list

              public FoodListArrayList() {

                           //initializing array list

                           foods = new ArrayList<String>();

              }

              @Override

              public void add(String food) {

                           // using ArrayList's built in method to add to foods list

                           foods.add(food);

              }

              @Override

              public boolean onTheMenu(String food) {

                           // using ArrayList's built in method to check if it contains a String

                           // with given value

                           return foods.contains(food);

              }

              @Override

              public String toString() {

                           // calling array list's built in implementation of toString

                           return foods.toString();

              }

}

// FoodListLinkedList.java

/**

* implementation of FoodListInterface using a linked list as underlying data

* structure

*/

public class FoodListLinkedList implements FoodListInterface {

              private LLStringNode head;

              // default constructor, initializes empty food list

              public FoodListLinkedList() {

                           head = null;

              }

              @Override

              public void add(String food) {

                           //updating head node if it is null

                           if (head == null) {

                                         head = new LLStringNode(food);

                           } else {

                                         //finding the last node

                                         LLStringNode temp = head;

                                         while (temp.getLink() != null) {

                                                       temp = temp.getLink();

                                         }

                                         //adding next to last node

                                         temp.setLink(new LLStringNode(food));

                           }

              }

              @Override

              public boolean onTheMenu(String food) {

                           LLStringNode temp = head;

                           while (temp != null) {

                                         if (temp.getInfo().equalsIgnoreCase(food)) {

                                                       return true;

                                         }

                                         temp = temp.getLink();

                           }

                           return false;

              }

              @Override

              public String toString() {

                           // appending all food data to a String variable enclosed within square

                           // braces

                           String str = "[";

                           LLStringNode temp = head;

                           while (temp != null) {

                                         str += temp.getInfo();

                                         temp = temp.getLink();

                                         if (temp != null) {

                                                       str += ", ";

                                         }

                           }

                           str += "]";

                           return str;

              }

}

//Assuming you have LLStringNode class with you.

// Test.java

/**

* class for testing all three implementations of FoodListInterface

*/

public class Test {

              public static void main(String[] args) {

                           //testing each implementation separately, using same inputs

                           System.out.println("Testing FoodListArray\n");

                           FoodListArray array = new FoodListArray();

                           array.add("Pizza");

                           array.add("Paratha");

                           array.add("Barbecue");

                           array.add("Burger");

                           array.add("Sandwich");

                           array.add("Shawarma");

                           array.add("Big Belly Burger");

                           System.out.println(array);

                           System.out.println("Is fettuccine alfredo on food list? "

                                                       + array.onTheMenu("fettuccine alfredo"));

                           System.out.println("Is Big Belly Burger on food list? "

                                                       + array.onTheMenu("Big Belly Burger"));

                          

                           System.out.println("\nTesting FoodListLinkedList\n");

                           FoodListArray linkedList = new FoodListArray();

                           linkedList.add("Pizza");

                           linkedList.add("Paratha");

                           linkedList.add("Barbecue");

                           linkedList.add("Burger");

                           linkedList.add("Sandwich");

                           linkedList.add("Shawarma");

                           linkedList.add("Big Belly Burger");

                           System.out.println(linkedList);

                           System.out.println("Is fettuccine alfredo on food list? "

                                                       + linkedList.onTheMenu("fettuccine alfredo"));

                           System.out.println("Is Big Belly Burger on food list? "

                                                       + linkedList.onTheMenu("Big Belly Burger"));

                          

                           System.out.println("\nTesting FoodListArrayList\n");

                           FoodListArray arrayList = new FoodListArray();

                           arrayList.add("Pizza");

                            arrayList.add("Paratha");

                           arrayList.add("Barbecue");

                           arrayList.add("Burger");

                           arrayList.add("Sandwich");

                           arrayList.add("Shawarma");

                           arrayList.add("Big Belly Burger");

                           System.out.println(arrayList);

                           System.out.println("Is fettuccine alfredo on food list? "

                                                       + arrayList.onTheMenu("fettuccine alfredo"));

                           System.out.println("Is Big Belly Burger on food list? "

                                                       + arrayList.onTheMenu("Big Belly Burger"));

                          

              }

}

/*OUTPUT*/

Testing FoodListArray

[Pizza, Paratha, Barbecue, Burger, Sandwich, Shawarma, Big Belly Burger]

Is fettuccine alfredo on food list? false

Is Big Belly Burger on food list? true

Testing FoodListLinkedList

[Pizza, Paratha, Barbecue, Burger, Sandwich, Shawarma, Big Belly Burger]

Is fettuccine alfredo on food list? false

Is Big Belly Burger on food list? true

Testing FoodListArrayList

[Pizza, Paratha, Barbecue, Burger, Sandwich, Shawarma, Big Belly Burger]

Is fettuccine alfredo on food list? false

Is Big Belly Burger on food list? true

Add a comment
Know the answer?
Add Answer to:
Need a help with this one FoodListInterface.java specifies a Java interface for an ADT that maintains...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • 10.3 Example. When you first declare a new list, it is empty and its length is...

    10.3 Example. When you first declare a new list, it is empty and its length is zero. If you add three objects—a, b, and c—one at a time and in the order given, to the end of the list, the list will appear as a b c The object a is first, at position 1, b is at position 2, and c is last at position 3.1 To save space here, we will sometimes write a list’s contents on one...

  • Homework #1 – Implementing Set with a Linked List Project Objectives 1. Be able to integrate...

    Homework #1 – Implementing Set with a Linked List Project Objectives 1. Be able to integrate the knowledge of Java Generics, Collection Framework and the Linked List data structure to implement the Set ADT. Components emphasized are: • Allowing parameterized type for handling a general class of values in the collection framework; • Understanding conceptual differences between lists and sets and implementing them with methods; • Implementation of an iterator with functionality that is appropriate for the collection, namely the...

  • The ADT Bag is a group of items, much like what you might have with a...

    The ADT Bag is a group of items, much like what you might have with a bag of groceries. In a software development cycle, specification, design, implementation, test/debug, and documentation are typical activities. The details are provided in the rest of the document. ADT Bag Specification: (Note: You should not change the names of the operations in your program. This should be included in an interface.) Specify operations to  create an empty bag that can hold up to 100...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • Question: A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing...

    Question: A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing the area of the shape. You can assume that all the sides are equal in length. B. Implement the Comparable interface in the Circle and Rectangle from Question2 comparing the area of each shape. (use the same classes in Question 2) C. Also add the overloaded toString() method to each class that prints a summary of the object. Then, in another class, create an...

  • I NEED HELP with this. please create a UML diagram. I need a simple code to...

    I NEED HELP with this. please create a UML diagram. I need a simple code to solve the problem.   The ADT Bag is a group of items, much like what you might have with a bag of groceries. In a software development cycle, specification, design, implementation, test/debug, and documentation are typical activities. The details are provided in the rest of the document. ADT Bag Specification: (Note: You should not change the names of the operations in your program. This should...

  • in java • Create an interface called Person. In this interface create a method called printData()...

    in java • Create an interface called Person. In this interface create a method called printData() • Implement interface Person by classes Student, Teacher, Admin. You need to think the relevant data attributes for each class. Define atleast 4 attributes for each class. • In each class override a method toString() and create a string which holds all data • In the method printData in each class print the data using toString() method. Design the UML and write a code...

  • Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...

    Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up for helping. True/False (11) Chapter 12 - Lists The ADT list only works for entries that are strings. The ADT list is more general than common lists and has entries that are objects of the same type. Adding entries to the end of a list does not change the positions of entries already in the list. The first entry is a list is at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT