Your country is at war and your enemies are using a secret

Your country is at war and your enemies are using a secret code to communicate with one another. You have managed to intercept a message that reads as follows:

:mmZdxZmx]Zpgy

The message is obviously encrypted using the enemy’s secret code. You have just learned that their encryption method is based upon the ASCII code (see Appendix 7). Individual characters in a string are encoded using this system. For example, the character ‘A’ is encoded using the number 65 and ‘B’ is encoded using the number 66. Your enemy’s secret code takes each letter of the message and encrypts it as follows:

if (OriginalChar + Key > 126) then
EncryptedChar = 32 + ((OriginalChar + Key) − 127)
else
EncryptedChar = (OriginalChar + Key)

For example, if the enemy uses Key = 10 then the message “Hey” would initially be represented as:

Character  ASCII code
H                            72
e                           101
y                           121

And “Hey” would be encrypted as:

Encrypted H = (72 + 10) = 82 = R in ASCII
Encrypted e = (101 + 10) = 111 = o in ASCII
Encrypted y = 32 + ((121 + 10) − 127) = 36 = $ in ASCII

Consequently, “Hey” would be transmitted as “Ro$.” Write a Java program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. You can assume that the original message consists entirely of ASCII codes that represent only printable characters. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys, the message will appear as gibberish. Since there are only 100 keys this would obviously be a pretty crummy encryption system. This Programming Project will require you to explore a bit on your own how to convert from a char to a number, process the number, then convert it back to a char. See Chapter 2 for String methods. You will want to use charAt(). Important: Note that the secret code has a so you will need to escape encode it by using \ if you hard-code it in your program.

Add methods to the Person class from Self-Test Question 16 to perform

Add methods to the Person class from Self-Test Question 16 to perform the following tasks:

  • Set the name attribute of a Person object.
  • Set the age attribute of a Person object.
  • Test whether two Person objects are equal (have the same name and age).
  • Test whether two Person objects have the same name.
  • Test whether two Person objects are the same age.
  • Test whether one Person object is older than another.
  • Test whether one Person object is younger than another.

Write a driver (test) program that demonstrates each method, with at least one true and one false case for each of the methods tested.

You have an augmented reality game in which you catch Edoc and

You have an augmented reality game in which you catch Edoc and acquire Edoc candy. You need 12 candies to evolve an Edoc into a Margorp. An evolution earns you back one candy. Each evolution also earns you 500 experience points. An Edoc or Margorp can each be transferred for one Edoc candy. In support of the game’s players, write an Edoc calculator program that inputs the number of Edoc you have caught and the number of Edoc candies in your possession. You can assume the initial number of Margorps is 0. The program should output the maximum number of experience points you can earn through transfers and evolutions. After Edocs evolve into Margorps, your program should consider if transferring the Margorps will result in enough candy to evolve even more Edoc.

For example, if you start with 71 candies and 53 Edoc, the program could output the following. Note that there are many other sequences of transfers and evolutions, with possibly a different final number of Edoc and Margorp, but the total number of experience points should be the same (the max possible):

Transfer 37 Edoc and 0 Margorp resulting in 108 candy, 16 Edoc, and 0 Margorp
Evolve 9 Edoc to get 4500 experience points and resulting in 9 candy, 7 Edoc, and 9 Margorp
Transfer 0 Edoc and 9 Margorp resulting in 18 candy, 7 Edoc, and 0 Margorp
Evolve 1 Edoc to get 500 experience points and resulting in 7 candy, 6 Edoc, and 1 Margorp
Transfer 4 Edoc and 1 Margorp resulting in 12 candy, 2 Edoc, and 0 Margorp
Evolve 1 Edoc to get 500 experience points and resulting in 1 candy, 1 Edoc, and 1 Margorp

Total experience points = 5500

Imagine a program that compresses files by 80 percent and stores them

Imagine a program that compresses files by 80 percent and stores them on storage media. Before the compressed file is stored, it must be divided into blocks of 512 bytes each. Develop an algorithm for this program that first reads the number of blocks available on the storage media. Then, in a loop, read the uncompressed size of a file and determine whether the compressed file will fit in the space left on the storage media. If so, the program should compress and save the file. It continues until it encounters a file that will exceed the available space on the media.

For example, suppose the media can hold 1000 blocks. A file of size 1100 bytes will compress to size 880 and require 2 blocks. The available space is now 998 blocks. A file of size 20,000 bytes will compress to size 16,000 and require 32 blocks. The available space is now 966.

Write a JavaFx application that displays a series of pictures of a

