For this year's final project, you are to design a version of the game Yahtzee. You will be working with random numbers and arrays in your program to write your game. Attached below is a word document with the rules for how my game is played and the scoring rules for the game. My version is different from the standard game, which means the coding will be different from the many versions that are already out on the web. This will be a challenging program, so you will be given four weeks in which to complete it. The due date is 6/23/20. As far as pacing your project, my recommendation would be that you have the dice array working by early next week. Have the scoring array working by June 10th. Have the scoring for a single game working by June 17th, and finish putting all the pieces together by June 23. As always, I am available for questions. I want you to think of this last project as a challenge to show what you are able to accomplish on your own and have some fun trying to figure it out like you would a puzzle. You may work with other people on this project. As long as I get individual emails from each person in a group to tell me they are part of the stated group, all will get credit. I will ask each person in a group to specify what part of the project is their responsibility. And finally, I am well aware that there are a number of different code versions of the game out there on the web. If you want to cut and paste somebody else's code, and submit it as your own, you will receive 0 credit for the assignment. Most such cases will be immediately evident to me by A) The program not playing the game the way I asked you to write the code B) The usage of various programming techniques that are not discussed until a student is in my AP programming class. Should there be any doubt, you will be asked to explain, in detail, what various sections of your code is doing in your program.
Attached are the answers and code for the assignment that was due on 4/27. See the attached files for working source code. (I have also included again a copy of the assignment from 4/27 so you don't have to go back to find it in a prior post.) Most people had the right answers. The most common common mistakes were when people mixed up the value of the index itself of the array with the value at the POSITION of the index. So if the index is 3, arrayName[3] will equal whatever VALUE is stored at INDEX position 3 which is the FOURTH element in the array since the index starts at position 0. In the answer keys attached below, included is the code solution for each problem as well as the answer you should have gotten. I have broken down the code for the individual questions with detailed comments explaining why the lines of code are there. The notes are fairly detailed and I think it should enable you to fix the errors in your program. If you still can not fix your program, email me again with the part on which you are having difficulty. Answers for questions 1 - 5 Question 1. 20 Question 2. 20 Question 3. 22 Question 4. x= 23, y=25 Question 5. x= 21, y=11 Next assignment - Due Friday 5/22/20. 3 parts. 1) If your have not already done so, use the comments and notes in the source code in the attached files below to get your program to work correctly. 2) For problems 6 - 10, change the integer array a from int[a] = new int[10]; to int[a] = {-3,6,9,10,17,19,20,28, 34, 45, 50}; Note this new array a has 11 elements, so if you hard coded your program to loop from index position 0 to 9 (10) elements or in some way wrote your code to assume the array had exactly 10 elements, your code will not work correctly. Review my example to see how and why it will work no matter what the size of array a is. Hint a.length Get the program to work correctly for the new size. 3). Use a random number generator to: a) Create an array a of a random size between 25 and 50 elements (inclusive). b) Use the random number generator to now fill in the array with random integers in the range from -100 to 100 (inclusive). (So if in step 1 you created an array a that has a size of 36, now generate 36 random numbers in the range from -100 to +100, one at a time, (use a loop) and add them into the array a you created in step a). c) Run steps 6 through 10 from the previous assignment on the new array you created. (Add 1 to each element in the array, count the number of negative elements, print the elements in reverse, etc.) d) Use a println statement to make sure your program runs correctly.
Attached below is an assignment on arrays with 8 questions. Please submit your answers by Friday - 5/8/20. You need only copy and paste your answers in an email to me. Reminder - when you declare an array of some object type, it does not have to be only a primitive data type like int or double. Example: I have an array of object types called whozeewhatsits. I have no idea what they are or what they do but I can still create an array of them. So if I want a 100 element array of whozeewhatsits named as myArray, the code would be whozeewhatsits[ ] myArray = new whozeewhatsits[100]; Good luck and stay healthy!
For this next assignment, we are going to combine working with Strings and arrays.
The user is to enter in a line of text (A sentence or two with a minimum of 10 words total). Your program will read the line of text and calculate the number of words in the sentence(s). It will then create an array that is the length of the number of words and store each word in the array. The program will then read the array from the last element to the first and print out the words in reverse order. Example: You enter the text. I am fine. How are you today? You read the input and count 7 words. (For this first program, any punctuation is counted as part of the word to which it is adjacent.) You create an array of length 7. So in position 0, the word "I" is stored, in position 2 "am" is stored. in position 3 "fine." is stored (any punctuation is stored with the word it is next to) etc. in position 6 (the 7th element), "today?" is stored. You will then USE THE ARRAY to print the sentence in reverse. So the actual output should look exactly as you see below: today? you are How fine. am I (Sounds like Yoda if you fixed the punctuation and caps!) (Today you are how? Fine am I!) This assignment will be due on Monday, May 4. We will also be having a short assessment next week. Stay healthy! Arrays are used in java programming to hold multiple variables of the same data type. Instead of having to declare hundreds of variables, you can declare one array and store all the values in that one variable name. It is a very handy and powerful programming tool. An array can be thought of as a numbered LIST of items. You have one list with many items on the list. To declare an array the syntax is as follows: variable type to be stored [ ] arrayName; The square brackets indicate that the variable is an array Examples: int[ ] grades; an array of integers called grades String [ ] names; an array of Strings called names Note that all variables stored in an array must be of the same data type. Just declaring the array does not actually instantiate or create it. You must use the new keyword to actually allocate space for the array in memory. Once an array is created, it can not change size. To create an array that can hold 50 integers in a variable called numbers: int [ ] numbers = new int[50]; To create an array called words to hold 100 String variables: String [ ] words = new String[100]; Note that when arrays of objects such as Strings are instantiated as above, if you try to access an element before something is stored there, you will get a null pointer run-time error. If you create an array of integers like numbers above, an initial value of zero is stored in each array position when it is first instantiated. So after the code int [ ] numbers = new int[50]; is executed, a reference to any index position in numbers would return the value of zero until something else was copied into it by later instructions. Now variables can be stored in the above arrays. To store or read an element in the array, you reference the position in the array. The first position in the array is always at index 0. If an array can store 100 elements, the index positions start at 0 and end at 99 (100 elements). If an array has 20 elements in it, the first element is at position 0 and the last is 19 (Note the last position is always one less than the length of the array.) To put elements into the String array words as written above, the code could look like: 1) words[0] = "Hello"; places the String "Hello" in the first position (index 0) of the array 2) String y = "world"; creates a String and stores it in the variable y. words[1] = y; places the String stored in variable y ("world") as the second element (index position 1) in the array. Accessing works the same way. the code String answer = words[52]; would take the String stored in index position 52 (the 53 element) in the array words and store it in the variable answer. The point in using arrays is to be able to store and access large amounts of information without having to create many variables. There is also a second way to create an array using an initializer list. These are used when you know the variables you want to store in the array. Let's say you want to create an array of integers called scores with the variables 2,10,20,35,46 and 97. An initializer list explicitly lists the elements of an array as it is instantiated. EXAMPLE: int [ ] scores = {2, 10, 20, 35, 46, 97}; Note that the keyword new is not used when using an initializer list. On the right side, instead of listing the number of elements to be stored in the array, the actual elements being stored is listed. Example 2 - An initializer list storing the days of the work week. String [ ] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; To get the size of an array, every array object has a field associated with it called length. So using the examples above, the value returned from scores.length = 6 (index positions would be 0 to 5) week.length = 5 (index positions would be 0 t0 4) int num = words.length; would store 100 in num since 100 is the number of elements in the String array words. Be mindful that length returns the actual size of an array, but since the index starts at 0, the position of the last element is ALWAYS ONE LESS THAN THE size of the array. Assignment: For the code below: Do questions 1 to 5 and send your answers to me via email by Wednesday, 4/22/20. The code for problems 6-10 is due by next Monday, 4/27/20. Put all the answers for problems 6 to 10 in the same program. You don't have to write a separate class for each loop. PALINDROMES!!!
A palindrome is a sequence of text that reads the same backwards and forwards. The word cat reversed is tac, so cat is not a palindrome. The word toot reversed is toot, so toot is a palindrome. The word racecars reversed is sracecar. So it is not a palindrome. However, racecar reversed is again racecar, so racecar is a palindrome. Ignoring punctuation and capitalizing, reversing the letters of Able was I ere I saw Elba., you get able was I ere I saw Elba This sentence is a palindrome. Ignoring punctuation and spacing, the sentence, Madam I'm Adam is also a palindrome. For your next program, the user is to enter a string of text and you are to determine if the sequence entered is a Palindrome. You must figure out how to correctly reverse the letters of a string and then compare the two string values. Remember, to compare the text of two strings you must use the .equals( ) method. You can not use = = to compare string objects. To get credit for this program, YOU MAY NOT USE the reverse( ) method in the StringBuilder class. You will have to do it by actually reversing the letters in the text to create a second string to compare to the first. For this first version, do not enter punctuation, and you do not have to consider spacing or capitalization. This program will be due on Friday, 4/16/20. You are to do a more advanced program using the String class. This will be a bit more complicated. This program will be due on Thursday, 4/9/20. I have received programs from most of my students. Overall, I have been very pleased with the work I have been seeing. You guys are a doing great job in a less than ideal situation. For those from whom I have not received anything at all by Monday, I will be contacting home to check on your situation. The next program will be assigned on Thursday. Stay healthy!
Please note: For those using the scanner to get input from the keyboard, you have to use the method:
scannerName.nextLine( ); to input multiple words from the keyboard. scannerName.next( ); will only read in a single word string. This extra method was added to the previous code sample in the prior post. If you are having a problem getting your code to compile correctly with online gdb, you should use the Scanner class instead of the JOptionPane class. Although a little different, it is easier than the dialog boxes and you don't have to parse the Strings to ints or doubles. Below is a simple program that shows you how to use the Scanner class to input data to your program. I will also include a word doc file attachment below the scribd doc. If you are still having problems when using the scanner class, email me.
Welcome back everybody.
The last two weeks were officially our "snow days" and adjusted "Spring break" days due to the corona virus with no work being mandatory from the students. Going forward, your assignments will be updated on this site on a regular basis. If you haven't already done so, please read the last several posts for important information. I have also sent an email to every parent/guardian email listed on Eschool. If no email was received from my school email at [email protected], please have them email me with a working email address and I will make sure I update them to my distribution lists. (It is possible that an email was received, but it was automatically forwarded to the spam folder.) For this next week, we will do some programs reviewing the basics and making sure that you have a compiler and development environment in which you can write, compile and execute a java program if you haven't already done so. (See the prior posts for information on this.) Your first assignment that is due Wednesday 4/1/20 is as follows: You are to write a program with methods called findSquare( ) and findCube( ), in addition to the main method. The program will prompt the user for a number in the main method. The main will parse the input as a decimal and then pass the values to the two methods. findSquare( ) - will find the square of the number entered and print the value out in the method itself. findCube( ) - will find the cube of the number entered and return the value to the main where the answer will be printed out. The main should then ask if you want to find the values for another number, quitting when the user indicates that they are done. Assignment 2 is due Monday, 4/6/20. The program will request the user to enter some text (a sentence or two). Using the methods from the String class, the program will determine how many words are in the text. Hint - each word is always separated by a space. As a reminder, the API documentation for the version we use can be found at: https://docs.oracle.com/javase/8/docs/api/ When you are done with your source code, just copy and paste the text of it in the body of an email and send it to me at [email protected]. Don't email an actual file. If you have any questions, email me. There will be a quiz or test some time within the next two weeks. Stay Healthy!!!! |
Archives
May 2020
Categories |