Ice01 Programming with String and String Arrays

Complete, Zip, and Upload via Student Tracker (Extra_Credit_01 in Select menu) DUE Wednesday, December 13th, BY MIDNIGHT


PARTS 1
through 5 are worth up to 5 points each of extra credit
PART 6 is optional, but is worth up to an additional 15 points of extra credit
PART 7 is optional, but is worth up to an additional 30 points of extra credit
TOTAL COMBINED POINTS EXTRA CREDIT AVAILABLE: 70 points

ICE PART 1: Getting Text from the User (5 Points)

Download the Nonrobot Template, and save it into a file named something like UsingText.java
Obviously, you will need to adjust the contents of the file so that it compiles  

 

Using the I/O routines that we've used previously, you should start by asking the user to type in his-or-her name, then print it back to them (e.g., "Hello, Zeke!").

 

Next, you should print out a friendly message if their name is "Alphonso" (try checking for both an exact match, and a case-insensitive match. In other words, "Alphonso", "alphonso", and "ALPHONSO" should all work).

Example: "Alphonso is an interesting name! Did you know that it is Italian in origin, and roughly translated means "I'm Ready to Rumble" ?"

 

ICE Part 2: More String Methods (5 Points)

Using documentation provided on-line ("Google it!") create a Java program that uses five different String methods (you might want to look in the Java Library under the java.lang package)

 

Start with an initial String "Rex Angus Cornelius Winkus".  Since this is such a long reference, declare a reference variable* (named userText) to hold the value of this String reference.

 

Example:

 

String userText = "Rex Angus Cornelius Winkus";

 

Send ten messages to userText  using five different String methods. Output each string to the screen, for example, using the toUpperCase() method:

 

System.out.println(userText.toUpperCase());
 

You should also experiment with the concatenation operator: +.  Try doing some stuff like:

 

System.out.println("The gentleman's name is: " + userText);
System.out.println("The gentleman's name is: " + userText.toUpperCase());

* REFERENCE VARIABLE Information:

 

ICE Part 3: Loading an Array with Strings and Printing Them (5 Points)

Create a simple Java program from scratch that will create an array of 5 Strings (the array should be named userNames). 

 

You should load the array with 5 different names (pick any that you want – if you’re at a loss for names, try “Mal”, “Inara”, “Derrial”, “River”, and “Kaylee"). 

 

Print all the names out.  Feel free to put everything into main, so that you can focus on using strings with arrays, and won't have to worry about methods, etc.

 

Of course, if you prefer using methods, then by all means do so! Smile

 

ICE Part 4: Is a Name Present in the Array? (5 Points)

Save a copy of what you did in the previous part, then continue working on the original in this part.

 

After prompting the user to type their name get the user’s name then see if the provided name is present in the array.

 

HINT IMPORTANT!
Before you do that, though, take a minute to remember/research what the difference is between == and .equals() when you’re dealing with strings.* SEE EXPLANATION BELOW, LOCATED BENEATH PART 7

 

ICE Part 5: Checking Username and Password (5 Points)

Save a copy of what you did in the previous part, then continue working on the original in this part.

 

Create a second array that is ‘parallel’ to the first, named passwords.  Use this second array to store the passwords for each of the people listed in the first array. 

 

Let’s say , if “Mal” is stored in slot 0 of the userNames array.  Mal’s password should be stored in slot 0 of the passwords array (See the image below for an illustration)

 

In addition to adding the above array, you should also ask the user for his/her password.  If the user name, or the password doesn’t match, you should say “User name unknown or password doesn’t match”

 

SECURITY QUESTION: Why not tell the user if only their userName is wrong?  Why not tell the user if only their password is wrong?

Users and Passwords

ICE Part 6: Adding a New User (and Password) to the Array (15 Points)

Save a copy of what you did in the previous part, then continue working on the original in this part.

 

Instead of creating a pair of arrays that are 5 elements long, and completely occupied, create a pair of arrays that are 10 elements long, but start with only those 5 pairs of username/passwords that are listed above. 

 

Once a user typing at the keyboard has provided a valid username/password, you should ask if they would like to add a new user (but only if there are blank slots left in the arrays). 

 

If so, you should ask the person at the keyboard to type a new username, and then a new password, and add them to the arrays.

 

At the end, you might want to come up with a way (just for test purposes) to show all the users in the array, including any added new users.

 

ICE Part 7: Altering Assignment 4 Basic to Get User Input for the Histogram Data (30 Points)

The histogram data in Assignment 4 Basic is currently hardcoded in the array. Rewrite it to get the data from the user instead, populating each of the seven elements of the array with values provided by the user. The robot would then lay down the Things on each row according to the data provided, and then duplicate those numbers by printing out rows of asterisks ("stars") in the console window, just like in Assignment 4 Basic. It is okay if this program is only in one file (it doesn't have to be split into multiple files like Assignment 4 Basic). Below is a screencap showing how the input and output might look.

 

 


* THE DIFFERENCE BETWEEN "==" AND THE .EQUALS( ) FUNCTION

 

The equals( ) function is a method of Object class which should be overridden by programmer.
String class overrides it to check if two strings are equal i.e. in content and not reference.

== operator checks if the references of both the objects are the same. Consider the programs:

 

String abc = "Awesome" ;
String xyz = abc;

 

if(abc == xyz)
System.out.println("Refers to same string");

 

Here the abc and xyz, both refer to same string "Awesome". Hence the expression (abc == xyz) is true.

 

String abc = "Hello World";
String xyz = "Hello World";

 

if(abc == xyz)
   System.out.println("Refers to same string");
else
   System.out.println("Refers to different strings");

if(abc.equals(xyz))
   System.out.prinln("Contents of both strings are same");
else
   System.out.prinln("Contents of strings are different");

 

Here abc and xyz are two different strings with the same content "Hello World".
Hence the expression (abc == xyz) is false where as (abc.equals(xyz)) is true.