Question
Objectives To perform while (or do-while) loops. To access individual characters in a String. To use mathematical reasoning to solve a problem. Description Write a
Objectives
To perform while (or do-while) loops.
To access individual characters in a String.
To use mathematical reasoning to solve a problem.
Description
Write a program (starTree.scala) that reads in a line of text and then outputs the text vertically along with a tree drawn of stars of the same height. (See the example run.)
Sample Run
The following is a sample run. It includes both input and output. For clarity, the input lines are preceded by the symbol > and the shell command (to start the program) is preceded by the symbol $.
$ scala starTree.scala Please enter a word. > EXAMPLE E * E X *** X A ***** A M ******* M P ********* P L *********** L E*************E
Observe that the first line contains 1 star, then 3 stars, 5 stars, etc. Each line is centered between the two word columns. And the last line, which determines the width, has the stars touching the two word columns.
Hint
To access the i-th entry in a String called word, use word(i), with word(0) being the first character.
To get the length of the word, use word.length
You will want to increment an index from 0 to the last character in the word (length minus 1).
To determine the number of stars to draw per line, see if there is a pattern between the index and the number of stars.
To determine the gap (of spaces) to draw per line, see again if there is some pattern between the gap and the index.
Remember that " "*x would print out x spaces, where x is some integer expression. " "*(3+1) would do 4 spaces.
Develop this program in STAGES. What I mean is for you to start with a small part of the problem and add features piece by piece until you achieve the final solution. Do one step, test it multiple times, and proceed only once that step works. Here is one suggested line of attack:
Start by having the program read the input and just print out the first letter of the word - word(0)
Now make it print out every letter of the word one line at a time. This requires wrapping everything into a while loop where you have a variable i (initially 0) that increments each iteration and prints out word(i). It should stop when i reaches word.length.
Now modify the code so it prints each letter, followed by a single *, followed by the letter again. Don't worry about spaces or more than one star yet.
Now modify it so it prints out the right number of stars. To print more than one star, you can use "*"*X where X is some expression. That expression can be a variable j that starts at 1 and increments by the proper amount. Or you can compute it from i directly. Notice that when i=0, it should print 1 star; when i=1, it should print 3 stars, then 5 stars, etc. See if you can spot the pattern and then get the right number of stars. Don't worry about spaces yet.
Now make it print the right number of spaces. This is again " "*X -- where the expression X will again depend on i (and the word.length).
Once you have that, you should be all done.
Need in scala
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