Question
Do it in Java: Directions Hydra Pieces of the Hydra class already exist and are in Hydra.java. Take a look at that code now if
Do it in Java: Directions
Hydra Pieces of the Hydra class already exist and are in Hydra.java. Take a look at that code now if you have not done so already. Also before you start, make sure you are familiar with the methods available to you in the ArrayBag class (check BagInterface.html).
Step 1. Compile the classes Hydra and ArrayBag. Run the main method in Hydra. Checkpoint: If all has gone well, the program will run and accept input. It will report that the head bag is null and then generate a null pointer exception. The goal now is to create and initialize the two bags.
Step 2. Create a new ArrayBag and assign it to headBag.
Step 3. Add the initial head into headBag.
Step 4. Create a new ArrayBag and assign it to workBag. Checkpoint: Compile and run the program. Enter 4 for the size of the initial head. The program should print out Bag [ 4 ] for the head bag. It should then report that the number of chops required is 0. The next goal is to cut off a single head. It will be encapsulated in the method simulationStep().
Step 5. Complete the simulationStep() method. Refer to the algorithm you developed in the pre-lab exercises to guide your code development. For now, dont worry about overflow.
Step 6. Call simulationStep(headBag, workBag) in main just after the comment ADD CODE HERE TO DO THE SIMULATION. 18 Lab 2 Bag Client
Step 7. Print out headBag just after the call to simulationStep. Checkpoint: Compile and run the program. Enter 4 for the size of the initial head. The program should print something similar to The head bag is Bag[ 4 ] The head bag is Bag[ 3 3 ] The number of chops required is 1 We see the head bag before and after the simulation step. The next goal is to do multiple steps of the simulation.
Step 8. Wrap the lines of code from the previous two steps in a while loop that continues as long as there is an item in the head bag. Checkpoint: Compile and run the program. Enter 3 for the size of the initial head. The program should print something similar to The head bag is Bag[ 3 ] The head bag is Bag[ 2 2 ] The head bag is Bag[ 2 1 1 ] The head bag is Bag[ 2 1 ] The head bag is Bag[ 2 ] The head bag is Bag[ 1 1 ] The head bag is Bag[ 1 ] The head bag is Bag[ ] The number of chops required is 7 Check the sequence of steps and verify that the work done was 7. Run the program again using 4 for the size of the initial head. The work done should be 15. Run the program again using 5 for the size of the initial head. The work done should be 25. Run the program again using 6 for the size of the initial head. The work done should be 25. Run the program again using 7 for the size of the initial head. The work done should be 25. Notice that for any input over 4, the work done is always 25. This is caused by an overflow of the work bag. For the final checkpoint we want to fix the code so that if there is an overflow, we stop the simulation and report the issue.
Step 9. Modify the simulationStep() method so that it will return false if either bag overflows. You will need to capture the return value of each call to the add() method. Step 10. Modify the while loop in the main() method so that it only continues if noOverflow is true.
Step 11. Capture the return value from the call to the simulationStep() methods and use it to set noOverflow correctly. Final checkpoint: Compile and run the program. Enter 3 for the size of the initial head. The program should print something similar to The head bag is Bag[ 3 ] The head bag is Bag[ 2 2 ] The head bag is Bag[ 2 1 1 ] The head bag is Bag[ 2 1 ] The head bag is Bag[ 2 ] The head bag is Bag[ 1 1 ] The head bag is Bag[ 1 ] The head bag is Bag[ ] Lab Manual for Data Structures and Abstractions with Java 19 The number of chops required is 7 Run the program again using 4 for the size of the initial head. The work done should be 15. Run the program again using 5 for the size of the initial head. You should get the computation ended early. Run the program again using 6 for the size of the initial head. You should get the computation ended early. Run the program again using 7 for the size of the initial head. You should get the computation ended early.
import java.io.*;
import java.util.*;
/**
* Hydra is a program that will simulate the work done for a
* computational task that can be broken down into smaller subtasks.
*
* @author Charles Hoot
* @version 4.0
*/
public class Hydra {
public static void main(String args[]) {
BagInterface headBag = null;
BagInterface workBag = null;
int startingSize;
System.out.println("Please enter the size of the initial head.");
startingSize = getInt(" It should be an integer value greater than or equal to 1.");
// ADD CODE HERE TO CREATE AND INITIALIZE THE TWO BAGS
System.out.println("The head bag is " + headBag);
boolean noOverflow = true;
// ADD CODE HERE TO DO THE SIMULATION
if (noOverflow) {
System.out.println("The number of chops required is " + workBag.getCurrentSize());
} else {
System.out.println("Computation ended early with a bag overflow");
}
}
/**
* Take one head from the headBag bag. If it is a final head, we are done with it.
* Otherwise put in two heads that are one smaller.
* Always put a chop into the work bag.
*
* @param headBag A bag holding the heads yet to be considered.
* @param workBag A bag of chops.
*
*/
public static boolean simulationStep(BagInterface heads, BagInterface work) {
// COMPLETE THIS METHOD
boolean result = true;
return result;
}
/**
* Get an integer value.
*
* @return An integer.
*/
private static int getInt(String rangePrompt) {
Scanner input;
int result = 10; //default value is 10
try {
input = new Scanner(System.in);
System.out.println(rangePrompt);
result = input.nextInt();
} catch (NumberFormatException e) {
System.out.println("Could not convert input to an integer");
System.out.println(e.getMessage());
System.out.println("Will use 10 as the default value");
} catch (Exception e) {
System.out.println("There was an error with System.in");
System.out.println(e.getMessage());
System.out.println("Will use 10 as the default value");
}
return result;
}
}
-------Baginterface:--------
/**
An interface that describes the operations of a bag of objects.
@author Frank M. Carrano
* This code is from Chapter 1 of
* Data Structures and Abstractions with Java 3/e
* by Carrano
*/
public interface BagInterface {
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
public int getCurrentSize();
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
public boolean isEmpty();
/** Adds a new entry to this bag.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful, or false if not. */
public boolean add(T newEntry);
/** Removes one unspecified entry from this bag, if possible.
@return Either the removed entry, if the removal
was successful, or null. */
public T remove();
/** Removes one occurrence of a given entry from this bag,
if possible.
@param anEntry The entry to be removed.
@return True if the removal was successful, or false if not */
public boolean remove(T anEntry);
/** Removes all entries from this bag. */
public void clear();
/** Counts the number of times a given entry appears in this bag.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
public int getFrequencyOf(T anEntry);
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if the bag contains anEntry, or false if not. */
public boolean contains(T anEntry);
/** Retrieves all entries that are in this bag.
@return A newly allocated array of all the entries in the bag.
Note: If the bag is empty, the returned array is empty. */
public T[] toArray();
} // end BagInterface
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started