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. |
Archives
May 2020
Categories |