Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Project 02 Materials / C11_YourID_Exercise11

.docx
Скачиваний:
20
Добавлен:
14.10.2016
Размер:
1.26 Mб
Скачать

exercise 11: string and scanner classes

Application Development

Name: <your name goes here>

Purpose: Working with strings is critical for processing input from humans. The Java String and Scanner classes provides us with many tools that can simplify the process of reading in data and translating information that humans produce into things that computers can use. In order for these methods to do their job, the programmer must provide information to the method using parameters. This exercise focuses on the use of these methods, including using the parameters properly.

Perform each of the following activities. If you have questions, issues, or doubts, please ask for help and do not just guess!

    1. What will the following code display ...

System.out.println("Enter the values separated by blanks");

Scanner keyboard = new Scanner(System.in);

int result = 0;

int cntr = 0;

result += keyboard.nextInt();

cntr++;

System.out.println(cntr + ": " + result);

result += keyboard.nextInt();

cntr++;

System.out.println(cntr + ": " + result);

result += keyboard.nextInt();

cntr++;

System.out.println(cntr + ": " + result);

... if this is what the user enters:

12 8 7 2 1 9 21 5

Show in the space below what the computer would display after the user enters the green text above. (Do not enter this code into the computer and run it. Show that you understand what this code will do by completing this exercise by hand)

    1. What will the following code display ...

System.out.println("Enter the values on separate lines.");

Scanner keyboard = new Scanner(System.in);

int result = 0;

int cntr = 0;

String line = keyboard.nextLine();

Scanner lineScnr = new Scanner(line);

result += lineScnr.nextInt();

cntr++;

System.out.println(cntr + ": " + result);

line = keyboard.nextLine();

lineScnr = new Scanner(line);

result += lineScnr.nextInt();

cntr++;

System.out.println(cntr + ": " + result);

line = keyboard.nextLine();

lineScnr = new Scanner(line);

result += lineScnr.nextInt();

cntr++;

System.out.println(cntr + ": " + result);

... if this is what the user enters:

12 35 19

3 10 5

6 8 2

Enter in the space below what the computer would output given the user enters the green text above.

  1. Answer the following questions about the use of the String Class, assuming the String reference variable named strng contains the following data:

00 01 12 23 34 45

012345678901234567890123456789012345678901234567890

this is the data you are supposed to use

  1. What would the following statement display?

System.out.println(strng.indexOf("you"));

  1. What would the following statement display?

System.out.println(strng.indexOf(" ", 18));

  1. What would the following statement display?

System.out.println("<" + strng.substring(30) + ">");

  1. What would the following statement display?

System.out.println("<" + strng.substring(15, 30) + ">");

  1. What would the following statement display?

System.out.println("<" + strng.charAt(6) + ">");

  1. Show in the space provided below what the following code will produce:

String strng = "this is the data you are supposed to use";

Scanner strngScnr = new Scanner(strng);

int cntr = 0;

int sum = 0;

while (strngScnr.hasNext()) {

String token = strngScnr.next();

sum += token.length();

cntr++;

System.out.println(cntr + ": " + sum);

}

  1. Congratulations! Save your work and upload this exercise to the LMS following the instructions given in Exercises 1 and 2. (Please be sure you have changed the name at the top of this document and properly renamed it to start with your StudentID before you began editing it, right?)

©iCarnegie - Distribution or copying without permission is prohibited.

Соседние файлы в папке Project 02 Materials