I have a deadline of Saturday the 15th on this assignment and need help desperat
ID: 3630451 • Letter: I
Question
I have a deadline of Saturday the 15th on this assignment and need help desperately!!
Here is the problem:
Once upon a time in a kingdom far away, the king hoarded food and the people starved. His
adviser recommended that the food stores be used to help the people, but the king refused. One
day a small group of rebels attempted to kill the king but were stopped by the adviser. As a
reward, the adviser was granted a gift by the king. The adviser asked for a few grains of wheat
from the king's stores to distribute to the people. The number of grains was to be determined by
placing them on a chessboard. On the first square of the chessboard, he placed one grain of
wheat. He then placed two grains on the second square, four grains on the third square, eight
grains on the fourth square, and so forth.
Compute the total number of grains of wheat that were placed on k squares by writing a recursive
method getTotalGrains(k, grains). Each time getTotalGrains is called, it "places" grains on a
single square; grains is the number of grains of wheat to place on that square. If k is 1, return
grains. Otherwise, make a recursive call, where k is reduced by 1 and grains is doubled. The
recursive call computes the total number of grains placed in the remaining k - 1 squares. To find
the total number of grains for all k squares, add the result of the recursive call to grains and
return that sum.
In addition to the above, write "The number of grains placed on the board is " + the amount
returned by getTotalGrains(k,grains) to a text file named Grains.txt. Plus, write the code that
will read the text file and display the same result in a JOptionPane.showMessageDialog box. As
in:
JOptionPane.showMessageDialog(null,inputStream.nextLine());
Make certain you import the JOptionPane class.
This is a text based program.
Thank you for whoever answers, I will take care of you on Karma Points!!
Explanation / Answer
Try this: import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class GrainProblem { public static int getTotalGrains(int k, int grains){ if (k == 1){ return grains; } else{ return grains + getTotalGrains(k-1, grains*2); } } public static void main(String[]args) throws IOException{ Scanner keyboard = new Scanner(System.in); System.out.println("Enter the number of squares."); int k = keyboard.nextInt(); System.out.println("Enter the number of grains in the first square."); int grains = keyboard.nextInt(); File file = new File("Grains.txt"); if (file.exists()){ System.out.println("The file already exists and will be overwritten."); } else{ file.createNewFile(); } PrintWriter printer = new PrintWriter(file); printer.print("The number of grains placed on the board is " + getTotalGrains(k, grains)); printer.close(); Scanner inputStream = new Scanner(file); JOptionPane.showMessageDialog(null,inputStream.nextLine()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.