This is a warm-up section. In this part you practice reading and understanding algorithms based on flow control structures. Submit your answers in a file called Answers_3.1.txt. Please answer in English.
Inspect the four code segments given below, find the logic behind them and answer these questions:
1. Describe in one sentence what the algorithm is intended to do (don’t translate the code to English).
2. Describe the role of each variable and suggest meaningful names to all the variables.
3. Suggest ways to improve the efficiency of the code, without changing the algorithm output.
1) Calculates and prints the square root of non-negative numbers whose square root is an integer.
a. n: the number we’re calculating the square root of. New name: number.
b. i: the current candidate of the calculation result. New name: currentSquareRoot.
a. Loop only until number/2 (make sure to handle number=1).
b. Use Math.sqrt(n), check if the result is an integer, print it if it is.
Here’s a good way to solve it (it’s called a “dry run”):
1. Take a piece of paper, write a table, where each column represents a single variable.
2. Write the initial values of the variables in the first line.
3. Run the iterations one by one. In each iteration, use the next empty line in the table to write the new values of all the variables.
4. Use another part of the paper to write the output.
5. Once you figured out the intention of the algorithm, try running the algorithm again with different numbers, and make sure you understand all the special cases.
6. Now create a new class in a new file, and copy the code to the main() method.
7. Fix the indentations, compile and run.
8. All you’ve got left to do is to think about the role of each variable, and you’re done.
If you’re unsure what a method does, read its API.
Flow control explanations can be found on Oracle’s site here.
Write a program called TextEncryption that encrypts text messages. It starts with these variables:
The program should go over the sentence in a loop, and replace the character given in the variable replace with the one given in the variable replaceWith. At the end of the program, print the resulting sentence. For example, the output of the above variables should be:
Make your code case insensitive, that is, replace every occurrence of the character replace, whether it's lower-case or upper-case.
· You may NOT use the various replace methods of class String!
· You can (and should) test your program with different characters and different sentences.
· When using loops, you should always consider extreme cases, such as the need to replace the first or last characters, consecutive characters to replace, a String which is made only by that character, etc.
In this part we will visualize loops. We do that by coding an algorithm which makes the turtle paint a polygon, and then another polygon and another and another. As opposed to last week, we are not going to write the code of each polygon, but rather use a loop to paint SOME polygon, and change the shape and size of the polygon on each iteration. By doing this, you will be able to see with your own eyes how loops work.
Write a program which draws, using our beloved Turtle object, the image below:
The image consists of a set of polygons with a different number of sides, ranging from 3 sides (the inner triangle) and up to 8 (the outer octagon).
1. Write a program called TurtlePolygons.
2. The program draws polygons, one inside another, where the smallest polygon is always the innermost one, and it’s always a triangle.
3. Each polygon with X sides is always smaller in size and inside the polygon with X+1 sides.
4. The program draws all the polygons with 3 sides and up to an octagon (8 sides), each polygon is drawn once (the order is not important).
5. Figure out your own formula to calculate the size of the polygons. We don’t care about the size, as long as the triangle is large enough to see and an octagon does not exceed the screen size.
6. Use finals (constants) wisely: the number 8 must be a final, and if we change it, the program should still draw the image it’s supposed to draw. Note however that the lower-limit of the image must not be less than 3 (you can assume that).
7. If you find out that integers are too rough for representing the angles, search the API for other methods which can serve your needs with better accuracy. DO NOT discuss these methods in the forum.
8. A good program should be 20-30 code lines (without comments).
9. As you can see, the turtle should not be visible when the program ends.
Always keep the same image size, with the same proportions, irrespective of how many polygons are drawn. I.e., adding more polygons (using the final constant) does not increase the sizes of the inner triangle and the outer polygon.
· Note: Please download this new version of Turtle.jar.
· The new API is here.
· This turtle is packed in the Default Package, which means you don’t import it.
Write a game called “The Million Shekel Drop”, based on the UK game show “The Million Pound Drop” (recently made Aliyah by Erez Tal). The program is based on a text file containing multiple-choice-questions (written by your classmates, and possible by yourself), where only 1 of 4 answers is correct. The game selects a random unanswered question from the file, prints it to the screen along with the 4 answers, which are printed on a random order.
1. The game is a single-player.
2. At the beginning of the game, the player is given 1,000,000 Shekels in cash.
3. The player is asked 8 questions. Each question has 4 answers, only one of which is correct.
4. The player has to bet ALL HIS MONEY on an answer, or split his money between several answers.
5. After the last bet, the player should not be left with cash. All the cash must be bet on answers. This means that if you’re showing the fourth answer, you should not wait for input, but rather automatically place the entire cash left on that answer (see sample output below).
6. When the bet is made, the correct answer is revealed. Next, two things happen:
a. The cash left on incorrect answers is dropped (lost).
b. The cash on the correct answer is given back to the player.
7. At any stage, if the player is left with no cash, they lose the game.
8. After 8 questions, the player can retire with the amount of cash left in their hands.
To understand how the program should handle special cases and what messages should be printed, inspect the sample game runs below:
Write a program called TheMillionShekelDrop, which runs the game according to the rules and the given output.
Printing a random question and its answers from the text file is handled by a class called Questionnaire. This class is included in a package called McqTrivia, which is packed in trivia.jar given.
1. Download trivia.jar from here, and add the jar to your CLASSPATH. Import the class McqTrivia.Questionnaire. Now you can use the class Questionnaire and instantiate an object by writing:
Questionnaire varname = new Questionnaire();
The API of Questionnaire is given here.
2. Use the Scanner class to handle the user's input.
3. Make sure your output and game run looks exactly according to the sample output above.
· In this exercise you should start thinking about the input-->processing-->output paradigm.
· Your game algorithm receives input from several sources; the user, another class (which receives input from a file), and constants.
· For this exercise, make sure you validate the input you receive from the user. For example, if the user is supposed to place a bet of 0-200000, then at least make sure the input is within that range. Inspect the following output sample for expected output:
· You can test your game by running it on the MCQ (Multiple Choice Questions) file given here.
Read the submission guidelines on the website.
Submit a .Zip file with the following files only (no .class files), with no folders. Print all these files and submit to Heder Avodot. Make sure both soft and hard copies are similar.
All your answers should be in English and Java.