Lesson part 2 - WordPress.com

advertisement
int, double, boolean
 If the turtle starts in the
middle of the screen and
turns 1 degree and moves
1 space, where should it
go?
T
 Integers cannot store any partial piece, they simply
throw away any decimal
 In Java,
 1.1*2 = 2.2
 But (int)(1.1*2) = 2//cast into an int
 In these slides we’ll talk about how the basic data types
work
 Every language, even an object oriented one needs
some basic building blocks
 Java has many basic types that do not belong to a class
 They are very limited in what they do
 They can only represent a value
 We will focus our attention to 3 types
 boolean
 int
 double
 Named after George Boole for his work in algebraic
logic
 How we evaluate logical statements
 A boolean variable can only represent one of two
values, true or false.
 boolean loveToCode = true;
 Later we will look at the algebraic
rules of evaluating a logical statement
 and (&&), or (||), not (!)

Java represents signed (positive and negative) integers with the int data type.


32 bits
Each bit can be a 0 or 1 (2 values) so there are:


Roughly you can think of this as half positive and half negative:



Without getting to much into detail, zero falls into the positive half of that range as a convenience
for the hardware (see 2’s complement representation).
So positive numbers are then 2147483648 – 1 = 2147483647


232 / 2 = 231 = 2147483648
The reason I say roughly is zero has to fit in.


2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2 = 232 = 4294967296
representations
Range: 1 to (231 – 1) = 1 to 2147483647
Negative numbers range: –231 to (-1) = –2147483648 to –1
Why would knowing this be important?


Would Bill Gates bank where they stored balances with ints?
What if they didn’t use negative numbers?
long is a 64 bits signed integer in Java
 Beyond this you would need to use an arbitrary precision library




BigInteger/BigDecimal (built in)
APFloat
Lots of fun mathematics here…
 Integer is the class in Java that “wraps” an int
 Wrapper classes represent a basic data type but add
functionality
 int ten = “10”;//error
 Integer ten = new Integer(“10”);//OK
 Integers and ints are “friends”… in Java it’s called
autoboxing
 Integer wrapperInt = new Integer(123);
 int basicInt = wrapperInt;//basicInt = 123
 See the Integer class for available methods
 Some useful constants:
 Integer.MAX_VALUE
 Integer.MIN_VALUE
Operator
Purpose
Example
+
Addition
int sum = 3 + 5;//8
-
Subtraction
double difference = 9.5 – 2.25;//7.25
*
Multiplication
double product = 1.1*1.1;//should be 1.21 but not
/
Division
int quotient = 10/4;//2, not 2.5!
double quotent = 10.0 / 4;// 2.5
%
Remainder
int remainder = 21%5;//1
()
Brackets –
change order
of operations
double value = (3+5)*2;//16
 Computers don’t store separate integers for the time.
Instead they just store one REALLY big integer.
 Suppose I told you that it is 35595 past midnight.
 What time is it?
 This is one of your exercises with % and /
 There are lots of useful places to use a remainder
 Random numbers!
 Let’s try some:
 13 % 2 = ?
 95 % 10 = ?
 18 % 5 = ?
 57 % 100 = ?
 How do you know if an integer is even or odd?
 How do you separate the digits of 1234?
 In Java the data type is called double
 Named from using a double “word” of memory
 In Java it’s 64 bits (2 – 32 bit words)
 doubles store decimal values in their representations
 Some decimals can’t be represented…
 Ex. 1/3 in decimal is 0.33333333
 It’s worse with binary numbers. Only numbers that can
be represented as a power of 2 can be stored exactly.
 Ex. ½, ¼, 1/8, 1/16, …
 This can lead to some interesting math
 Like ints and Integers there is also a Double wrapper
class
 One basic way to get text output in Java is to use
System.out.print or System.out.println
 Try to find a pair of decimal numbers that produce an
error when multiplied.
 Ex. System.out.println(3.5 * 8.5);//prints 29.75, worked
 Try executing the following code:
 System.out.println(15 / 4);
 System.out.println(15 / 4.0);
 System.out.println(15.0 / 4);
 System.out.println(15.0 / 4.0);
 What do you notice?
 First thing to know:
 Two integers will produce an integer result
 If a decimal is present in the calculation the result will
