
(Modify weight in the nine tails problem) In the text, we assign the number of the flips as the weight for each move. Assuming the weight is three times of the number of flips, revise the

Program Plan:
- Create a package “main”.
- Add a java class named “Edge” to the package which is used to get the edges from the graph.
- Add a java class named “Graph” to the package which is used to add and remove vertices, edges.
- Add a java class named “UnweightedGraph” to the package which is used to store vertices and neighbors.
- Add a java class named “WeightedGraph” to the package which is used to get the weighted edges and print the edges.
- Add a java class named “WeightedEdge” to the package which is used to compare edges.
- Add a java class named “NineTailModel” to the package which is used to compare edges.
- Add a java class named “Test” to the package.
- Import the required packages.
- Declare the main class.
- Give the “main ()” method.
- Allocate the memory for the “Test” class.
- Define “Test”.
- Get the initial nine coins from the user.
- Create an object for the “ModifiedWeightedNineTailModel” class.
- Create an array list.
- Display the steps to flip the coin.
- Display the number of flips.
- Define “ModifiedWeightedNineTailModel” class.
- Create an edges and graph.
- Obtain a BSF tree rooted at the target node.
- Define “getEdges” method.
- Create an array list.
- Create all the edges for the graph by calling “getFlippedNode” and “getNumberOfFlips” methods.
- Add edge for a legal move from the node u to v.
- Return the edge.
- Define “getNumberOfFlips” method.
- Declare the required variables.
- Check if the “node1” is not equal to “node2” means increment the “count”.
- Return the value.
- Define “getNumberOfFlips” method.
- Return the total number of flips.
- Give the “main ()” method.
The given program is used to modify weight in the nine tails problem is as follows:
Explanation of Solution
Program:
Edge.java: Refer book from chapter 28 – Listing 28.1.
Graph.java: Refer book from chapter 28 – Listing 28.3.
UnweightedGraph.java: Refer book from chapter 28 – Listing 28.4.
WeightedGraph.java: Refer book from chapter 29 – Listing 29.2.
WeightedEdge.java: Refer book from chapter 29 – Listing 29.1.
NineTailModel.java: Refer book from chapter – Listing 28.13.
Test.java:
//import the required statement
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//definition of "Test" class
public class Test
{
//main method
public static void main(String[] args)
{
//memory allocation
new Test();
}
//definition of "Test"
public Test()
{
// get the input from the user
System.out.print("Enter an initial nine coin H’s and T's: ");
Scanner input = new Scanner(System.in);
String s = input.nextLine();
//declare the variable
char[] initialNode = s.toCharArray();
//create an object
ModifiedWeightedNineTailModel model = new ModifiedWeightedNineTailModel();
//create an ArrayList
java.util.List<Integer> path =
model.getShortestPath (NineTailModel.getIndex(initialNode));
//display the steps to flip the coin
System.out.println("The steps to flip the coins are ");
for (int i = 0; i < path.size(); i++)
NineTailModel.printNode(
NineTailModel.getNode (path.get(i).intValue()));
//display the number of flips
System.out.println("The number of flips is " +
model.getNumberOfFlips (NineTailModel.getIndex(initialNode)));
}
//definition of "ModifiedWeightedNineTailModel"
public static class ModifiedWeightedNineTailModel extends NineTailModel
{
//constructor
public ModifiedWeightedNineTailModel()
{
// create edges
List<WeightedEdge> edges = getEdges();
// create a graph
WeightedGraph<Integer> graph = new WeightedGraph<Integer>(
edges, NUMBER_OF_NODES);
/* obtain a BSF tree rooted at the target node*/
tree = graph.getShortestPath(511);
}
//definition of "getEdge" method
private List<WeightedEdge> getEdges()
{
// create an ArrayList
List<WeightedEdge> edges = new ArrayList<WeightedEdge>();
//check the condition
for (int u = 0; u < NUMBER_OF_NODES; u++)
{
//check the condition
for (int k = 0; k < 9; k++)
{
// get the node for vertex u
char[] node = getNode(u);
//check the condition
if (node[k] == 'H')
{
/*call the "getFlippedNode" method*/
int v = getFlippedNode(node, k);
int numberOfFlips = getNumberOfFlips(u, v);
/* add edge for a legal move from node u to node v*/
edges.add(new WeightedEdge(v, u, numberOfFlips));
}
}
}
//return statement
return edges;
}
//definition of "getNumberOfFlips" method
private static int getNumberOfFlips(int u, int v)
{
//declare the variables
char[] node1 = getNode(u);
char[] node2 = getNode(v);
int count = 0;
//check the condition
for (int i = 0; i < node1.length; i++)
//check the condition
if (node1[i] != node2[i]) count++;
//return statement
return 3 * count;
}
//definition of "getNumberOfFlips" method
public int getNumberOfFlips(int u)
{
//return statement
return (int)((WeightedGraph<Integer>.ShortestPathTree)tree).getCost(u);
}
}
}
Enter an initial nine coin H’s and T's: HHHTTTHHH
The steps to flip the coins are
HHH
TTT
HHH
HHH
THT
TTT
TTT
TTT
TTT
The number of flips is 24
Want to see more full solutions like this?
Chapter 29 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Mechanics of Materials (10th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Modern Database Management
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
- whats for dinner? pleasearrow_forwardConsider the follow program that prints a page number on the left or right side of a page. Define and use a new function, isEven, that returns a Boolean to make the condition in the if statement easier to understand. ef main() : page = int(input("Enter page number: ")) if page % 2 == 0 : print(page) else : print("%60d" % page) main()arrow_forwardWhat is the correct python code for the function def countWords(string) that will return a count of all the words in the string string of workds that are separated by spaces.arrow_forward
- Consider the following program that counts the number of spaces in a user-supplied string. Modify the program to define and use a function, countSpaces, instead. def main() : userInput = input("Enter a string: ") spaces = 0 for char in userInput : if char == " " : spaces = spaces + 1 print(spaces) main()arrow_forwardWhat is the python code for the function def readFloat(prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. Here is a typical usage: salary = readFloat("Please enter your salary:") percentageRaise = readFloat("What percentage raise would you like?")arrow_forwardassume python does not define count method that can be applied to a string to determine the number of occurances of a character within a string. Implement the function numChars that takes a string and a character as arguments and determined and returns how many occurances of the given character occur withing the given stringarrow_forward
- Consider the ER diagram of online sales system above. Based on the diagram answer the questions below, a) Based on the ER Diagram, determine the Foreign Key in the Product Table. Just mention the name of the attribute that could be the Foreign Key. b) Mention the relationship between the Order and Customer Entities. You can use the following: 1:1, 1:M, M:1, 0:1, 1:0, M:0, 0:M c) Is there a direct relationship that exists between Store and Customer entities? Answer Yes/No? d) Which of the 4 Entities mention in the diagram can have a recursive relationship? e) If a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type?arrow_forwardNo aiarrow_forwardGiven the dependency diagram of attributes {C1,C2,C3,C4,C5) in a table shown in the following figure, (the primary key attributes are underlined)arrow_forward
- What are 3 design techniques that enable data representations to be effective and engaging? What are some usability considerations when designing data representations? Provide examples or use cases from your professional experience.arrow_forward2D array, Passing Arrays to Methods, Returning an Array from a Method (Ch8) 2. Read-And-Analyze: Given the code below, answer the following questions. 2 1 import java.util.Scanner; 3 public class Array2DPractice { 4 5 6 7 8 9 10 11 12 13 14 15 16 public static void main(String args[]) { 17 } 18 // Get an array from the user int[][] m = getArray(); // Display array elements System.out.println("You provided the following array "+ java.util.Arrays.deepToString(m)); // Display array characteristics int[] r = findCharacteristics(m); System.out.println("The minimum value is: " + r[0]); System.out.println("The maximum value is: " + r[1]); System.out.println("The average is: " + r[2] * 1.0/(m.length * m[0].length)); 19 // Create an array from user input public static int[][] getArray() { 20 21 PASSTR2222322222222222 222323 F F F F 44 // Create a Scanner to read user input Scanner input = new Scanner(System.in); // Ask user to input a number, and grab that number with the Scanner…arrow_forwardGiven the dependency diagram of attributes C1,C2,C3,C4,C5 in a table shown in the following figure, the primary key attributes are underlined Make a database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NFarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,


