(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)
- Translate the following VM commands to Assembly instructions: □ push constant 1 □ push constant 5arrow_forwardSuppose the state of the argument and local memory segments are as follows: argument local stack 0 0 9 sp-> 256 1 257 1 14 2 258 259 Now consider the following VM code: 1 push constant 2 pop local @ 3 push constant 15 4 pop local 1 5 push local 1 6 push argument 1 7 gt 8 pop local 2 9 push local 0 10 push argument 0 11 add 12 pop local 0 13 push local 1 14 push local 1 15 push constant 1 16 sub 17 add 18 pop local 1 What will be the value of local 1 after the VM code has executed?arrow_forwardSuppose the state of the RAM is as follows and the adjacent assembly code will execute: RAM 0 3 1 2 2 0 فيا 3 6 456 5 1 4 1234567 $1 A = M A = M A = M D = M @4 M = D What will be the value of the RAM[4] following the assembly code execution?arrow_forward
- Two industries that use decision trees extensively are lenders (banks, mortgage companies, etc.) and insurance. Discuss how a decision tree is used to solve a business problem.arrow_forwardand some More lab 9 For the last lab of the term, I want you to create a practical application for your database, in which you modify it in some way, for instance, taking a new order. Since the emphasis is on the database connectivity, neither a GUI nor a web application is required. In fact, a GUI would require x2go, which I don't have at home and you probably don't either. A web interface would need to be on osiris, I could figure out some way to try it out, I guess. There are also numerous security concerns. The applications should be easy for someone like a store employee or on-line customer to use. The most common operations, such as adding a customer or renting a tool, should be implemented. These Part 1 instructions now include how to use postgresql in C as well as Java and python. The important issue of transactions and commit status is addressed here. Some previous notes about web applications, and the PHP language [Previously I wrote] Now write one more program, that updates…arrow_forwardc# app formarrow_forward
- (a) Giving C and k constants, determine the big-theta for the function: f(x) = 9x4 − x³ +5x+2+(-13 + 9x³ - 7x)log(8x6 + 5x³ + 9x² + 11x + 3) -arrow_forwardFollow the instructionsarrow_forwardPlease help me adjust the X-axis on my graphs in Excel spreadsheet. Range numbers are from 200 to 500 but is graphed 0 to 300. Link:https://mnscu-my.sharepoint.com/:x:/g/personal/vi2163ss_go_minnstate_edu/EdVWDTGQ2hNJuGHiHPukjuIB9DBRlyoUC8Fuqlxj2E_CQg Thank you!arrow_forward
- 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