also have a decimal
 Try it:
 2 + 10/3 – 1.5 =
 25/(1.5 + 2.5) =
 2*8/12 + 4/3*2.5 =
 5/2 – 13%3 + 5*(1+2) =
 How many hours are there is 390 minutes?
 int minutes = 390;
 final int MINUTES_PER_HOUR = 60;
 double answer = minutes / MINUTES_PER_HOUR;
 Answer is 390/60 = 6 but since it’s a double, 6.0
 But what if you actually wanted 6.5
 You’d need to change the data types
 This is called casting
 double answer = (double)minutes / MINUTES_PER_HOUR;
 (double) changed minutes from an int to a double
 could also have been (double)MINUTES_PER_HOUR





int integer = 10;
double decimal = integer;//OK no loss of precision
integer = decimal;//Error, decimal thrown away!
integer = (int)decimal;//OK, tells Java you meant to
Rule:
 If changing data types will result in a loss of information you must
cast in order to show Java you have acknowledged the loss in your
code.
 Later we will see how this applies to class hierarchies
 Example, all Human are Mammals
 Human teacher = new Human(“Mr . Joyce”);
 Mammal person = teacher;//OK
 Not all Mammals are Humans
 Mammal cat = new Mammal(“Foofoo”);
 Cat fluffy = cat;
 Human pet = fluffy;//Compile time Error
 Human someone = person;//Still an error
 Human someone = (Human)person;//now OK –run time error if not
 Which produces an error?
 int a = (int)(5/2.0)+1;
 int b = 2 + 9/3.0;
 int c = 5/(int)2.0 – 1;
 double d = (int)(9.5 – 1.5)/4;
 double e = 15/4 – 2*3 + 5%4;
 int i1 = 5;
 int i2 = 24;
 int i3 = 9;
 double d1 = 2.5;
 double d2 = 10;
 double d3 = 1.25;






int result = (int)d2/(int)d3;
int result = (int)(d2-d1)/i1;
int result = (int)(((int)d1 + i3)/i1);
int result = (int)((d1 + i3)/d3);
double result = ((int)d1-(int)d3)/d2;
double result = (int)((double)i2/d1)/i3;
 The Math class contains… well Math
 Square roots
 Exponents
 Trig functions
 …
 Just about everything you need for calculation
 Something else that’s important to programming is
randomness
 Math.random()
 Random numbers can be generated from the random
method of the Math Class
 static double random();
 returns a random number between 0.0 (inclusive) and
1.0 (exclusive)
 Ex.
 double randomNumber = Math.random() * 100;
 This is a random number between 0.0 and 99.999999
 Ex.
 double randomNumber = Math.random() * 35;
 Between 0.0 and 34.9999999999
 double randomNumber = Math.random() * 50 + 10;
 Between 10.0 and 59.999999999
 int randomNumber = (int)(Math.random() * 20) + 5;
 Between 5 and 24
 don’t forget to cast with ints
 Notice the range is 5 to 24!
 Math.random() * 20 returns 0 to 19.999999999999…
 (int)(Math.random() * 20) chops off all decimals
 Returns 0 to 19
 Add 5 and the range moves to 5 – 24
 (int)Math.random() * 20 + 5;
 returns 5 every time your program is run:
 Generate a random value, say 0.774312
 Cast it to an it
 Now it has the value 0
 0 * 20 = 0
 0+5=5
 If you want to generate a number in the range [a,b) 
not including b (as doubles)
 Math.random()*(b – a) + a
 If you want to generate a number in the range [a,b] (as
ints)
 (int)(Math.random()*(b – a+1)) + a
 A helpful way to remember what you are doing is to
realize that the formula is this:
 (int)(Math.random()*(number of values)) + first value
 Eg. There are 22 values from 50 – 71, the first is 50
 (int)(Math.random()*(22)) + 50
 Basic Data types in our course
 boolean
 int
 double
 Wrapper classes
 Rounding errors
 Casting
 Implicit when no information lost
 Must be present when information could be lost
 (ie. double to int)
 Can strategically cast data types
 Math between data types
 The Math class
 Random numbers
Download