pair programming assignment

Strings

Many programs manipulate not just numbers but text. Java stores text in string variables. They are declared and assigned in a manner similar to a numeric variable, and the sequence may have any length. (A string may even have length zero. Such a string is called an empty string, and it is denoted as "".) For example,

   String name = "Joan Wilson";

assigns the 11 characters enclosed in quotes to the variable name.

   String temp = name; 

assigns the content of variable name to variable temp

   System.out.println(name); 

displays the contents of variable name on the standard output stream.

Learning Exercises with Strings

  1. Concatenation

    Given:

       String firstName = "Jane";
       String lastName = "Doe";
       String name;
    

    What value will name contain after

          name = firstName + lastName;
    

    How could the preceding be revised to give a better result?

  2. substrings

    The primary difference between strings and numeric data types is that any portion of the characters in a string variable is itself a string variable. Such a portion is called a substring.

    What will be the resulting substring in the following examples? If invalid, give a corrected version of the call to substring.

    String name = "John Smith"
    String firstName = name.substring(0, 4);
    String name = "John Smith"
    String lastName = name.substring(5, 6);

  3. Extracting substrings

    Consider the following code fragment

          String name = "George Bush";
    
          String x = name.substring(6, 7);
          String y = x + x + x + x + ".";
    

    What value has the String y? What value has the expression y.length()

  4. Manipulating Strings

    Using substring and concatenation, write a program containing a sequence of commands that will extract characters from name = "albert einstein" to make newName = "bertie"

  5. Who's There

    Write a program that asks the user for a name and then prints out Hello 'name'. E.g. a sample run might look like

    $ java Who
    Enter your name: James   
    Hello James
    $ 

    The command Console.readString() will read in a String (such as a name)

  6. Usernames

    Write a program which when given a first name, second name and a number, will print out a dcu username. E.g. given mickey mouse 3, the program will print mmouse3 Note: to make an username, take

    1. the first letter of your first name
    2. add your surname
    3. add the number

    Note 2: Actually it's slightly different if the surname is long, but you can ignore that problem for the moment.

  7. Email Addresses

    Write a program which when given a first name, second name and a number, will print out a dcu email address. E.g. given mickey mouse 3, the program will print mickey.mouse3@mail.dcu.ie Note: to make an email address, take

    1. the first name
    2. concatenate it with your surname
    3. separate the two with a dot
    4. add the number
    5. add the @mail.dcu.ie part
  8. Optional exercise

    Write a program that reads a number greater than or equal to 1000, and less than a million. Print the number with a comma separating the thousands. Here is a sample interaction with the user:

    $ java Comma
    Please enter a number >= 1000: 23456   
    23,456
    $ 

    Hint: think of the end of the number. Here's another hint

  9. Optional exercise

    Write a program that reads a number from the user. The number contains a correctly positioned comma. The program should print the number with the comma removed, e.g.

    $ java DeCommify
    Please enter a number >= 1000: 12,345   
    12345
    $ 

    Note that there is no comma in the output.

    Here's a hint.

Hint eile: read the question carefully: the number will be correctly formatted and will contain a comma.