Lab 6 - Lab 6: Introduction to Arrays Exercise 1:...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lab 6: Introduction to Arrays Exercise 1: HelloEveryone a. Open the class contained in the file, then compile and run it. // public class HelloEveryone { public static void main( String args ) { String student = { "Gerald", "Houston", "Duc", "Dustin", "Eli", "Fred", "Gavin", "Jennifer", "Joe", "John", "Kevin", "Lee", "Sahil", "Patrick", "Rishi", "Utkarsh", "Amar", "Brandon", "David", "Sibte"}; for (String name : student) ); System.out.printf("Hello %s!\n", name } // end main } // end class b. The class Arrays in the package java.util has a void method called sort that will act on an array and sort it, in this case in alphabetical order. Import the class arrays and then apply the method Arrays.sort() to sort our students in alphabetical order. Show your class list with students sorted from A-Z. Since the method is static, do not attempt to invoke it from the array as an object. Follow the syntax of other static methods you have seen such as Math.cos() Exercise 2: SearchEveryone and SearchWithWildCard a. Now we want to search the class list for all the students whose name begins with a certain letter. For example Jennifer, Joe and John all begin with J. Open the class This only finds students whose name begins with the letter 'J'. Enable the user to enter a string at the keyboard. First import the Scanner class and then create a keyboard object. Prompt the user with the following string. "Enter a capital letter (A-Z) to find all the students whose name starts with that letter." Read in their letter (String) using the next() method and store it in the String userString. Convert their input toUpperCase then get the first letter using the code: char firstLetter = userString.charAt(0); // in case user enters more than one letter. b. You can use the following enhanced for loop to printout all the names that start with the char firstLetter. Use the for loop to step through the array as usual, but now only print out the array element if it starts with the specified letter. for (String name : student) if (firstLetter == name.charAt(0) ) System.out.println("\t " + name); Before you print out the search results, include a message as shown in the typical results below. Notice the user did not follow instructions but typed sKS4. They did not enter the s as a capital letter and in fact typed in more than one character. But the program should still work. Demonstrate your program working with the letter 'S' or 'D'. c. Use the String class method startsWith() to printout all the students whose name starts with "Du", the same as Drexel's initials. The method is case sensitive and has the syntax: name.startsWith("Du") d. Use the String class method endsWith() to printout all the students whose name ends with: "hi" or "avid". Note: You could allow the user to use a wildcard like *. Then user input Du* triggers the startsWith("Du") method, while *hi triggers the endsWith("hi") method. Compile and run my file to explore this option. At least try both searches du* and *hi. Exercise 3: Perpetual Calendar. The program enables the user to enter any year (past or future) at the keyboard but contains two common errors which you will now fix. Begin by opening this file. a. Woops! Our programmer forgot about leap years in which February has 29 days instead of only 28. Add an if statement which in leap years, gives to February one day more. b. Woops! Our programmer forgot about verb tenses. Consider the following output for the year 1000. In the year 1000 each month has the following number of days. The verbTense should be " had " since this is in the past. For a year in the future, verbTense should be " will have ". For the present year of 2007, verbTense should be " has ". So the output really requires three verb tenses. Use an if - else if - else structure to assign the correct value to the String verbTense and demonstrate it working for a choice of year in each of the three cases. Exercise 4: Anagrams a. Open the program contained in the file and adjust it as follows. Using the Scanner class, create a scanner object named keyboard then prompt the user to enter two phrases one after the other. To read in a phrase which may contain spaces, use the nextLine() method of the Scanner class. Do not use the next() method which will only read up to the first white space. b. Demonstrate your modified program using the two phrases: String1: The centenarians String2: I can hear ten "tens" Hint: At the keyboard, use the escape character \" for each " in the second string. c. Demonstrate your program on any of the following phrase pairs that are close but not quite anagrams. Examine the array of letter tallies for each phrase and use this to find the correct version for the second phrase that would give a valid anagram pair. Then run your program with the corrected phrase. String1: String1: String1: Desperation The country side The Morse Code String2: String2: String2: A Rope Ends This No City Dust There Here Come Spots Exercise 5: Explore Unicode Characters Adjust the range of the for loop to answer the following questions. Notice the use of type-casting. // Convert int to Char public class intToChar { public static void main(String args) { for(int k = 0; k<= 140; k++) System.out.println("The integer " +k+ " is equivalent to: [" + (char) k +"]"); } } a. What is the integer code for the tab character? b. What is the integer code for the 'new line' character? c. What is the integer code for the 'system beep' character? d. What is the integer code for the 'A' character? e. What is the integer code for the 'zero' character? ........ ........ ........ ........ ........ (If not deactivated.) ...
View Full Document

This note was uploaded on 01/17/2012 for the course ECE 203 taught by Professor Robincarr during the Fall '07 term at Drexel.

Ask a homework question - tutors are online