Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 28, Problem 28.7PE
Program Plan Intro
Find a cycle
Program Plan:
Exercise.java:
- Import the required packages.
- Create a class “Exercise”:
- Define the main method
- Define and declare the required vertices.
- Define and declare the required edges.
- New graph gets created for the defined edges and vertices.
- Display the cycle that is found.
- Define the main method
- Define and create the class “UnweightedGraphFindCycle”
- Create a new constructor.
- New graph gets using the constructor.
- Method to “cloneEdges()” gets created.
- New list gets created.
- Loop that iterates to add the edges into the list.
- A neighbor copy of the edges is added.
- Returns the result of the neighbor copy.
- Method “getACycle()” gets created to find the cycle.
- New list is created.
- Loop that iterates to add the elements into the list.
- New list that contains the neighbor node, clone edges are created.
- Loop that iterates to assign the value to the parent node is defined.
- New stack gets defined.
- Elements are added in to the stack.
- Validate the stack and add elements into the stack.
- Mark the nodes that are visited.
- Validate the elements of the stack add and remove elements based on the nodes that are visited.
- If the graph does not contain any cycle returns null.
UnweightedGraph.java:
- Import the required packages.
- Create a class “UnweightedGraph”:
- New list for the vertices gets created.
- New list for the neighbor node gets created.
- Create an empty constructor.
- Method to create new graph gets created and adjacency list gets created.
- Method to create an adjacency list gets created.
- Method to return the size of the vertices.
- Method to return the index of the vertices gets defined.
- Method to gets the neighbor node gets defined.
- Method to return the degree of the vertices gets created.
- Method to print the Edges gets created.
- New to clear the graph gets created.
- Method to add vertex gets created.
- Method to add edge gets created.
- Method to perform the depth first search gets defined.
- Method to perform breadth first search gets defined.
- Search tree gets returned.
- Create a class “SearchTree”
- Define the method to return the root.
- Method to return the parent of the vertices
- Method to return the search order gets defined.
- Method to return the number of vertices found gets defined.
- Method to get the path of the vertices gets defined.
- Loop to validate the path gets defined.
- Path gets returned.
- Method to print the path gets defined.
- Method to print the tree gets defined.
- Display the edge.
- Display the root.
- Condition to validate the parent node to display the vertices gets created.
Graph.java:
- A graph interface gets created.
- Method to return the size gets defined.
- Method to return the vertices gets defined.
- Method to return the index gets created.
- Method to get the neighbor node gets created.
- Method to get the degree gets created.
- Method to print the edges.
- Method to clear the node gets created.
- Method to add the edges, add vertex gets created.
- Method to remove the vertices gets defined.
- Method for the depth first search gets defined.
- Method for the breadth first search gets defined.
Edge.java
- Create a class “Edge”
- Define and declare the required variables.
- Constructor gets defined.
- Method that defines Boolean objects gets defined.
- Return the value after validating the vertices.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Tape for a Turing Machine (Doubly-linked List)”.(I USE ECLIPSE & RUN CHECK ONLINE CONSOLE)A class named Tape to represent Turing machine tapes. The class should have an instance variable of type Cell that points to the current cell with the method:
public Cell getCurrentCell() -- returns the pointer that points to the current cell.
public char getContent() -- returns the char from the current cell.
public void setContent(char ch) -- changes the char in the current cell to the specified value. Don't forget the Javadoc comments. IT WOULD HAVE A PUBLIC CLASS
public void moveLeft() -- moves the current cell one position to the left along the tape. Note that if the current cell is the leftmost cell that exists, then a new cell must be created and added to the tape at the left of the current cell, and then the current cell pointer can be moved to point to the new cell. The content of the new cell should be a blank space. (Remember that the Turing machine's tape is conceptually…
5. Now examine the for-loops in the above Java program. (assume that we have the have replaced the array with an ArrayList)
a) Which ArrayList method should you use to control the for-loop iterations?
b) Explain why the first for-loop cannot be controlled by that ArrayList method (but the other two for-loops can).
Here is a quick review of how array elements are modified and accessed:
1.
public class BasicArray{
2.
public static void main(String[] args)
int[] basic = new int[4];
for (int i=0; i
(Sort ArrayList)
Write the following method that sorts an ArrayList:
public static <E extends Comparable<E>> void sort(ArrayList<E> list)
Write a test program that prompts the user to enter 10 integers, invokes this method to sort the numbers, and displays the numbers in increasing order.
Sample Run
Enter 10 integers: 3 4 12 7 3 4 5 6 4 7
The sorted numbers are 3 3 4 4 4 5 6 7 7 12
Class Name: Exercise19_09
Chapter 28 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 28.2 - What is the famous Seven Bridges of Knigsberg...Ch. 28.2 - Prob. 28.2.2CPCh. 28.2 - Prob. 28.2.3CPCh. 28.2 - Prob. 28.2.4CPCh. 28.3 - Prob. 28.3.1CPCh. 28.3 - Prob. 28.3.2CPCh. 28.4 - Prob. 28.4.1CPCh. 28.4 - Prob. 28.4.2CPCh. 28.4 - Show the output of the following code: public...Ch. 28.4 - Prob. 28.4.4CP
Ch. 28.5 - Prob. 28.5.2CPCh. 28.6 - Prob. 28.6.1CPCh. 28.6 - Prob. 28.6.2CPCh. 28.7 - Prob. 28.7.1CPCh. 28.7 - Prob. 28.7.2CPCh. 28.7 - Prob. 28.7.3CPCh. 28.7 - Prob. 28.7.4CPCh. 28.7 - Prob. 28.7.5CPCh. 28.8 - Prob. 28.8.1CPCh. 28.8 - When you click the mouse inside a circle, does the...Ch. 28.8 - Prob. 28.8.3CPCh. 28.9 - Prob. 28.9.1CPCh. 28.9 - Prob. 28.9.2CPCh. 28.9 - Prob. 28.9.3CPCh. 28.9 - Prob. 28.9.4CPCh. 28.10 - Prob. 28.10.1CPCh. 28.10 - Prob. 28.10.2CPCh. 28.10 - Prob. 28.10.3CPCh. 28.10 - If lines 26 and 27 are swapped in Listing 28.13,...Ch. 28 - Prob. 28.1PECh. 28 - (Create a file for a graph) Modify Listing 28.2,...Ch. 28 - Prob. 28.3PECh. 28 - Prob. 28.4PECh. 28 - (Detect cycles) Define a new class named...Ch. 28 - Prob. 28.7PECh. 28 - Prob. 28.8PECh. 28 - Prob. 28.9PECh. 28 - Prob. 28.10PECh. 28 - (Revise Listing 28.14, NineTail.java) The program...Ch. 28 - (Variation of the nine tails problem) In the nine...Ch. 28 - (4 4 16 tails problem) Listing 28.14,...Ch. 28 - (4 4 16 tails analysis) The nine tails problem in...Ch. 28 - (4 4 16 tails GUI) Rewrite Programming Exercise...Ch. 28 - Prob. 28.16PECh. 28 - Prob. 28.17PECh. 28 - Prob. 28.19PECh. 28 - (Display a graph) Write a program that reads a...Ch. 28 - Prob. 28.21PECh. 28 - Prob. 28.22PECh. 28 - (Connected rectangles) Listing 28.10,...Ch. 28 - Prob. 28.24PECh. 28 - (Implement remove(V v)) Modify Listing 28.4,...Ch. 28 - (Implement remove(int u, int v)) Modify Listing...
Knowledge Booster
Similar questions
- QUESTION 3 Save Answer Write the missing statements as per given comments to complete the programs. Java program for flexible array using ArrayList import java.util.ArrayList; import java.util.lterator, public class IteratorPatternDemo { public static void main(String args | DE I/ Define an object of ArrayList named as aryStack I| Add or push "Ahmed" on the top of aryStack i.e., at position 0 I| Add or push "Khalid" on the top of aryStack | Add or push "Majid" on the top of aryStack I/ Remove first name from aryStacki.e., from position 0 // Define an iterator "it" on aryStack I/ Write code to display all names in aryStack using the iterator • while( String obj = (String)it.next(): System.out.printin(obj): }}arrow_forward(Implement set operations in MyList)(JAVA) Please use class name: Exercise_01 The implementations of the methods addAll, removeAll, retainAll, toArray(), and toArray(T[]) are omitted in the MyList interface. Implement these methods. Test your new MyList class using the code at https://liveexample.pearsoncmg.com/test/Exercise24_01.txt. Sample Output: Enter five strings for array name1 separated by space: TomGeorgePeterJeanJaneEnter five strings for array name2 separated by space: TomGeorgeMichaelMichelleDanielEnter two strings for array name3 separated by space: TomPeterlist1:[Tom, George, Peter, Jean, Jane]list2:[Tom, George, Michael, Michelle, Daniel]After addAll:[Tom, George, Peter, Jean, Jane, Tom, George, Michael, Michelle, Daniel] list1:[Tom, George, Peter, Jean, Jane]list2:[Tom, George, Michael, Michelle, Daniel]After removeAll:[Peter, Jean, Jane] list1:[Tom, George, Peter, Jean, Jane]list2:[Tom, George, Michael, Michelle, Daniel]After retainAll:[Tom, George] list1:[Tom, George,…arrow_forward(Please Help. My professor did not teach us any of this and will not answer my emails) Design a class named Queue for storing integers. Like a stack, a queue holds elements. In a stack, the elements are retreived in a last-in-first-out fashion. In a queue, the elements are retrieved in a first-in-first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue A data field named size that stores the number of elements in the queue A constructor that creates a Queue object with defult capacity 8 The method enqueue(int v) that adds v into the queue The method empty () that returns true if the queue is empty The method getSize() that returns the size of the queuearrow_forward
- (Implement a doubly linked list) The MyLinkedList class used in Listing 24.6 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows:public class Node<E> { E element; Node<E> next; Node<E> previous;public Node(E e) { element = e; } }Implement a new class named TwoWayLinkedList that uses a doubly linked list to store elements. The MyLinkedList class in the text extends MyAbstractList. Define TwoWayLinkedList to extend the java.util.AbstractSequentialList class. You need to implement all the methods defined in MyLinkedList as well as the methods listIterator() and listIterator(int index). Both return an instance of java.util. ListIterator<E>. The former sets the cursor to the head of the list and the latter to the element at the specified index.arrow_forward(Implement MyLinkedList) The implementations of the methods contains(E e), get(int index), indexOf(E e), lastIndexOf(E e), and set(int index, E e) are omitted in the MyLinkedList class. Implement these methods. Define a new class named MyLinkedListExtra that extends MyLinkedListto override these methods. Test your new MyList class using the code at import java.util.*; public class Exercise24_02 { public static void main(String[] args) { new Exercise24_02(); } public Exercise24_02() { String[] names = {"Tom", "Susan", "Kim", "George", "Peter", "Jean", "George", "Jane", "Denise", "Jenny", "Susan", "Kathy", "Jane"}; MyList<String> list = new MyLinkedListExtra<>(names); System.out.println(list); Scanner input = new Scanner(System.in); System.out.print("Enter a name: "); String name = input.next(); System.out.print("Enter an index: "); int index = input.nextInt(); System.out.println(name + " is in the list? " +…arrow_forward- Write a Polynomial class that has methods for creating a polynomial, reading and writing a polynomial, and adding a pair of polymomials - In order to add 2 polynomials, traverse both lists, If a particular exponent value is present in either one, it should also be present In the resulting polynomial unless its coefficient is zero. java submit as a text dont use others answers please attach screenshot of output Thank you!!!arrow_forward
- Edit only the class definition. DO NOT CHANGE the code given under 'main' please. Steps: Additionally implement any Python Magic/Dunder methods such that instances of the class minimally:1. Support addition, subtraction, equality operations and the built-in abs function2. Are Iterable i.e., support for loops and star arguments for unpacking into function calls3. Support a string representation that displays the class name and coordinates stored by the instance: i.e. for an object initialized as: Vector(0, 3), the string representation should be ‘Vector(0, 3)’The code given under main tests for each of the program requirements and subsequently uses the turtle module to plot randomly generated points rotated. A screenshot for a sample run of the program is attached. Template.py: from math import hypot, pi from random import uniform import turtle as t class Vector2D: ... if __name__ == '__main__': # Test Vector class a, b = Vector2D(0, 3), Vector2D(0, -3) tests =…arrow_forwardnarmts Save Answer Write a Java program that uses ArrayList and Iterator. It should input from user the names and ages of your few friends in a loop and add into ArrayList. Finally, it should use Iterator to display the data in a proper format. (Hint- Lecture 02: Slide 8) Sample output: List of my Friends Enter name and age [friend# oj Khalid Al-shamri 22.5 Do you want to add another friend (y/n)? y Enter name and age [friend# 1] Rahsed Al-anazi 21.1 Do you want to add another friend (y/n)? y Enter name and age [friend# 2] Salem Al-mutairi 23.7 Do you want to add another friend (y/n)? n Here is the data you entered: 0. Khalid Al-shamri, 22.5 1. Rahsed Al-anazi, 21.1 2. Salem Al-mutairi, 23.7arrow_forwardJava (Generic Method) - What Order?arrow_forward
- Name:- MIT Note:- Please type this java code and also need an output for this given Java program.arrow_forwardQuestion 7: Write a method for the ArrayBoundedStack class that creates and returns a stack copy of this. (Hint: use topIndex and elements) Use following method signature: public ArrayBoundedStack<T> copy()arrow_forwardHi, You are the Best :)arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education