Testimony Jeffrey MacDonald trial - Aug 17 1979 Segal - Murtagh
Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit:...
-
Upload
henry-haynes -
Category
Documents
-
view
230 -
download
0
description
Transcript of Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit:...
![Page 1: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/1.jpg)
JavaPrimitive Types, Operators,
and Strings(Java: An Eventful Approach, Ch 5),
Slides Credit: Bruce, Danyluk and Murtagh
CSCI 120 Lecture 15
30 October 2012
![Page 2: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/2.jpg)
Primitive Data Type
• Can write value as a literal
Ex: int a=1732
• Can perform operations using operator symbols
Ex: x+1
![Page 3: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/3.jpg)
Operators vs. Method Invocations
• Operators produce new valuesEx: if the value of count is 3
count+1 will produce 4, but will not change count
• Method Invocations can modify objectsEx: box.move(-1,-1); changes the position of the box
![Page 4: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/4.jpg)
Operators and Precedence Rules• Arithmetic and logical negations, - and !• Multiplication and division: * and /• Addition and subtraction: + and –• Comparisons: <,<=,>,>=• Equality and inequality: == and !=• And: &&• Or: ||
Ex: a+b*c is the same as a+(b*c) but not (a+b)*c
![Page 5: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/5.jpg)
Numeric Types
• Integers:Ex: int anInt=99;
• Real numbers:Ex: double aDouble=98.6;
An int can be converted into a double without loss of precision, but not vice versa
![Page 6: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/6.jpg)
Dividing int and double
int doubleint int result double result
double double result double result
Clearly, unless an integer is divided by another integer, all results are double.
Ex: 3.0 / 4.0 = 0.75 3 / 4.0=0.75
3 / 4 = 0
The following table summaries the types of result one will get when dividing two integers, two doubles, or a double and an integer
![Page 7: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/7.jpg)
How a double is displayed
• If you print a double, the output is always accompanied by a decimal place.
Ex: double a=1000;
System.out.print(a); will output 1000.0
• Large numbers use scientific notation
Ex: double a=1000000000; System.out.print(a); will output 1.0E9
![Page 8: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/8.jpg)
Selecting a Numeric Type
• Use int whenever you need whole numbers only.
• Use double when you need non-integer values
• Use int when methods demand it
Ex: setColor( int, int, int )
![Page 9: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/9.jpg)
Useful Functions on double
Math.pow(a,b) ab
Math.exp (b) eb
Math.ln(a) ln(a)Math.sqrt(a) Square root of a
![Page 10: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/10.jpg)
System.currentTimeMillis()
• Used to check elapsed timeEx: printing out the duration of a mouse press
public void onMousePress(Location point){ startingTime=System.currentTimeMillis();}public void onMouseRelease(Location point){ System.out.println(System.currentTimeMillis()-startingTime));}
![Page 11: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/11.jpg)
String
• Java uses String to manipulate textual data• Quoted text is of type String• Programs can combine String objects
Ex: String a="He";
String b="llo";System.out.print(a+b); will print out Hello
• String accumulators: b = b + " world";
![Page 12: Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture 15 30 October.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b207f8b9ab0599951c5/html5/thumbnails/12.jpg)
Student To Do’s• HW09
– Exercise 4.9.2 (Dicey)– Exercise 3.12.3 (Random Box)– Due Monday 11/5 by 1:25pm– Submit your .java files and bluej project file.
• Read Java: An Eventful Approach– Ch. 5 (Today)
12