cs177 week2: recitation primitive data types and strings with code examples

20
CS177 Week2: Recitation Primitive data types and Strings with code examples

Upload: bruce-cannon

Post on 04-Jan-2016

245 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS177 Week2: Recitation Primitive data types and Strings with code examples

CS177

Week2: Recitation

Primitive data types and Stringswith code examples

Page 2: CS177 Week2: Recitation Primitive data types and Strings with code examples

Questions?

Page 3: CS177 Week2: Recitation Primitive data types and Strings with code examples

Data Types

Page 4: CS177 Week2: Recitation Primitive data types and Strings with code examples

int int x; //declaration of variable x x = 23; //assigns the number 23 to x Stored as 1’s and 0’s. 23d = 10111b.

d stands for decimal, b for binary

Page 5: CS177 Week2: Recitation Primitive data types and Strings with code examples

Binary -> Decimal Conversion What does 11011110b equal in decimal?

Page 6: CS177 Week2: Recitation Primitive data types and Strings with code examples

222

Page 7: CS177 Week2: Recitation Primitive data types and Strings with code examples

Decimal -> Binary Conversion Do the following steps:

Start with a decimal number x Set binary number b = 0 Until x = 0

Is x odd? If yes, put a 1 at the end of b If no, put a 0 at the end of b

Divide x by 2, rounding down (truncation) Reverse the digits of b

Page 8: CS177 Week2: Recitation Primitive data types and Strings with code examples

Ex: x = 9

Page 9: CS177 Week2: Recitation Primitive data types and Strings with code examples

Ex: x = 222

11011110b = 222d, just like before

Page 10: CS177 Week2: Recitation Primitive data types and Strings with code examples

Positive and Negative Numbers Positive numbers start with a 0 bit Negative numbers start with a 1 bit 2’s complement – way that a negative

number is represented

Page 11: CS177 Week2: Recitation Primitive data types and Strings with code examples

Doubles Rational numbers (can still be an integer, but

it is stored differently) 64 bits

double y = 1.25; //declares double y and assigns 1.25 to y

double z = 3; double a = 2.0; double b = -8.8;

Page 12: CS177 Week2: Recitation Primitive data types and Strings with code examples

Loosely typed or strongly typed? Java is:

Strongly typed double x; x = 3; What is the type of variable x? A double!

Page 13: CS177 Week2: Recitation Primitive data types and Strings with code examples

Strings Many letters in one type String word1 = “purdue”; String word2 = “boilermakers”; String word3 = “p”; String word4 = “”; //This is legal – called

empty String

Page 14: CS177 Week2: Recitation Primitive data types and Strings with code examples

Chars char – one character. Could be any ASCII

character String – many characters together char w = ‘a’; char x = w; char y = ‘w’; String z = “w”; Are the variables w and x equal? Are the variables x and y equal? Are the variables y and z equal?

Page 15: CS177 Week2: Recitation Primitive data types and Strings with code examples

String - String concatenation

/*************************************************************************

* Compilation: javac Ruler.java

* Execution: java Ruler

*

* Prints the relative lengths of the subdivisions on a ruler.

*

* % java Ruler

* 1

* 1 2 1

* 1 2 1 3 1 2 1

* 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

* 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

*

*************************************************************************/

Page 16: CS177 Week2: Recitation Primitive data types and Strings with code examples

public class Ruler {

public static void main(String[] args) {

String ruler1 = "1 ";

String ruler2 = ruler1 + "2 " + ruler1;

String ruler3 = ruler2 + "3 " + ruler2;

String ruler4 = ruler3 + "4 " + ruler3;

String ruler5 = ruler4 + "5 " + ruler4;

System.out.println(ruler1);

System.out.println(ruler2);

System.out.println(ruler3);

System.out.println(ruler4);

System.out.println(ruler5);

}

}

Page 17: CS177 Week2: Recitation Primitive data types and Strings with code examples

Double Operation -Quadratic formula

/*************************************************************************

* Compilation: javac Quadratic.java

* Execution: java Quadatic b c

* Given b and c, solves for the roots of x*x + b*x + c.

* Assumes both roots are real valued.

* % java Quadratic -3.0 2.0

* 2.0

* 1.0

* % java Quadratic -1.0 -1.0

* 1.618033988749895

* -0.6180339887498949

* * Remark: 1.6180339... is the golden ratio.

* * % java Quadratic 1.0 1.0

* NaN

* NaN

*************************************************************************/

Page 18: CS177 Week2: Recitation Primitive data types and Strings with code examples

public class Quadratic {

public static void main(String[] args) {

double b = Double.parseDouble(args[0]);

double c = Double.parseDouble(args[1]);

double discriminant = b*b - 4.0*c;

double sqroot = Math.sqrt(discriminant);

double root1 = (-b + sqroot) / 2.0;

double root2 = (-b - sqroot) / 2.0;

System.out.println(root1);

System.out.println(root2);

}

}

Page 19: CS177 Week2: Recitation Primitive data types and Strings with code examples

Char Operation

/*************************************************************************

* Compilation: javac Charactertest.java

* Execution: java Charactertest

Is this letter lowercase? true

Is this letter uppercase? false

Is this a letter? true

/*************************************************************************

public class Charactertest {

public static void main(String args[]) {

char letter = 'd';

boolean lowerCase = Character.isLowerCase(letter);

boolean upperCase = Character.isUpperCase(letter);

boolean letterOrdigit = Character.isLetter(letter);

System.out.println("Is this letter lowercase? " + lowerCase);

System.out.println("Is this letter uppercase? " + upperCase);

System.out.println("Is this a letter? " + letterOrdigit);

}

Page 20: CS177 Week2: Recitation Primitive data types and Strings with code examples

Questions?