Question: Part 0 - Download Code Template Download the following code template files for use as starter code: Lab 4 . java Download Lab 4 .

Part 0- Download Code Template
Download the following code template files for use as starter code: Lab4.java Download Lab4.java
As you progress through the lab, look over the relevant starter code file and make sure you know what material has been provided for you.
Part 1
Starting with one grain of rice, double the number of grains of rice you receive each day. If a king paid you for 64 days, how many grains of rice would you have? Write a short, method with the name countGrains that calculates and prints the number of grains you earn each day and the running sum of all the grains to date.
Note: Do you observe anything unusual in your output when you run your code? Explain, in comments.
Day 1 and you got 1 grain(s) of rice for a total of 1 grain(s).
Day 2 and you got 2 grain(s) of rice for a total of 3 grain(s).
Day 3 and you got 4 grain(s) of rice for a total of 7 grain(s).
...
Day 30 and you got X grain(s) of rice for a total of Y grain(s).
Day 31 and you got X grain(s) of rice for a total of Y grain(s).
Day 32 and you got X grain(s) of rice for a total of Y grain(s).
...
Part 2
Single Loops
Write a method powerOfTwo that takes an integer argument named exponent and prints to the console what 2 is raised to the exponent. For example,
powerOfTwo(10); //should print: "2 to the power of 10 is 1024"
powerOfTwo(4); //should print: "2 to the power of 4 is 16"
Note: you are not allowed to use any library functions!
Write a method numBackward that reverses the digits of a given integer (that is, an integer passed in as an argument). Use a single loop and modular arithmetic to accomplish this goal. If you don't want to use modular arithmetic, you can also use String methods. Play around with ideas on paper first. After you reverse the digits, compare the number you got with the original argument and determine if the original argument is a palindrome. Your method should print a message to the screen that states the reversed number and whether or not it is a palindrome. For example,
numBackward(123456789)//should print: "backward: 987654321, not palindrome!"
numBackward(123454321)//should print: "backward: 123454321, palindrome!"
Part 3
Nested Loops
In this section, well produce more complex output by wrapping one loop inside another. This so-called nesting produces more complex results at the cost of increasing program complexity, and in the worst case can make some programs intractable. Well limit our degree of nesting to k ==2 here to get started. Build a method called StraightLine that produces a straight line, just as in loop 0 in section 2 above. Use a loop variable called size to terminate the loop. The output should look like:
***//size ==3
*****//size ==5
Now, wrap this loop that produces a line inside another loop, as demonstrated in loop 1 in section 2. Make the outer loop also terminate using the "size" variable. The only thing to add to the outer loop is a System.out.println(); to move the cursor to the next line. When youre done, the output should look like the square below, with the same number of rows and columns.
***//size ==3
***
***
*****//size ==5
*****
*****
*****
*****
Part 4
Nested Loops
Write a method called BoxMaker that asks the user for an integer x (using Scanner), and then builds a box of x asterisks. For example, if x ==4, then your output would be:
****//x ==4
**//inner body" ": x -numLids("*"on each side)== x -2
**
****

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!