Introduction to Java Programming and Data Structures, Comprehensive Version Plus MyProgrammingLab with Pearson EText -- Access Card Package
bartleby

Videos

Question
Book Icon
Chapter 29, Problem 29.4PE
Expert Solution & Answer
Check Mark
Program Plan Intro

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.
Program Description Answer

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);

}

}

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Answer all of the questions with steps by step explanation to every question.
W Go Tools Window Help mac283_quiz3_fall2025.pdf Page 2 of 2 @ Q Q Û • ¨ ® - Qy Search X 00 01 11 10 0 1 1 1 0 1 1 1 1 1 A ABC 88% Problem 3. Draw the combinational circuit that directly implements the Boolean expression: F(x, y, z) = xyz + (y²+z) Problem 4. Find the truth table that describes the following circuit. y- z - X Problem 5. a) Describe how a decoder works and indicate typical inputs and outputs. b) How many inputs does a decoder have if it has 64 outputs? NOV 6 M tv♫ zoom
CPS 2390 Extra Credit Assignment For each problem, choose the best answer and explain how you arrived at your answer. (15 points each.) 1.If control is redirected to location x4444 after the execution of the following instructions, what should have been the relationship between R1 and R2 before these instructions were executed? Address Instruction x4400 1001100010111111 x4401 0001100100100001 x4402 0001100001000100 x4403 0000100001000000 A. R1 R2 (R1 was greater than R2) B. R1 R2 (R2 was greater than R1) C. R1 R2 (R1 and R2 were equal) = D. Cannot be determined with the given information. 2. If the value stored in RO is 5 at the end of the execution of the following instructions, what can be inferred about R5? Address x3000 Instruction 0101000000100000 x3001 0101111111100000 x3002 0001110111100001 x3003 0101100101000110 x3004 0000010000000001 x3005 0001000000100001 x3006 0001110110000110 x3007 0001111111100001 x3008 0001001111111000 x3009 0000100111111000 x300A 0101111111100000 A. The…

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Boolean Algebra - Digital Logic and Logic Families - Industrial Electronics; Author: Ekeeda;https://www.youtube.com/watch?v=u7XnJos-_Hs;License: Standard YouTube License, CC-BY
Boolean Algebra 1 – The Laws of Boolean Algebra; Author: Computer Science;https://www.youtube.com/watch?v=EPJf4owqwdA;License: Standard Youtube License