/* * Midterm1InLab.java * Midterm 1 test for CS102, Fall 2009 * * @name Dale Reed // Replace my name with yours * @date Wednesday, September 23, 2009 * * Be sure to comment out sections of code that don't work before * you turn in your solution, since you get no points if your * program doesn't compile. See instructions for each of the problems * below. * * You MAY write additional methods if you feel that would be helpful. * Turnin this exam using the following command from your CS account: * * turnin -c cs102 -p midterm1 Mid1.java * * where the file with your code in it is called Mid1.java */ import java.util.Scanner; // used for console input commands public class Mid1 // For first Midterm { // For input from the keyboard Scanner keyboard = new Scanner(System.in); public static void main (String[] args) { // Replace my name with yours in the statement below System.out.println("Name: YOUR NAME!!! \n" + "Midterm Exam 1, CS 102, Fall 2009 \n"); // Create an object so I can call the two methods below Mid1 myMidterm1 = new Mid1(); // Calling two methods. myMidterm1.problem1(); // first problem myMidterm1.problem2(); // second problem // End of the midterm. System.out.println("\nEnd of Midterm1 problems.\n\n"); }// end main() /* ----------------------------------------------------------------- * Write the code for your solution to PROBLEM 1 inside the method * called problem1() below these comments. * * Write code to read in 7 integers (1..100) from the keyboard. Print * out the largest and the smallest numbers. * * For one set of input, running this would look like: * Inside Problem 1 * Enter 7 integers between 1 and 100: 41 9 23 45 91 7 13 * The largest and smallest are: 91 7 * * For another set of input, running this would look like: * Inside Problem 1 * Enter 7 integers between 1 and 100: 9 3 5 7 2 8 4 * The largest and smallest are: 9 2 */ private void problem1() { System.out.println("Inside Problem 1"); // Write your solution to problem 1 starting here... // ... }// end method problem1() /* ---------------------------------------------------------------- * Write the code for your solution to PROBLEM 2 inside the method * called problem2() below these comments. * * I recommend that you first turn in your completed solution * to problem 1, in case you don't finish this portion or it causes * your program not to compile. * * According to legend a sage appeared in the court of a king * and challenged him to a game of chess. The king, being an * expert player gladly accepted the challenge and agreed that * the loser would pay the winner in rice, with one grain of rice * on the first board square, two on the second, four on the * third, and so on, doubling the number of grains of rice for each * of the 64 board squares, paying off one square per day. * Over-confident, the king lost and began paying in rice, * placing one grain of rice on a square the first day, two on the * next square on the second day, and so on. The king began to * realize his mistake in agreeing to this payment, and that in * order to pay off his debt he would lose his kingdom. Being * a dishonorable monarch, rather than confess his predicament * to the sage, he cheated. * He did double the number of grains of rice on each square * each day for the first 20 days. Starting on day 21, he kept * adding the same number of grains of rice on each square, * only doubling this number every 4 days. In other words, there * were the same number of grains of rice added on the 21st, 22nd, 23rd, * and 24th squares. This number was then doubled on the 25th * square, with this new value added on the 26th, 27th, and 28th * squares, and so on. (After the last day, the sage learned of the * trickery and transformed himself into a swarm of beatles that ate * all the rice in the land.) * Write a program in Java that displays the number of grains * of rice added each day as well as the total number of * grains of rice accumulated on the board for each of the * 64 days. The first 27 lines of your output should look like * the following: * * Inside Problem 2 * The square number, grains added, and accumulated grains of rice are: * 1. 1 1 * 2. 2 3 * 3. 4 7 * 4. 8 15 * 5. 16 31 * 6. 32 63 * 7. 64 127 * 8. 128 255 * 9. 256 511 * 10. 512 1023 * 11. 1024 2047 * 12. 2048 4095 * 13. 4096 8191 * 14. 8192 16383 * 15. 16384 32767 * 16. 32768 65535 * 17. 65536 131071 * 18. 131072 262143 * 19. 262144 524287 * 20. 524288 1048575 * 21. 1048576 2097151 * 22. 1048576 3145727 * 23. 1048576 4194303 * 24. 1048576 5242879 * 25. 2097152 7340031 * ... * */ private void problem2() { System.out.println("Inside Problem 2"); // Write your solution to problem 2 starting here... // I recommend that you first turn in your completed solution // to problem 1, in case you don't finish this portion or it // causes your program not to compile. // // HINT: Large values don't fit into a variable of type: int // For large numbers, instead use type: long }// end method problem2() }// end of class Midterm1InLab