Built-in Types: Escape Sequences

Escape Characters

Suppose you want to print a message with quotation marks in the output. Can you write a statement like this?

System.out.println(“He said “Java is fun””);

No, this statement has a compile error. The compiler thinks the second quotation character is the end of the string and does not know what to do with the rest of the characters.

To overcome this problem, Java uses a special notation to represent special characters, as shown in Table 2.6. This special notation, called an escape character, consists of a backslash (\) followed by a character or a character sequence. For example, \t is an escape character for the Tab character and an escape character such as \u03b1 is used to represent a Unicode. The symbols in an escape character are interpreted as a whole rather than individually.

So, now you can print the quoted message using the following statement:

System.out.println(“He said \”Java is fun\””); The output is
He said “Java is fun”
Note that the symbols \ and together represent one character.

Escape Characters

Copy and paste this table to your JavaIntroNotes_YI document

https://docs.oracle.com/javase/tutorial/java/data/characters.html

  • Write a program NamePrinter_YI that displays your name inside a box, like this:

Do your best to approximate lines with characters, such as |, -, and +.

  • Write a program FacePrinter_YI that prints a face, using text characters, hopefully, better looking than this one:
  • (Optional)Write a program CatPrinter_YI that prints an animal speaking a greeting, similar to (but different from) the following

Built-in: Wind Chill + Scanner + Math.random()

 

Screen Shot 2014-09-09 at 10.24.34 PM

  1. Wind chill.
    Given the temperature t (in Fahrenheit) and the wind speed v (in miles per hour), the National Weather Service defines the effective temperature (the wind chill) to be:

screen-shot-2016-09-20-at-8-46-04-am

screen-shot-2016-09-20-at-9-02-54-am

Write a program WindChill_YI.java that takes two double command-line arguments t and v and prints the wind chill. Use Math.pow(a, b) to compute ab.
Note: the formula is not valid if t is larger than 50 in absolute value or if v is larger than 120 or less than 3 (you may assume that the values you get are in that range).

Resources for some of these assignments:

[spoiler title=’Math.random()’]
/**
 * An itty bitty program to learn about Math.random()
 * GE
 * 9/28/2017
 * 
 */
public class MathRandTest_GE
{
   public static void main(String []args)
   {
       for (int i = 0; i < 100; i++)
        {
            System.out.println(Math.random());
        }

    }
}

[/spoiler] [spoiler title='Scanner Class']
/**
 *  good resource: http://docs.oracle.com/javase/tutorial/essential/io/scanning.html
 * @author gracielaelia
 * Problem: Both hasNext and next methods may block waiting for further input.
 * hasNext() doesn't return false ever!!!! next() blocks until something is input!
 * As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), 
 * or at the end of the file if you use < style redirection.
 * You cannot easily send and EOF character using the keyboard"
 * -- usually Ctrl+D on Linux/Unix/Mac 
 * or Ctrl+Z on Windows does it
 */   
import java.util.Scanner;

public class ScannerTest_GE{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);
        String name;     // just a declaration
        double base, exponent;
        System.out.println("Enter the base ");
        base = scan.nextDouble();
        System.out.println("Enter the exponent ");
        exponent = scan.nextDouble();
        System.out.println("Enter your name ");
        name = scan.next();
        System.out.println(name + " the value is " + Math.pow(base,exponent));
        System.out.println(name + " the integer value is " + (int)Math.pow(base,exponent));
    }
  
} 
[/spoiler]

Conditionals: if if/else flowcharts

New topic: Conditionals
Click on this label:
Screen Shot 2014-10-02 at 10.05.29 AM

Classwork:
Flowcharts: write a short code fragment for each of the following flowcharts:
if and if/else
flowchars-if-else

while and for loops

flowchart-loop

More on conditionals with the slides on this link:

