Choose your own adventure games, movies and books are a not so popular sub section of entertainment media, where each story is written from a second-person point of view, with the reader assuming the role of the protagonist and making choices that determine the main character’s actions and the plot’s outcome. These actions are usually only a choice between two deciding factors where sometimes the stories advance to different paths or a path you were position you had previously been to is revisited. Occasionally when revisiting a node that you have been to before actions may have changed ever so slightly. In this problem we are going to design the logic for this system using our knowledge of data structures. In PRIMARY LANGUAGE: Create an ChooseYourAdventure Class:
In this problem you will create a class named ChooseYourAdventure
that will create an object that lets the user design their own
”Choose You Own Adventure App.” This app will be created from
interconnected elements that are related to each other by
unidirectional links. One element can have at most two links
leaving that element and going to another. Links leaving the
element can be added to any element that isn’t marked ENDING; and
the links can be connected to any node. When constructing your
ChooseYourAdventure objects they must take in ONE STRING for the
STARTING ELEMENT, and an ARRAY OF STRINGS for the ENDING elements
that must have a size be greater than 0. A ChooseYourAdventure
object must only have one STARTING element that cannot be replaced
( though the string that it presents can be edited ); the same
thing applies to each ENDING ELEMENT. ENDING ELEMENTS MAY NOT BE
REMOVED OR ADDED TO. You may add elements to the
ChooseYourAdventure object that include their own string to present
to the user. Each element should be able to tell: how long the user
has been on the game; how often the user has traveled to this
element; which node that the user has traveled to it from. A
ChooseYourAdventure object should also be able to show the history
of every choice made to progress the story. In a comment block
before the class definition you will describe the data structures
and primitive data types used to satisfy the requirements for this
class. In a comment block, before each method/function not only
will you describe the inputs and outputs of the method, but you
will also state how it works and its Big-O time complexity and
space complexity.
ChooseYourAdventure Requirements ( can be renamed ):
• numberOfElements
• addElement
• addLink
• removeElement
• removeLink
• choiceHistory
Element Requirements ( can be renamed ):
• links
• isEnding
• storyLines - the string or STRINGS presented to the user based on times visited, how many choices the user has made and where the user visited from where
• printStory - prints the correct storyline based on times visited, how many choices the user has made and where the user visited from where
(No outside library such as java.util* be used except Scanner, Date, Timer)
(Please put it in Java)
Working code implemented in Java and appropriate comments provided for better understanding:
Here I am attaching code for these files:
Source code for Main.java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ChooseYourAdventure chooseYourAdventure = new
ChooseYourAdventure();
creator(chooseYourAdventure);
play(chooseYourAdventure);
}
public static void menu(){
System.out.println("1. Show elements");
System.out.println("2. Add element");
System.out.println("3. Update element");
System.out.println("4. Remove element");
System.out.println("5. Add link");
System.out.println("6. Remove link");
System.out.println("7. Finish creation");
}
public static void play(ChooseYourAdventure
chooseYourAdventure){
System.out.println("\n\n\nBegin\n");
chooseYourAdventure.play();
}
public static void creator(ChooseYourAdventure
chooseYourAdventure){
chooseYourAdventure.init();
int choice = 0;
while(choice!=7){
menu();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter: ");
choice = scanner.nextInt();
switch (choice){
case 1:{
chooseYourAdventure.printNodes();
break;
}
case 2:{
chooseYourAdventure.addElement();
break;
}
case 3:{
chooseYourAdventure.update();
break;
}
case 4:{
chooseYourAdventure.removeElement();
break;
}
case 5:{
chooseYourAdventure.addLink();
break;
}
case 6:{
chooseYourAdventure.removeLink();
break;
}
default: {
break;
}
}
}
}
}
Source code for ChooseYourAdventure.java:
import java.util.Scanner;
/**
* A class to create Choose Your Adventure App and using it.
*
* To satisfy the requirements for this class are used types: int,
String.
* Data structures: list, tree.
*
*/
public class ChooseYourAdventure {
private Node head;
private Element root;
private int size;
private int maxId;
public ChooseYourAdventure() {
head = null;
root = null;
size = 0;
maxId = 0;
}
/**
* A method to start play.
* <p>
* Prints starting story. Then checks the user's selection and
* prints the next stories until the user reaches the ending
story.
* <p>
* Time complexity: O(log(n))
* Space complexity: O(n).
*/
public void play() {
System.out.println(root.story);
root.timesVisited++;
Element.addStoryLine(root.story);
Element tmp = root;
while (tmp != null) {
if (tmp.isEnding()) {
System.out.println("\nThe end of the story!");
break;
}
if (tmp.left != null) {
System.out.print("Choose next branch from");
System.out.print(" (1)");
if (tmp.right != null) {
System.out.print(", (2)");
}
System.out.print(": ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
if (choice == 1) {
tmp = tmp.left;
System.out.println(tmp.story);
tmp.timesVisited++;
Element.addStoryLine(" -> \n" + tmp.story);
} else if (choice == 2) {
if (tmp.right != null) {
tmp = tmp.right;
System.out.println(tmp.story);
tmp.timesVisited++;
Element.addStoryLine(" -> \n" + tmp.story);
} else {
System.out.println("Branch (" + choice + ") doesn't exist.");
}
} else {
System.out.println("Branch (" + choice + ") doesn't exist.");
}
} else {
System.out.println("\nThe end of the story!");
break;
}
}
}
/**
* A method to print not ending nodes.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*
* @return number of not ending elements.
*/
public int printNotEndingNodes() {
int count = 0;
Node tmp = null;
if (head != null) {
tmp = head.next;
}
while (tmp != null) {
if (!tmp.element.isEnding()) {
System.out.print("Element #" + tmp.id);
System.out.println(": " + tmp.element.story);
count++;
}
tmp = tmp.next;
}
return count;
}
/**
* A method to update element.
* <p>
* Checks if there are elements to update. Take element id.
* Update element with that id.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*/
public void update() {
if (size != 0) {
int count = printNotEndingNodes();
if (count != 0) {
int id;
System.out.print("Enter element number: ");
Scanner scanner = new Scanner(System.in);
id = scanner.nextInt();
if (id <= maxId) {
if (size == 1 || id == 1) {
System.out.println("You can't update starting element.");
} else {
Node tmp = head;
while (tmp.next.id != id) {
tmp = tmp.next;
if (tmp.next == null) {
System.out.println("Element number " + id + " doesn't
exist.");
return;
}
}
if (!tmp.next.element.isEnding()) {
System.out.print("Enter new story: ");
Scanner updateScanner = new Scanner(System.in);
String story = updateScanner.nextLine();
tmp.next.element.updateStory(story);
} else {
System.out.println("You can't update ending element.");
}
}
} else {
System.out.println("Element number " + id + " doesn't
exist.");
}
} else {
System.out.println("There are no elements to update.");
}
} else {
System.out.println("There are no elements.");
}
}
/**
* A method to print nodes.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*/
public void printNodes() {
Node tmp = head;
while (tmp != null) {
System.out.print("Element #" + tmp.id);
if (tmp.element.isEnding()) {
System.out.print(" (Ending)");
}
System.out.println(": " + tmp.element.story);
tmp = tmp.next;
}
}
/**
* A method to initialise Choose Your Adventure App.
* <p>
* Creates start element. Take number of ending elements.
* Creates ending elements.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*/
public void init() {
System.out.print("Enter story for start element: ");
Scanner scanner = new Scanner(System.in);
String story = scanner.nextLine();
root = new Element(null, null, story, false);
head = new Node(null, root, ++maxId);
size++;
System.out.print("Enter the number of ending elements: ");
int num = scanner.nextInt();
size += num;
for (int i = 0; i < num; i++) {
Scanner scanner1 = new Scanner(System.in);
int number = i + 1;
System.out.print("Enter story for ending element #" + number + ":
");
String endingStory = scanner1.nextLine();
Element element = new Element(null, null, endingStory, true);
Node node = new Node(null, element, ++maxId);
Node tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
tmp.next = node;
}
}
/**
* A method to print nodes with free links.
* <p>
* Goes through the list of nodes. Prints nodes with free links and
keeps
* their ids.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*
* @return ids nodes with free links.
*/
public int[] printNodesWithFreeLinks() {
Node tmp = head;
int[] ids = new int[maxId];
int i = 0;
while (tmp != null) {
if ((tmp.element.left == null || tmp.element.right == null)
&& !tmp.element.isEnding()) {
System.out.println("Element #" + tmp.id + ": " +
tmp.element.story);
ids[i] = tmp.id;
i++;
}
tmp = tmp.next;
}
return ids;
}
/**
* A method to add link.
* <p>
* Finds nodes with ids from and to.
* Adds link between them.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*
* @param from - node id from the element of which the link will
go.
* @param to - node id to the element of which the link will
go.
*/
private void addLink(int from, int to) {
Element fromElement = null, toElement = null;
Node tmp = head;
while (tmp != null) {
if (tmp.id == from) {
fromElement = tmp.element;
}
if (tmp.id == to) {
toElement = tmp.element;
}
tmp = tmp.next;
}
assert fromElement != null;
if (fromElement.left == null) {
fromElement.left = toElement;
} else {
fromElement.right = toElement;
}
}
/**
* A method to remove link.
* <p>
* Finds nodes with ids from and to.
* Removes link between them.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*
* @param from - node id from the element of which the link will be
removed.
* @param to - node id to the element of which the link will be
removed.
*/
private void removeLink(int from, int to) {
Node tmp = head;
Element elementFrom = null, elementTo = null;
while (tmp != null) {
if (tmp.id == from) {
elementFrom = tmp.element;
}
if (tmp.id == to) {
elementTo = tmp.element;
}
tmp = tmp.next;
}
if (elementFrom != null) {
if (elementFrom.right != null) {
if (elementFrom.right == elementTo) {
elementFrom.right = null;
}
}
if (elementFrom.left != null) {
if (elementFrom.left == elementTo) {
elementFrom.left = null;
}
}
}
}
/**
* A method to add link.
* <p>
* Checks of there are elements to link.
* Asks the user between which elements he wants to add a
link.
* Calls private addLink method.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*/
public void addLink() {
int[] ids = printNodesWithFreeLinks();
if (ids.length == 0) {
System.out.println("There are no elements to link.");
return;
}
if (ids[0] == 0) {
System.out.println("There are no elements to link.");
return;
}
System.out.println("Choose which element you want to add the link
to: ");
System.out.print("Enter element number: ");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
boolean idExists = false;
for (int i = 0; i < ids.length; i++) {
if (id == ids[i]) {
idExists = true;
}
}
if (idExists) {
System.out.println("Choose which element you want to link to:
");
printNodes();
System.out.print("Enter element number: ");
int nextId = scanner.nextInt();
if (nextId != id) {
addLink(id, nextId);
} else {
System.out.println("You cannot link an element to itself");
}
} else {
System.out.println("Element with id = " + id + " doesn't
exist.");
}
}
/**
* A method to remove link.
* <p>
* Asks the user between which elements he wants to remove a
link.
* Calls private removeLink method.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*/
public void removeLink() {
System.out.println("Select the elements between which you want to
remove the link: ");
printNodes();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the element number the link you want to
remove from: ");
int from = scanner.nextInt();
System.out.print("Enter the element number the link you want to
remove: ");
int to = scanner.nextInt();
removeLink(from, to);
}
/**
* A method to return number of elements.
* <p>
* Time complexity: O(1)
* Space complexity: O(1).
*
* @return number of elements.
*/
public int numberOfElements() {
return size;
}
/**
* A method to add element.
* <p>
* Takes story from creator. Creates element and adds it to
list.
* <p>
* Time complexity: O(n)
* Space complexity: O(n).
*/
public void addElement() {
String story;
System.out.print("Enter story: ");
Scanner scanner = new Scanner(System.in);
story = scanner.nextLine();
Element element = new Element(null, null, story, false);
Node node = new Node(null, element, ++maxId);
if (head == null) {
head = node;
} else {
Node tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
tmp.next = node;
}
size++;
}
/**
* A method to remove element.
* <p>
* Checks if there are elements to remove.
* Takes element number to remove. Removes if element exists.
* Remove links connected with that element.
* <p>
* Time complexity: O(n^2)
* Space complexity: O(n).
*/
public void removeElement() {
if (size != 0) {
int count = printNotEndingNodes();
if (count != 0) {
int id;
System.out.print("Enter element number: ");
Scanner scanner = new Scanner(System.in);
id = scanner.nextInt();
if (id <= maxId) {
if (size == 1 || id == 1) {
System.out.println("You can't remove starting element.");
} else {
Node tmp = head;
while (tmp.next.id != id) {
tmp = tmp.next;
if (tmp.next == null) {
System.out.println("Element number " + id + " doesn't
exist.");
return;
}
}
if (!tmp.next.element.isEnding()) {
for (int i = 1; i <= maxId; i++) {
if (i != id) {
removeLink(id, i);
removeLink(i, id);
}
}
tmp.next = tmp.next.next;
} else {
System.out.println("You can't remove ending element.");
}
}
size--;
} else {
System.out.println("Element number " + id + " doesn't
exist.");
}
} else {
System.out.println("There are no elements to remove.");
}
} else {
System.out.println("There are no elements.");
}
}
private static class Element {
private Element left;
private Element right;
private String story;
private boolean ending;
private static String storyLine;
private int timesVisited;
public Element(Element left, Element right, String story,
boolean ending) {
this.left = left;
this.right = right;
this.story = story;
this.ending = ending;
}
/**
* A method to add story to storyLine.
* <p>
* Time complexity: O(1)
* Space complexity: O(1).
*
* @param story - story to add.
*/
public static void addStoryLine(String story) {
storyLine = storyLine.concat(story);
}
/**
* A method to return isEnding.
* <p>
* Time complexity: O(1)
* Space complexity: O(1).
*
* @return is ending element.
*/
public boolean isEnding() {
return ending;
}
/**
* A method to update story.
* <p>
* Time complexity: O(1)
* Space complexity: O(1).
*
* @param story - new story.
*/
public void updateStory(String story) {
this.story = story;
}
/**
* A method to remove story.
* <p>
* Time complexity: O(1)
* Space complexity: O(1).
*/
public void removeStory() {
story = "";
}
/**
* A method to print times visited.
* <p>
* Time complexity: O(1)
* Space complexity: O(1).
*/
public void printTimesVisited() {
System.out.println("This element was " + timesVisited + " times
visited.");
}
}
private static class Node {
private Node next;
private Element element;
private int id;
public Node(Node next, Element element, int id) {
this.next = next;
this.element = element;
this.id = id;
}
}
}
Sample Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.
Choose your own adventure games, movies and books are a not so popular sub section of...
You will create your own silly story based on information provided by the user. For example, the parts in bold were entered by the user. For ideas see: Mad Libs. My Silly Story name: Frank Zhang whole number: 345 body part: stomach noun: cup floating point number: 1.23 clothing article: hat destination: Colorado goal: degree The resulting story is: Congratulations! Today is your 345 day. You're off to Colorado! You're off and away! You have brains in your stomach, You...
Go back to In Class Lab A where you created a Game Menu. Modify your code so that the user can input one of the four choices. Use an if/else statement so that depending on the user’s choice, the correct game will play. If the user inputs a 4 (Exit), your code exits without playing any game. For the MATH and MADLIBS choices, use your code from Lab 1. For the CHOOSE YOUR OWN ADVENTURE choice, write the code. Write...
***JAVA: Please make "Thing" movies.
Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
The following is for java programming. the classes
money date and array list are so I are are pre made to help with
the coding so you can resuse them where applicable
Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...
Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
Hello,
In need of help with this Java pa homework. Thank you!
2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or snap beans). Following the steps shown in "Assignment: Tutorial on Creating Classes in IntelliJ", create the class in a file of its own in the same package as class Main. Name the class an appropriate name. The class must have (a) private field variables of appropriate...
Assignment Requirements
I have also attached a Class Diagram that describes the
hierarchy of the inheritance and interface behaviors . The link to
the PDF of the diagram is below
MotorVehical.pdf
Minimize File Preview
User Define Object Assignment:
Create a Intellij Project. The
Intellij project will contain three user defined
classes. The project will test two of the User Define Classes by
using the invoking each of their methods and printing the
results.
You are required to create three UML...
Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...