Lesson 8 – Input, output, and Strings

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Concept - Getting input with Scanner

Concept - Getting input with Scanner

Activity - Input

Record your participation in Piazza clicker question Lesson8 Q1

Java Factory is having a sale where everything is being sold at 2/3 of its original cost. Write a program to read price from the keyboard and then calculate and print the new price (Call the new price discountedPrice). Do not use .67. Let Java do the division to get a more acurrate answer

What do you need to do? Here is pseudocode

  create a Scanner
  prompt for a price
  get the next double from the keyboard
  calculate 2/3 of the price 
  print it

 

Here is the skeleton of a project you can use for getting input

Concept - Formatting output

 

Activity - Formatted Output

Record your participation in Piazza clicker question Lesson8 Q2

Our boss is not happy with the way output looks in the last activity. It looks messy. The boss wants us to modify the previous program to format the discountedPrice with a dollar sign and 2 decimal places. Which of the following statements will do that?

  1. System.out.printf("$%10.2f", discountedPrice);
  2. System.out.printf("%$.2f", discountedPrice);
  3. System.out.printf("$.2f", discountedPrice);
  4. System.out.printf("$%10.2d", discountedPrice);
  5. System.out.printf("$%.2f", discountedPrice);

 

Activity - Strings

Record your participation in Piazza clicker question Lesson8 Q3

Look at this string:

String city = "San Jose";

S a n   J o s e
0 1 2 3 4 5 6 7

Concept - substring

Often you want just a portion of a string, called a substring. To get a substring, use the following code

str.substring(start, pastEnd)

Given this string:

String str = "Udacity";

U d a c i t y
0 1 2 3 4 5 6

look at these substring examples:

substring code to extract the substring
U

str.substring(0,1)

str.charAt(0)

y

str.substring(6)

str.substring(str.length() -1)

str.charAt(6)

str.charAt(str.length() -1)

Uda

str.substring(0, 3)

city

str.substring(3,7)

str.substring(3)

 

Activity - Substrings

Record your participation in Piazza clicker question Lesson8 Q4

Given this declaration:

String word = "University";

What is returned by word.substring(1, 5);

  1. Unive
  2. Univ
  3. nive
  4. niver

Activity - Strings

Record your participation in Piazza clicker question Lesson8 Q5

Now assume we have a string of unknown length called text. We know it contains more than one word.

 

 

Activity - Program using substring

Record your participation in Piazza clicker question Lesson8 Q6

Write a program that asked the user to enter a String from the keyboard. Print the string and then print the first and last characters. Test using the Strings hello and San Jose

Here is the skeleton of a project to get input

Activity - indexOf

Record your participation in Piazza clicker question Lesson8 Q7

Write a program that asked the user to enter a phrase of 3 words. Print the phrase and then print just the first word and finally print the length of the first word.

The first word is the letters up to the space. You can find the position of the space by using this statement

int indexOfSpace = pharse.indexOf(" ");