Assignments:

  1. Write a more general and robust version of ComplexRootsQuad.java that prints the roots of the polynomial ax^2 + bx + c, prints real and complex solutions.
  2. What (if anything) is wrong with each of the following statements?
    if (a > b) then c = 0;
    if a > b { c = 0; }
    if (a > b) c = 0;
    if (a > b) c = 0 else b = 0;
  3. Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.
  4. Write a program AllEqual_YI.java that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise.
  5. Rewrite TenHellos.java to make a program ManyHellos_YI.java that prompts the user for the consecutive lines to print… You may assume that the argument is less than 1000. Hint: consider using i % 10 and i % 100 to determine whether to use “st”, “nd”, “rd”, or “th” for printing the ith Hello.Example output:
    * Starting number? 4
    * Ending number? 8

    * 4th Hello
    * 5th Hello
    * 6th Hello
    * 7th Hello
    * 8th Hello

  6. What is the value of m and n after executing the following code?
    int n = 123456789;
    int m = 0;
    while (n != 0) {
       m = (10 * m) + (n % 10);
       n = n / 10;
    }
    
    
  7. What does the following code print out?
    int f = 0, g = 1;
    for (int i = 0; i <= 15; i++) {
       System.out.println(f);
       f = f + g;
       g = f - g;
    }
    
    

A site to check your understanding:


Conditionals: Ex 1-4

 

Classwork:

  1. Write a program YI_FivePerLine.java that, using one for loop and one if statement, prints the integers from 1000 to 2000 with five integers per line. Hint: use the % operator.

  2. Write a program YI_FunctionGrowth.java that prints a table of the values of ln n, n, n ln n, n^2, n^3, and 2^n for n = 16, 32, 64, …, 2048. Use tabs (‘\t’ characters) to line up columns.

Homework:

  1. What is the value of m and n after executing the following code?

int n = 123456789;
int m = 0;
while (n != 0) {
   m = (10 * m) + (n % 10);
   n = n / 10;
}

  1. What does the following code print out?

int f = 0, g = 1;
for (int i = 0; i <= 15; i++) {
   System.out.println(f);
   f = f + g;
   g = f - g;
}

Conditionals: Ex 1-7

Screen Shot 2014-09-23 at 10.00.31 AM

Classwork:
1. Write a more general and robust version of YI_Quadratic.java that prints the roots of the polynomial ax^2 + bx + c, prints an appropriate error message if the discriminant is negative, and behaves appropriately (avoiding division by zero) if a is zero.

  1. What (if anything) is wrong with each of the following statements?
    if (a > b) then c = 0;
    if a > b { c = 0; }
    if (a > b) c = 0;
    if (a > b) c = 0 else b = 0;

  2. Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise.

Homework: Conditionals and loops
4. Write a program YI_AllEqual.java that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise.

  1. Rewrite YI_TenHellos.java to make a program YI_Hellos.java that takes the number of lines to print as a command-line argument. You may assume that the argument is less than 1000. Hint: consider using i % 10 and i % 100 to determine whether to use “st”, “nd”, “rd”, or “th” for printing the ith Hello.

  2. What is the value of m and n after executing the following code?

int n = 123456789;
int m = 0;
while (n != 0) {
   m = (10 * m) + (n % 10);
   n = n / 10;
}

  1. What does the following code print out?
int f = 0, g = 1;
for (int i = 0; i <= 15; i++) {
   System.out.println(f);
   f = f + g;
   g = f - g;
}

Conditionals: Simple Interest

Screen Shot 2014-09-23 at 10.00.31 AM

Write a program, SimpleInterest_YI.java that uses a while loop to calculate how many years it would take to have at least $1,100 at an interest rate of 1.2% when $1,000 is deposited in a savings bank account. DO NOT USE THE FORMULA.

stack-of-money

Simple interest is money you can earn by initially investing some money (the principal). A percentage (the interest) of the principal is added to the principal, making your initial investment grow!

As part of your program include the calculation done with the formula.

An example of Simple Interest

Conditionals: Sum of Squares Difference

Project Euler Problem 6

screen-shot-2016-10-27-at-8-36-29-am

Sum square difference
The sum of the squares of the first ten natural numbers is,

1^2 + 2^2 + … + 10^2 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Conditionals: Even Fib Numbers

screen-shot-2016-10-27-at-8-36-29-am

Project Euler
Even Fibonacci numbers
Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Write a java program, EvenFibPE2_YI.java to solve this problem.