SSEA August 2016 Cynthia Lee CS106A Homework Assignment 2: Java Console and Graphics Based on past assignments created by Marty Stepp, Mehran Sahami, Keith Schwarz, Eric Roberts, Stuart Reges, and others. DUE: MONDAY, AUGUST 22, 1:30pm (Problem 3 do not start until after lecture Friday; it is due Tuesday 8/23 11:59pm) PURPOPSE: The purpose of this assignment is to acquaint you with writing Java console programs, practicing essential programming skills of well-designed decomposition into helper methods, good commenting of file and each method, and creative expression through code. ASSIGNMENT: This assignment has 3 parts. Read each part carefully, then read the general instructions. PROBLEM 1: HAILSTONE SEQUENCE Write an interactive ConsoleProgram named Hailstone that displays information about a "Hailstone sequence". Douglas Hofstadter s Pulitzer-prize-winning book Gödel, Escher, Bach mentions a cumulative algorithm for generating a series of integers called a Hailstone sequence, which can be described as follows: Pick a positive integer and call it n. Repeat the following process until n is 1: o If n is even, divide it by two. o Otherwise (if n is odd), multiply it by 3 and add 1. For example, if you start with n of 7, the sequence is 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1. As you can see, the number goes up and down, but eventually ends at 1. 1 It reminiscent of the formation of hailstones, which are carried upward by wind over and over again before they finally descend to the ground. Your program should prompt the user to type a value for n as an integer (you may assume that the user will type a positive integer) and then prints the Hailstone sequence for that n. Your Hailstone sequence should print each number on the same line with commas between the values. You should also print the number of steps it took for the sequence to reach 1. Your program should exactly duplicate the output of the following sample run, plus be able to run properly with other values. (User input as read from a call to readint is shown in bold with an underline to help distinguish it from normal output text. But the text doesn't actually show up as underlined on the screen when you run the program.) 1 The Collatz conjecture claims that this sequence always eventually reaches 1; no one has yet proven or disproven it.
If the user enters 1 as the initial value for n, the program's output should be: A Hailstone sequence can certainly get very long. For example, how many steps does it take when n is 27? PROBLEM 2: EXAM SCORES Write an interactive ConsoleProgram named ExamScores that displays information about exam scores. The program should repeatedly prompt the user to enter exam scores until a particular "sentinel" value is entered. By default this sentinel value is -1. You may assume that the user will type positive integers other than the sentinel itself. Once the sentinel value is entered, the program should display the maximum and minimum exam score typed, as well as the average score and the number of scores that failed the exam. "Failing" the exam in this context is receiving a score of 59 or lower. Your program should exactly duplicate the output of the following sample run (user input underlined), plus be able to run properly with other values: This program should be written to use a class constant to represent the sentinel value of -1, so that by changing only that constant's value and recompiling / running the program, it will now use the new sentinel value throughout the code and output. For example, if the constant's value is changed to -42, the program's output would look like:
If only one score is entered, that score is the maximum, minimum, and average. For example: If no scores are entered, the program should instead print the following message saying that no scores were entered: PROBLEM 3: ASCII ART ROCKET In the old days, when computers didn t always have graphics capabilities but only hand text consoles for interaction, programmers and users became quite clever in how to draw graphics just using letters, numbers, and punctuation. This was called ASCII art (ASCII being the encoding system for text in early computers). You still see ASCII art sometimes today in forum postings, text messages, and other settings. ڰ ڿ- ڰڿஇ and \_( ツ )_/ are examples of ASCII art. Write a ConsoleProgram named Rocket that displays an ASCII art rocket ship, as shown in the figure at right. Unlike the last two parts of this assignment, this problem is not an interactive program; it does not read any input from the user. You must exactly reproduce the format of the output at right, including identical characters and spacing. One way to write a Java program to draw this figure would be to write a single println statement that prints each line of the figure. However, this solution would not receive full credit. A major part of this assignment is showing that you understand nested for loops, methods and class constants. In lines that have repeated patterns of characters that vary in
number from line to line, represent lines and character patterns using nested for loops. (See lecture slides.) Another significant component of this assignment is the task of generalizing the program using a single class constant that can be changed to adjust the size of the figure. You should create one (and only one) class constant named SIZE to represent the size of the pieces of the figure. Use 5 as the value of your SIZE constant. Your figure must be based on that exact value to receive full credit. On any given execution your program will produce just one version of the figure. However, you should refer to the class constant throughout your code, so that by simply changing your constant's value and recompiling, your program would produce a figure of a different size. Your program should scale for any constant value of 2 or greater. The course web site will contain files that show you the expected output if your size constant is changed to various other values. Other guidelines for Rocket: Use methods to represent each part of the rocket, structuring your solution in such a way that the methods match the structure of the output itself. Avoid significant redundancy; use methods so that no substantial groups of identical statements appear in your code. In this problem, no println statements should appear in your run method. You do not need to use methods to capture redundancy in partial lines, such as if a single line has the substring... printed twice. Development strategy: We suggest that you not worry about the constant at first. Write an initial program without a constant that produces the default size-5 output. Make sure you use nested for loops for sequences of repeated characters. After your figure looks correct at the default size, begin modifying the code to use the constant. GENERAL INSTRUCTIONS: You are allowed to work in pairs and submit a joint assignment. Although we are not assigning any grades in this course, you are now officially joining the Stanford community, and we expect that you will take seriously your personal integrity responsibilities under Stanford sڿhonorڿcode.ڿitڿreadsڿasڿfollows: Stanford Honor Code 1. The Honor Code is an undertaking of the students, individually and collectively: a. that they will not give or receive aid in examinations; that they will not give or receive unpermitted aid in class work, in the preparation of reports, or in any other work that is to be used by the instructor as the basis of grading; b. that they will do their share and take an active part in seeing to it that others as well as themselves uphold the spirit and letter of the Honor Code. 2. The faculty on its part manifests its confidence in the honor of its students by refraining from proctoring examinations and from taking unusual and unreasonable precautions to
prevent the forms of dishonesty mentioned above. The faculty will also avoid, as far as practicable, academic procedures that create temptations to violate the Honor Code. 3. While the faculty alone has the right and obligation to set academic requirements, the students and faculty will work together to establish optimal conditions for honorable academic work. Your Honor Code obligations and allowances in this course include the following: If you need help, please seek out our available resources to help you. You are of course permitted to collaborate within your pairs for the assignment. This is an exception to the usual default assumption that everyone should work completely independently. Assisting fellow students those who are not your official partners for the assignment is also encouraged, when that assistance consists of oral conceptual discussions or debugging help. This is an exception to the usual default assumption that everyone should work completely independently. Do not use others ڿsolutions, or overly participate in the solutions of others by, e.g., providing your solutionsڿasڿaڿreferenceڿorڿtakingڿoverڿtheڿkeyboardڿforڿthemڿtoڿ help ڿthem. Although we are not assigning grades, this would defeat the purpose of our summer program. It is very important, for this assignment and for all your Stanford assignments, that you NOT place your solutions on any public website or forum (e.g., GitHub). Some students innocently think to do this as an augmentation to their resumes, but the Stanford CS Department considers this to be a serious Honor Code violation and has and will pursue offenders regardless of intent. If you want to do something like this, please ensure that the repository is private and only provide access to recruiters individually on request.