Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote...

32
Escape Sequences \n newline \t tab \b backspace \r carriage return \’ single quote \” double quote \\ backslash

Transcript of Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote...

Page 1: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Escape Sequences

\n newline

\t tab

\b backspace

\r carriage return

\’ single quote

\” double quote

\\ backslash

Page 2: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Typecasting

Typecasting allows you to take a value that belongs to one type and “cast” it into another type.

The syntax for typecasting is to put the name of the type in parentheses and use it as an operator.

1. double pi = 3.14159;

2. int x = (int) pi;

3. System.out.println(x);

4. // What will the output be?

Page 3: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Precedence

Typecasting takes precedence over arithmetic operations.

1. double pi = 3.14159;

2. int x = (int) pi * 20.0;

3. System.out.println(x);

4. // What will the output be?

Converting to an integer always rounds down, even if the fraction part is 0.99999999

Page 4: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Math methods

Like objects, classes can have methods too. One of the most commonly used classes in Java is Math.

Methods of classes are invoked the same as they are for objects.

Page 5: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Math.sqrt

To find a square root of a number in Java, the Math.sqrt method can be called.

The Math.sqrt method takes a single argument which is the a double and returns a double which is its square root.

1. double x = Math.sqrt(9.0);

2. System.out.println(x);

3. // What will the output be?

Page 6: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Math.pow

Page 7: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Trig Functions

The common trigonometric functions can be called using methods of the Math class.

The trig function methods take a single double argument which is the angle in radians and returns a double which is the result of that trigonometric function.

1. double angle = 1.5;

2. double sine = Math.sin(angle);

3. double cosine = Math.cos(angle);

4. double tangent = Math.tan(angle);

Page 8: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

math

Java also provides a few commonly used numbers such as pi. The double Math.PI can be used anywhere in place of π.

1. double halfPie = Math.PI/2;

Another useful method of Math is round. Math.round can be used to round off a floating point number to the nearest integer which is then returned as an int.

1. int x = Math.round(Math.PI * 20.0);

2. System.out.println(x);

3. // What is the output?

Page 9: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

More Math methods

Page 10: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Exercise 2-1

Copy the code below and calculate the area of a circle with a radius of 63.4 and round it to the nearest whole number.

1.class Circle{

2. public static void main(String[] args) {

3. double radius = 63.4;

4.

5. // your code goes here

6.

7. System.out.println(area);

8. }

9.}

Page 11: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Adding new methods

We can define new methods to do specific tasks and then call them in our main method like we have been doing with the previous few examples.

When a method is called from somewhere else in the program, it is “invoked”.

Methods have the following syntax:

1. public static void NAME( PARAMETERS) {

2. STATEMENTS

3. }

Page 12: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Methods

By convention, Java methods start with a lower case letter and use camelCase.

The list of parameters specifies what information, if any, you have to provide to invoke the new method.

When a method is called from somewhere else in the program, it is supplied arguments which match up with the parameters of the method.

Page 13: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Methods

Copy the following method in to your program. This method should go inside your Class but before the main method.

1. public static void newLine() {

2. System.out.println(“”);

3. }

This method is named newline and the empty parentheses means that it takes no arguments. It contains one statement which prints an empty String before skipping to the next line.

Page 14: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Calling a method

1. class NewLine {

2. public static void newLine() {

3. System.out.println(“”);

4. }

5. public static void main(String[] args){

6. System.out.println(“First line.”);

7. newLine();

8. System.out.println(“Second line.”);

9. }

10. }

11. // What will the output be?

Page 15: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Exercise 2-2

Add a new method after newLine named threeLine. threeLine should have no return type (void) and take no parameters. make threeLine invoke newLine three times. In your main method, copy the following:

1.System.out.println(“First line.”);

2.threeLine();

3.System.out.println(“Second line.”);

4.// What is the output?

Page 16: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

class

A class is a collection of related methods. In the last example, the class named NewLine contained three methods, named newLine, threeLine, and main.

Methods of other classes then the one we are writing in can be called by specifying the name of the class.

1. Math.abs(73); // abs is a method of the class Math

2. newLine(); // Java assumes newLine is a method of the class we are writing (NewLine).

Page 17: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

parameters

A parameter is a variable that stores an argument. The parameter list of a method indicates what arguments are required.

For example, printTwice specifies a single parameter, s, that has type String.

1. public static void printTwice(String s) {

2. System.out.println(s);

3. System.out.println(s);

4. }

Page 18: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

parameters

To invoke printTwice, a single argument with the type String must be provided.

1. printTwice(“Don’t make me say this twice!”);

When a method is invoked, the arguments that are provided to it are assigned to the parameters. This is known as parameter passing. The argument must have the same type as the parameter

Variables can be used as arguments as well.

1. String argument = “Never say never.”;

2. printTwice(argument);

Page 19: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Multiple parameters

Methods can have multiple parameters by declaring each one separately.

1. public static void printTime(int hour, int minute) {

2. System.out.print(hour);

3. System.out.print(“ : “);

4. System.out.println(minute);

5. }

When invoking functions, remember that you do not have to declare the types of arguments.

1. printTime(int hour, int minute); // INCORRECT

2. printTime(hour, minute); // CORRECT

Page 20: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Exercise 2-3

Recreate the Midnight program which calculates and displays the number of seconds since midnight. Use the following code in your main method and create the new method calculateSeconds which does the calculations and prints out the result.

1. public static void main(String[] args){

2. int hour = 10;

3. int minute = 02;

4. int second = 37;

5. calculateSeconds(hour, minute, second);

6. }

Page 21: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

boolean

The boolean data type only has two possible values, true or false. True and false are Java keywords so they can’t be used as variable names.

1. boolean isMale = true;

2. boolean is21 = false;

Page 22: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Conditionals

To write useful programs, we need to check conditions and change the behavior of the program accordingly. This is done via conditional statements.

The if statement is the simplest form of a conditional statement.

1. if (x > 0) {

2. System.out.println(“x is positive”);

3. }

The expression in the parenthese is called a condition. If it is true, then the statements in brackets get executed. If not true, nothing happens.

Page 23: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Relational Operators

x == y x != y x > y x < y x >= y x <= y

Remember that = is the assignment operator while == is a comparison operator.

The two sides of a condition operator have to be the same type.

Page 24: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Alternative Execution

Another form of conditional execution is alternative execution in which there are two possibilities and the condition determines which one gets executed.

1. if (x%2 == 0) {

2. System.out.println(“x is even”);

3. } else {

4. System.out.println(“x is odd”);

5. }

Page 25: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Exercise 2-4

Create a new Class called Parity with the following method: public static void printParity(int x) which prints out either “<x> is even” or “<x> is odd” whether x is even or odd. Put the following statements in your main method.

1. public static void main(String[] args){

2. printParity(17);

3. printParity(-486);

4. printParity(10000);

5. }

Page 26: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Chained Conditionals

One way to check for a number of related conditionals and choose one of several actions is by chaining a series of ifs and elses.

1. if (x > 0) {

2. System.out.println(“x is positive”);

3. } else if (x < 0) {

4. System.out.println(“x is negative”);

5. } else {

6. System.out.println(“x is zero”);

7. }

Page 27: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Nested Conditionals

Conditionals can be nested within another.

1. if (x == 0) {

2. System.out.println(“x is zero”);

3. } else {

4. if (x > 0) {

5. System.out.println(“x is positive”);

6. } else {

7. System.out.println(“x is negative”);

8. }

9. }

Page 28: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

The return statement

The return statement allows you to terminate the execution of a method before you reach the end. One reason to use it is if you detect an error condition.

1. public static void printLogarithm(double x) {

2. if (x <= 0.0) {

3. System.out.println(“Positive numbers only, please.”);

4. return;

5. }

6. double result = Math.log(x);

7. System.out.println(“The log of x is “ + result);

8. }

Page 29: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Recursion

A method can invoke itself. This is called recursion.

1. public static void square (int x) {

2. if (x > 100) {

3. System.out.println(x);

4. }else {

5. System.out.println(x);

6. square(x*x);

7. }

8. }

Page 30: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Exercise

1. public static void countdown(int n) {

2. if (n == 0) {

3. Sysem.out.println(“Blastoff!!!”);

4. } else {

5. System.out.println(n);

6. countdown(n – 1);

7. }

8. }

Page 31: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Return values

So far, all of our methods have been void: that is, methods that return no value. When a void method is invoked, it is typically on a line by itself with no assignment.

1. countdown(100);

2. newLine();

Methods can also return things. These methods are called value methods.

For value methods, just replace void with the type the function is going to return. To return the value, write the return statement followed by the expression we are returning.

1. public static double area(double radius) {

2. return Math.PI * radius * radius;

3. }

Page 32: Escape Sequences \nnewline \t tab \bbackspace \rcarriage return \single quote \double quote \\backslash.

Exercise 2-5

Write a method that takes in x1, x2, y1, and y2 and returns the distance between the two points.

1. public static double distance

2. (double x1, double y1, double x2, double y2) {

3. double dx = x2 - x1;

4. double dy = y2 - y1;

5. double dsquared = dx*dx + dy*dy;

6. double result = Math.sqrt(dsquared);

7. return result;

8. }