Introduction Your task is to write a program that outputs a story to the console
ID: 3910146 • Letter: I
Question
Introduction
Your task is to write a program that outputs a story to the console window. We’ll use the “Star Wars” story format as outlined below for this program. Your goal is to first reproduce this output, and then tune your software to eliminate repeated lines of code by moving them to methods and calling those methods. For many of the works in the Star Wars universe, a common story telling device is used for the introduction:
//TitleOfStory
“A long time ago,”
“In a galaxy far, far away…”
//IntroductionToStory
“There was much strife and unrest.”
//RemainderOfStory
“<camera pans downward past stars as the string section enters>”
In your software, you are free to use any text for lines 1,4 and 6 above, but the rest of the script should stay the same.
Your First Program
The first program will simply clone the output from above and tell two different stories. These two stories will be the same text for most lines above, but each will have unique text for the title, the content of the story, and the remainder of the story. For example, you could retell Lucas’ “The Phantom Menace” and make up your own “Star Wars Story” resulting in the following output:
“Episode 1 : The Phantom Menace”
“A long time ago,”
“In a galaxy far, far away…”
“Turmoil has engulfed the Galactic Republic.”
“There was much strife and unrest.”
“The Supreme Chancellor has secretly dispatched two Jedi Knights to settle the conflict.”
“<camera pans downward past stars as the string section enters>”
“Episode 7 : To Be Announced”
“A long time ago,”
“In a galaxy far, far away…”
“Han Solo discovers he can use both the Force and a whip, making him better than Luke.”
“There was much strife and unrest.”
“Can Solo pick up the pieces of the fragmented Republic and restore the Jedi Council?”
“<camera pans downward past stars as the string section enters>”
While there are many ways to write this output to the console, your eventual goal will be to make use of methods and method calls for elements of the story that are static and repeated over and over. When you are done with this program, it will be inspected for the following characteristics:
Lines of code are short and easy to understand
The program compiles and runs without errors
The output of the program is similar to the output above (two stories that vary on lines 1,4, and 6 as described above)
The program should contain no duplicate “System.out.println” statements.
If you find yourself copying and pasting println statements, consider how you could condense this code further with methods
Name Instructions
You must name the files “TellingStories.java” and “TellingDynamicStories.java”
You must name your methods (for both files) as follows:public static void main(String[] args) //every program will have one of these
main will either call “TellStoryOne()” and “TellStoryTwo()” in the first file, or call “tellDynamicStory(…)” in the second file, but the main method will be quite short.
public static intro()
public static middle()
public static outro()
public static tellStoryOne() //should call intro(), middle() and outro()
public static tellStoryTwo() //same as above
public static tellDynamicStory(String title, String content, String remainder)
This function will be provided the input from main, which will use a Scanner to ask the user for each of the strings, as demonstrated in the output above
Explanation / Answer
Below is your code.
TellingStories.java
public class TellingStories
{
public static void main(String[] args) {
tellStoryOne();
System.out.println();
tellStoryTwo();
}
public static void intro() {
System.out.println("A long time ago,");
System.out.println("In a galaxy far, far away...");
}
public static void middle() {
System.out.println("There was much strife and unrest.");
}
public static void outro() {
System.out.println("<camera pans downward past stars as the string section enters>");
}
public static void tellStoryOne() {
System.out.println("Episode 2 : Attack of the Clones");
intro();
System.out.println("The Galactic Republic has gone under attack by Jar Jar Binks' horrible CGI.");
middle();
System.out.println("The Supreme Chancellor has secretly ordered two Jedi Knights to remove Jar Jar from the Star Wars Canon.");
outro();
}
public static void tellStoryTwo() {
System.out.println("Episode 8 : Attack of the Clones Part II");
intro();
System.out.println("A descendant of the evil Jar Jar Binks is working with Kylo Ren to rebuild the Empire along with another Death Star.");
middle();
System.out.println("Can the Resistance prevent Darth Binks and Darth Ren from overthrowing the Galaxy?");
outro();
}
}
TellingDynamicStories.java
public class TellingDynamicStories
{
public static void main(String[] args)
{
tellDynamicStory();
}
public static void tellDynamicStory()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the Title of the Story:");
String title = keyboard.nextLine();
System.out.println("Please enter the Content of the Story:");
String content = keyboard.nextLine();
System.out.println("Please enter the Remainder of the Story:");
String remainder = keyboard.nextLine();
System.out.println();
System.out.println(title);
intro();
System.out.println(content);
middle();
System.out.println(remainder);
outro();
}
public static void intro() {
System.out.println("A long time ago,");
System.out.println("In a galaxy far, far away...");
}
public static void middle() {
System.out.println("There was much strife and unrest.");
}
public static void outro() {
System.out.println("<camera pans downward past stars as the string section enters>");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.