Write a JavaFx application that displays a series of pictures of a person with arms, legs, and of course a head. Use a happy face for the head. Use ovals for the body, arms, and legs. Draw a sequence of figures that appear one after the other, as in Listing 4.9. Make the figures assume a running position. Change the color of the person’s face in each succeeding figure, going from white to pink to red to yellow to green. Have the smiling face gradually change its mouth shape from a smile on the first person to a frown on the last person. Use a switch statement to choose the color. Embed the switch statement in a loop.

Listing 4.9

Repeat the previous project, but position the centers of the circles on

Repeat the previous project, but position the centers of the circles on a spiral. The center of each circle will depend on both an angle and a distance from the origin. A constant change in both the angle and the distance will result in a spiral pattern.

Previous project

Create a JavaFx application that draws a pattern of evenly spaced circles. Use four constants to control the pattern: the number of circles to draw, the radius of the first circle, the change in the radius of each subsequent circle, and the change in the x-coordinate of the circle. Cycle the colors of the circles through red, green, and blue.

Suppose that you work for a beverage company. The company wants to

Suppose that you work for a beverage company. The company wants to know the optimal cost for a cylindrical container that holds a specified volume. Write a fragment of code that uses an ask-before-iterating loop. During each iteration of the loop, your code will ask the user to enter the volume and the radius of the cylinder. Compute and display the height and cost of the container. Use the following formulas, where V is the volume, r is the radius, h is the height, and C is the cost.

h = V/πr2

C = 2πr(r + h)

Repeat Programming Project 7, but write the program as JavaFx application. Use

Repeat Programming Project 7, but write the program as JavaFx application. Use a constant for the initial velocity of the ball. Draw a circle for the position of the ball at each second. The ycoordinate should be proportional to the height of the ball, and the x-coordinate should change by a small constant amount.

Programming Project 7

Write a program that simulates a bouncing ball by computing its height in feet at each second as time passes on a simulated clock. At time zero, the ball begins at height zero and has an initial velocity supplied by the user. (An initial velocity of at least 100 feet per second is a good choice.) After each second, change the height by adding the current velocity; then subtract 32 from the velocity. If the new height is less than zero, multiply both the height and the velocity by −0.5 to simulate the bounce. Stop at the fifth bounce. The output from your program should have the following form:

Enter the initial velocity of the ball: 100
Time: 0 Height: 0.0
Time: 1 Height: 100.0
Time: 2 Height: 168.0
Time: 3 Height: 204.0
Time: 4 Height: 208.0
Time: 5 Height: 180.0
Time: 6 Height: 120.0
Time: 7 Height: 28.0
Bounce!
Time: 8 Height: 48.0
. . .

Write a program to answer questions like the following: Suppose the species

Write a program to answer questions like the following: Suppose the species Klingon ox has a population of 100 and a growth rate of 15 percent, and it lives in an area of 1500 square miles. How long would it take for the population density to exceed 1 per square mile? Use the class Species in Listing 5.19 with the addition of the getDensity method from Self-Test Question 10.

Listing 5.19

Self-Test Question 10.

Define a method called getDensity that could be added to the definition of the class SpeciesSecondTry in Listing 5.6. This method has one parameter of type double that is named area. The parameter area gives the area occupied by the species, expressed in square miles. The method getDensity returns a value of type double that is equal to the number of individuals per square mile of the species. You can assume that the area is always greater than zero. The definition is very short.

Create a class that represents a grade distribution for a given course.

Create a class that represents a grade distribution for a given course. Write methods to perform the following tasks:

  • Set the number of each of the letter grades A, B, C, D, and F.
  • Read the number of each of the letter grades A, B, C, D, and F.
  • Return the total number of grades.
  • Return the percentage of each letter grade as a whole number between 0 and 100, inclusive.
  • Draw a bar graph of the grade distribution.

The graph will have five bars, one per grade. Each bar can be a horizontal row of asterisks, such that the number of asterisks in a row is proportionate to the percentage of grades in each category. Let one asterisk represent 2 percent, so 50 asterisks correspond to 100 percent. Mark the horizontal axis at 10 percent increments from 0 to 100 percent, and label each line with its letter grade.

For example, if the grades are 1 A, 4 Bs, 6 Cs, 2 Ds, and 1 F, the total number of grades is 14, the percentage of As is 7, the percentage of Bs is 29, the percentage of Cs is 43, the percentage of Ds is 14, and the percentage of Fs is 7. The A row would contain 4 asterisks (7 percent of 50 rounded to the nearest integer), the B row 14, the C row 21, the D row 7, and the F row 4. The graph would look like this: