/* * 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 * ... * */ /* * Midterm1InLabSolution.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 Midterm1InLabSolution // 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 Midterm1InLabSolution myMidterm1 = new Midterm1InLabSolution(); // 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... System.out.print("Enter 7 integers: "); int largest = 0; int smallest = 101; int nextNumber = 0; // loop through 7 times. Each time read in a number // and check to see if it is the largest or smallest. // If it is, store it. for( int i=0; i<7; i++) { // read in the next number nextNumber = keyboard.nextInt(); // store it if it is the largest so far if( nextNumber > largest) { largest = nextNumber; } // store it if it is the smallest so far if( nextNumber < smallest) { smallest = nextNumber; } }//end for( int i... // Display the answers System.out.println("The largest and smallest are: " + largest + " " + smallest); }// 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 * ... * * Running this program gives: 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 26. 2097152 9437183 27. 2097152 11534335 28. 2097152 13631487 29. 4194304 17825791 30. 4194304 22020095 31. 4194304 26214399 32. 4194304 30408703 33. 8388608 38797311 34. 8388608 47185919 35. 8388608 55574527 36. 8388608 63963135 37. 16777216 80740351 38. 16777216 97517567 39. 16777216 114294783 40. 16777216 131071999 41. 33554432 164626431 42. 33554432 198180863 43. 33554432 231735295 44. 33554432 265289727 45. 67108864 332398591 46. 67108864 399507455 47. 67108864 466616319 48. 67108864 533725183 49. 134217728 667942911 50. 134217728 802160639 51. 134217728 936378367 52. 134217728 1070596095 53. 268435456 1339031551 54. 268435456 1607467007 55. 268435456 1875902463 56. 268435456 2144337919 57. 536870912 2681208831 58. 536870912 3218079743 59. 536870912 3754950655 60. 536870912 4291821567 61. 1073741824 5365563391 62. 1073741824 6439305215 63. 1073741824 7513047039 64. 1073741824 8586788863 End of Midterm1 problems. */ 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 // declare and initialize variables used to store values long dailyRiceAmount = 1; long totalRiceSoFar = 1; // display title line for table System.out.println("The square number, grains added, and " + "accumulated grains of rice are: "); // loop for the 64 days for( int square=1; square<=64; square++) { // display values for this day System.out.println( square + ". " + dailyRiceAmount + " " + totalRiceSoFar); // increment values for board squares 1..20, or // if the square is a multiple of 4 if( (square <= 20) || ((square % 4) == 0) ) { dailyRiceAmount = dailyRiceAmount * 2; } // Accumulate total rice so far totalRiceSoFar = totalRiceSoFar + dailyRiceAmount; }//end for( int i... }// end method problem2() }// end of class Midterm1InLab 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