Basic Computation
Fundamentals of Computer Science
public static void main(String [] args)
logical AND logical OR logical NOT && || !
Basic Computation logical AND logical OR logical NOT && - - PowerPoint PPT Presentation
Basic Computation logical AND logical OR logical NOT && || ! public static void main(String [] args) Fundamentals of Computer Science Outline Data Types Variables Primitive Data Types The String Class Type
Fundamentals of Computer Science
public static void main(String [] args)
logical AND logical OR logical NOT && || !
Variables Primitive Data Types The String Class Type Conversion
Think of them as places to store data. They are implemented as memory locations.
The value is stored in the memory location.
“I'm going to need an integer and let's call it a” REMEMBER: in Java you are required to declare a variable before using it!
“Whenever I say a, I mean the value stored in a”
“I want the value 10”
“Variable b gets the literal value 7”
“Make me an integer variable called c and assign it the value obtained by adding together a and b” = in CS is not the same as = in math!
4
With digits after a decimal point or Using e notation.
Java provides mechanism to …
Define a variable Initialize it Fix the value so it cannot be changed
public static final double PI = 3.14159;
int, double, and char are primitive types.
"Java is fun" is a value of class type String
DEFINITION
Stores information your program needs Each has a unique name Each has a specific type
Java built-in type what it stores example values
String sequence of characters "Hello world!" "I love this!" concatenate char characters 'a', 'b', '!' compare int integer values 42 1234 add, subtract, multiply, divide, remainder double floating-point values 9.95 3.0e8 add, subtract, multiply, divide boolean truth values true false and, or, not
8
Just a block of memory in your computer Size of block measured in bits (number of 0s or 1s) Integers:
9
type bits example byte 8 0110 1110 short 16 0110 1110 1101 1101 int 32 0101 1001 0000 0001 0111 1101 0110 0010 long 64 1101 0011 1001 0001 1101 0101 1010 0101 0111 1010 0011 1010 1011 1100 1111 1111
10
011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000000000101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101010101010101010101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101
byte x;
It's good practice not to rely on a default value.
12
011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000001110101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101010101010101010101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101
primitive value byte x = 7;
An integer value between -231 and +231-1 Between -2,147,483,648 and 2,147,483,647 Operations: add
subtract multiply divide remainder +
/ % example result comment 10 + 7 17 10 - 7 3 10 * 7 70 10 / 7 1 integer division, no fractional part 10 % 7 3 remainder after dividing by 7 10 / 0 runtime error, you can't divide an integer by 0! Watch out for this! / is integer division if both sides are integers!
13
count++ or ++count
count-- or --count
count++; ++count; count = count + 1; count--;
count = count - 1;
Floating-point number (as specified by IEEE 754) Operations:
add subtract multiply divide +
/ example result 9.95 + 2.99 12.94 1.0 - 2.0
1.0 / 2.0 0.5 1.0 / 3.0 0.3333333333333333 1.0 / 0.0 Infinity 0.0 / 123.45 0.0 0.0 / 0.0 NaN
15
865000000.0 can be written as 8.65e8 0.000483 can be written as 4.83e-4
18
Either true or false Controls logic and flow of control in
programs
Operations:
logical AND logical OR logical NOT && || !
19
a !a true false false true logical AND logical OR logical NOT && || ! a b a && b a || b false false false false false true false true true false false true true true true true
20
char data type
Holds a single character Single apostrophe, e.g. 'a', 'z'
public class CharExample { public static void main(String [] args) { char ch1 = 'y'; char ch2 = 'o'; String result = "" + ch1; result = result + ch2; result = result + ch2; result = result + ch2; System.out.println(result); } } % java CharExample yooo
Double quotes with nothing in between, an empty String
You can't, for example, assign a floating point value to a
22
011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000001110101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101010101010101010101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101
primitive value byte x = 7;
011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000010000101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101010101010101010101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101
23
primitive value byte x = 7; x = x + 1;
011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000010000101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101000000000000011101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101
24
primitive values
byte x = 7; x = x + 1; short y = 7;
011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000010000101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101000000000000011101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101
25
byte x = 7; x = x + 1; short y = 7; x = y;
primitive values
You may know 7 can fit in a byte, but compiler doesn't!
But not to a variable of any type further to the left.
Sequence of characters Treated as a single item.
greeting = "Hello!";
String greeting = "Hello!";
String greeting = new String("Hello!");
String greeting = "Hello"; String sentence; sentence = greeting + " officer"; System.out.println(sentence);
A sequence of characters Double quote around the characters Concatenation using the + operator
String firstName = “Michele"; String lastName = "Van Dyne"; String fullName = firstName + " " + lastName; String favNumber = "42"; System.out.println(fullName + "'s favorite number is " + favNumber); Michele Van Dyne's favorite number is 42
29
Recall a data type is a set of values and the operations
For numeric data types, these are mathematical
For class data types, these operations are its methods
"Java" refers to a language. ?
System.out.println( "\"Java\" refers to a language.");
char singleQuote = '\'';
'
abc\def
new line
String s3 = "";
37
public static void main(String [] args)
identifier meaning value type args[0] 1st thing on command line after Java class name "bananas" String args[1] 2nd thing on command line "12" String args[2] 3rd thing on command line after Java class "0.21" String args.length # of things on command line 3 int % java CostCalc bananas 12 0.21 To buy 12 bananas you will need $2.52 Program input comes in as Strings from command line (for now)
A type cast temporarily changes the value of a variable
For example,
Illegal without (int)
The value of (int)distance is 9, The value of distance, both before and after the cast, is 9.0. Any nonzero value to the right of the decimal point is truncated
rather than rounded.
Helps protect you from mistakes (aka "bugs")
public class TypeExample0 { public static void main(String [] args) { int orderTotal = 0; double costItem = 29.95;
System.out.println("total=" + orderTotal); } } % javac TypeExample0.java TypeExample0.java:7: possible loss of precision found : double required: int
^
39
Manually → using a cast A cast is accomplished by putting a type inside ()'s Casting to int drops fractional part Does not round!
public class TypeExample1 { public static void main(String [] args) { int orderTotal = 0; double costItem = 29.95;
System.out.println("total=" + orderTotal); } } % java TypeExample1 total=31
40
Numeric types: If no loss of precision → automatic promotion
public class TypeExample2 { public static void main(String [] args) { double orderTotal = 0.0; int costItem = 30;
System.out.println("total=" + orderTotal); } } % java TypeExample2 total=31.8
41
String concatenation using the + operator converts numeric
types to also be a String
public class TypeExample3 { public static void main(String [] args) { double costItem = 29.95; String message = "The widget costs "; message = message + costItem; message = message + "!"; System.out.println(message); } } % java TypeExample3 The widget costs 29.95!
42
Things that take value(s) and return a result e.g. Math functions e.g. Type conversion: String → int
String → double
e.g. Random number generation
Live in some particular Java class e.g. Math, Integer or Double Call using class name followed by dot
43
44
method description Integer.parseInt(String a) converts text a into an int Double.parseDouble(String a) convert text a into a double
public class CostCalc { public static void main(String [] args) { String product = args[0]; int qty = Integer.parseInt(args[1]); double cost = Double.parseDouble(args[2]); double total = qty * cost; System.out.print("To buy " + qty); System.out.print(" " + product); System.out.println(" you will need $" + total); } }
% java CostCalc elections 2 1e6 To buy 2 elections you will need $2000000.0
Normal rules of mathematical precedence e.g. multiplication/division before addition/subtraction Use ()'s to force a different order of calculation
example result comment 10 + 7 * 2 24 multiplication comes before addition (10 + 7) * 2 34 ()'s force addition to occur first 10 / 7 + 2 3 integer division result is 1 which is added to 2 10 - 7 - 2 1 (10 - 7) - 2 1 10 - (7 - 2) 5
46
When binary operators have equal precedence, the operator
(including -, *, /, and %)
can be written as amount += 5;
49
true example false example == equal 7 == 7 7 == 8 != not equal 7 != 8 7 != 7 < less than 7 < 8 8 < 7 <= less than or equal 7 <= 7 8 <= 7 > greater than 8 > 7 7 > 8 >= greater than or equal 8 >= 2 8 >= 10 Is the sum of a, b and c equal to 0? (a + b + c) == 0 Is grade in the B range? (grade >= 80.0) && (grade < 90.0) Is sumItems an even number? (sumItems % 2) == 0
50
public class LeapYear { public static void main(String [] args) { int year = Integer.parseInt(args[0]); boolean isLeapYear; // Leap year if divisible by 4 but not by 100 isLeapYear = (year % 4 == 0) && (year % 100 != 0); // But also leap year if divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0); System.out.println(isLeapYear); } }
% java LeapYear 2000 true
int will convert to a double if need be double cannot automatically convert to int
51
expression resulting type resulting value (int) 3.14159 Math.round(3.6) 2 * 3.0 2 * (int) 3.0 (int) 2 * 3.0
Integer.parseInt() Double.parseDouble()
52
expression resulting type resulting value Integer.parseInt("30") Double.parseDouble("30") Integer.parseInt("30.1") Double.parseDouble("30.1") Integer.parseInt("$30") Double.parseDouble(3.14)
Strings never (automatically) go back to number
53
expression resulting type resulting value "testing " + 1 + 2 + 3 "3.1" + 4159 "2" + " + " + "3" 1 + 2 + 3 + "66"
Variables Primitive Data Types The String Class Type Conversion
Due next Thursday, 9/1, before midnight!
Already installed on lab machines Bring your own laptop if you want See resources page on web site for install tips
Assignment page is the contract Has helpful frequently asked questions
55