Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 29, Problem 29.4PE

(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.

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
(Use DevC++) 1. Concert Tickets Sale and Charity Donation Program Design and implement a program that prompts the user to input the concert name, ticket price,number of tickets sold, and percentage of the gross amount to be donated to the charity. The sampleoutput is as shown below: Sample Output:**************************************************************************Concert Name: ………………………………..… The Concert at the ParkTicket Price: …………………………………..… P 49.75Number of Tickets Sold: ………………….…. 5,985 pcs.Gross Amount: ………………………………….. P 297, 753.75Percent of Gross Amount Donated: ……… 13.00%Amount Donated: …………………………..…. P 38, 707. 9875Net Sale: ………………………………………….. P 259, 045.7625************************************************************************** Note that the concert name such as “The Concert at the Park” must be declared as String. Input: Concert Name, Ticket Price, Number of Tickets Sold, and Percent of Gross Amount Donated Output: see sample output above
(Guess the Number Modification) Modify the program of Exercise 5.32 to count the number of guesses the player makes. If the number is 10 or fewer, print Either you know the secret oryou got lucky! If the player guesses the number in 10 tries, then print Ahah! You know the secret!If the player makes more than 10 guesses, then print You should be able to do better! Why shouldit take no more than 10 guesses? Well, with each “good guess” the player should be able to eliminatehalf of the numbers. Now show why any number 1 to 1000 can be guessed in 10 or fewer tries.
(Computer-Assisted Instruction: Reducing Student Fatigue) One problem in CAI environments is student fatigue. This can be reduced by varying the computer’s responses to hold the student’s attention. Modify the program of Exercise 6.57 so that various comments are displayed for each answer as follows: Possible responses to a correct answer: Very good!Excellent!Nice work!Keep up the good work! Possible responses to an incorrect answer: No. Please try again.Wrong. Try once more.Don't give up!No. Keep trying.Use random-number generation to choose a number from 1 to 4 that will be used to select one of the four appropriate responses to each correct or incorrect answer. Use a switch statement to issue the responses. ------------------------------ EXERCISE 6.57 CODE: ----------------------------- //Name: IhabAtouf//Date:02/23/2023// exercise 6.57 on page 281//program description: create computer-assisted instruction (CAI) program that help students master thier math skills in…

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
What is control break logic?

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

What is the distinction between application software and system software? Give an example of each.

Computer Science: An Overview (13th Edition) (What's New in Computer Science)

A program contains the following method definition: public static int cube(int num) { return num num num; } W...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
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