  ## 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.

### 3.1.0    (example, DO NOT submit)

int n = 100;

for (int i = 0; i <= n; i++) {

if (i*i == n) {

System.out.println(i);

break;

}

}

### 3.1.0    (Answers, example)

1)      Calculates and prints the square root of non-negative numbers whose square root is an integer.

2)      Variables:

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.

3)      Improvements:

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.

### 3.1.1

int n = 152; int c = 1;

while (++c < n) { if (n % c == 0) { System.out.println(c); }}

### 3.1.2

Scanner is = new Scanner(System.in);

System.out.print("> ");

String l = is.nextLine();

for (int cc = l.length(); cc > 0; --cc)

System.out.print(l.charAt(cc - 1));

System.out.println();

### 3.1.3

int a = (int)(100 * (Math.random()+1));

int b = (int)(100 * (Math.random()+1));

int c = 0;

do { c += a; } while (--b > 0);

System.out.println(c);

### 3.1.4

for (int cn = 2; cn < 100; cn++) {

boolean ip = true;

for (int pn = 2; pn < cn; pn++) {

if (cn % pn == 0) { ip = false; }

}

if (!ip) continue; else System.out.println(cn);

} 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.  ### “Text encryption” is the action that takes a text that everybody can read and change its letters so that it can’t be read by people. You can’t just scramble the text, because you would want to turn it back to the original text sometime, and read it. The opposite operation is called “decryption”. There are many encryption/decryption algorithms, and it’s a whole branch of mathematics. In this case, we will use the simplest encryption algorithm, in which we replace any occurrence of a specific character with something else. Write a program called TextEncryption that encrypts text messages. It starts with these variables:

char replace = 'o';

char replaceWith = '*';

String originalSentence = "The quick brown fox jumps over the lazy dog";

String newSentence = "";

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:

Original sentence: The quick brown fox jumps over the lazy dog

Encrypted sentence: The quick br*wn f*x jumps *ver the lazy d*g

### Bonus (2 points)

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).

## What you should do

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.

### Bonus (3 points)

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. ### A new version of Turtle Jar. IMPORTANT!

·         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.

## The game rules

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:

#### Winning a game

The Million Shekel Drop!

You have 1000000 shekels.

The cash that you have at hand after 8 consecutive correct answers is yours!

You now have 1000000 shekels.

Next question:

(from Daniel  Yacouboff): In computers, what is the shortcut buttons for displaying your Desktop?

A) Ctrl + S

B) Shift + D

C) Winkey + D

D) Alt + F4

How much money would do you put on answer A (0-1000000): 0

How much money would do you put on answer B (0-1000000): 0

How much money would do you put on answer C (0-1000000): 500000

How much money would do you put on answer D (0-500000): 500000 (automatic)

The correct answer is: C

You now have 500000 shekels.

Next question:

(from Daniel Yacouboff): What is the name of the tallest mountain in America?

A) Mount Kilimanjaro

B) Mount Hermon

C) Mount Everest

D) Mount Aconcagua

How much money would do you put on answer A (0-500000): 100000

How much money would do you put on answer B (0-400000): 100000

How much money would do you put on answer C (0-300000): 100000

How much money would do you put on answer D (0-200000): 200000 (automatic)

The correct answer is: D

You now have 200000 shekels.

Next question:

(from Eli Agiv): In the Bible who interpreted the  dreams of Pharaoh?

A) Samuel

B) Daniel

C) David

D) Joseph

How much money would do you put on answer A (0-200000): 0

How much money would do you put on answer B (0-200000): 0

How much money would do you put on answer C (0-200000): 0

How much money would do you put on answer D (0-200000): 200000 (automatic)

The correct answer is: D

You now have 200000 shekels.

Next question:

(from Eli Agiv): Who defeated North Korea 7-0 in a group match in Football World Cup 2010?

A) Portugal

B) Uruguay

C) Australia

D) Chile

How much money would do you put on answer A (0-200000): 200000

The correct answer is: A

You now have 200000 shekels.

Next question:

(From Gadi Jacobson): What is the height of mountain Everest:

A) 9712 meters

B) 8848 meters

C) 9315 meters

D) 10102 meters

How much money would do you put on answer A (0-200000): 0

How much money would do you put on answer B (0-200000): 200000

The correct answer is: B

You now have 200000 shekels.

Next question:

(from Eli Agiv): Who were third in Football World Cup 2010?

A) France

B) Slovenia

C) Germany

D) England

How much money would do you put on answer A (0-200000): 0

How much money would do you put on answer B (0-200000): 100000

How much money would do you put on answer C (0-100000): 100000

The correct answer is: C

You now have 100000 shekels.

Next question:

(from Natalie Dubman): Who was the second Israeli PM?

A) Moshe Sharett

B) David Ben-Gurion

C) Igal Allon

D) Levi Eshkol

How much money would do you put on answer A (0-100000): 100000

The correct answer is: A

You now have 100000 shekels.

Next question:

(from Eli Agiv):  What caused lot of noise in Football World Cup 2010?

A) Bagpiper

B) Organ

C) Vuvuzela

D) Accordion

How much money would do you put on answer A (0-100000): 0

How much money would do you put on answer B (0-100000): 0

How much money would do you put on answer C (0-100000): 50000

How much money would do you put on answer D (0-50000): 50000 (automatic)

The correct answer is: C

You are going home with 50000 shekels.

Bye!

#### Losing a game

The Million Shekel Drop!

You have 1000000 shekels.

The cash that you have at hand after 8 consecutive correct answers is yours!

You now have 1000000 shekels.

Next question:

(from Daniel  Yacouboff): In computers, what is the shortcut buttons for displaying your Desktop?

A) Winkey + D

B) Ctrl + S

C) Shift + D

D) Alt + F4

How much money would do you put on answer A (0-1000000): 500000

How much money would do you put on answer B (0-500000): 500000

The correct answer is: A

You now have 500000 shekels.

Next question:

(from Eli Agiv): What year Yitzhak Rabin served as Chief of Staff?

A) 1968 to 1972

B) 1974 to 1978

C) 1964 to 1968

D) 1949 to 1952

How much money would do you put on answer A (0-500000): 0

How much money would do you put on answer B (0-500000): 0

How much money would do you put on answer C (0-500000): 0

How much money would do you put on answer D (0-500000): 500000 (automatic)

The correct answer is: C

You have no more cash left. You lose.

You are going home with 0 shekels.

Bye!

## 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.

## Validating user input (added October 26)

·         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:

Next question:

(from Gadi Ickowicz): In the Teenage Mutant Ninja Turtles, what is the name of Splinter's master?

A) Leonardo

B) Oroku Saki

C) Hatori Hanzo

D) Hamato Yoshi

How much money would do you put on answer A (0-1000000): -500

You don't have that amount of cash.

How much money would do you put on answer A (0-1000000): 5000000

You don't have that amount of cash.

How much money would do you put on answer A (0-1000000): 950000

How much money would do you put on answer B (0-50000): 45500

How much money would do you put on answer C (0-4500): 5000

You don't have that amount of cash.

How much money would do you put on answer C (0-4500): -5000

You don't have that amount of cash.

How much money would do you put on answer C (0-4500): 500

How much money would do you put on answer D (0-4000): 4000 (automatic)

The correct answer is: D

You now have 4000 shekels.

Next question: ·         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.

·         TextEncryption.java

·         TurtlePolygons.java

·         TheMillionShekelDrop.java

All your answers should be in English and Java.

Good luck!

© 2010 Boaz Kantor, IDC