Practice Quiz 8 Answer Key


Telephone Numbers

  • Write a program that reads in a series of numbers, as strings, from a file and converts them to valid telephone numbers.
  • The numbers are contained in file is called numbers.txt.
  • The numbers must read into the file using a loop, and stored as an array of Strings
  • Note that you can assume the length of the array to be 10.
  • As it reads in each number, it should convert each number to a valid telephone number in the format XXX.XXX.XXXX by calling a method named formatPhone.
  • Here is the information about this method:
    • The method is named formatPhone
    • It take in a String array variable
    • It alters the String array so that each String element is a properly formatted phone number, in the format XXX.XXX.XXXX
    • Hint, use a for loop to iterate through each String in the array
    • This method must call the substring method
    • It returns nothing.
  • The program must also contain a second method which writes out the contents of the array in a file named phones.txt
  • The second method:
    • The method is named print
    • It takes in an array of Strings
    • It opens up a new PrintWriter and connects it to a file called phones.txt
    • It prints out the array to the file using a for loop
    • It closes the PrintWriter
    • It returns nothing
  • Copy and paste the starter code below into a new file called Phones.java


/*
* @author
* Section info
*/
public class Phones {
    
    /**
    * Write your javadoc comment for formatPhone here
    */
public static void formatPhone(String[] numbers){
    return;
}

/**
* Write your javadoc comment for print here
*/
public static void print(String[] numbers){
    return;
}


public static void main(String[] args) {
    //write the program logic here


}


  • Copy and paste the below data into a file called numbers.txt:

5556667777
3334445555
1118884944
3332225677
1234445555
8889990000
1116664344
2224563333
1112229999
4568899399

  • Below is the output that your program should give inside of the phones.txt file
555.666.7777
333.444.5555
111.888.4944
333.222.5677
123.444.5555
888.999.0000
111.666.4344
222.456.3333
111.222.9999
456.889.9399
Once you have the correct output inside of the phones.txt file, your program is complete.


Customers
  • Write a program that reads in a series of names, along with their gender, from a file.
  • The file is called names.txt.
  • The program uses a loop to read in each name from the file and store each person's title (Mr. or Ms.) + name in an array called customers.
  • Note: you can assume that the length of the array is know ahead of time to be 8.
  • The program should then call two methods.
  • The first method:
    • The method is named capitalizeArray
    • It take in a String array
    • It alters the values in the String array to capitalize all of the letters
    • This method must use a for loop. It cannot call any outside methods that we have not discussed in this class.
    • It returns nothing.
  • The second method:
    • The method is named print
    • It takes in an array of Strings
    • It opens up a new PrintWriter and connects it to a file called customers.txt
    • It prints out the array to the file using a for loop
    • It closes the PrintWriter
    • It returns nothing
  • Once you have filled the customers array with titles and names, call the capitalizeArray method to capitalize all the names in the array.
  • Next, call the print method to display each of the capitalized names in the customers.txt file
  • Copy and paste the starter code below into a new file called Customers.java
/**
 * @author
 *
 */
import java.util.Scanner;
public class Customers
{
    /**
     * Write the javadoc comment for capitalizeArray here
     */
    public static void capitalizeArray(String[] customers)
    {
        return;
    }
    
    /**
     * Write the javadoc comment for printArray here
     */
    public static void printArray(String[] customers)
    {
        return;
    }
    
    public static void main(String[] args)
    {
        //declare your customers array here
        //note, you can assume your array will be of length 8
        String name, gender;
        
        
        //Use a while loop to read in the data from the file
        
        
        //call your two methods
    }
}

  • Copy and paste the below data into a file called names.txt:
Barbara Stanton
F
Timoteo Martinez
M
Grace Gu
F
Xing Xiao
M
Dung Kim
F
Tanya White
F
Alan Ngo
M
Abir Fadel
M
  • Below is the output that your program should give in a file called customers.txt:
MS. BARBARA STANTON
MR. TIMOTEO MARTINEZ
MS. GRACE GU
MR. XING XIAO
MS. DUNG KIM
MS. TANYA WHITE
MR. ALAN NGO
MR. ABIR FADEL