CP122 Computer Science I Chapter 2: Data Types, Variables, and I/O
Course blogger?
QRC Tutoring
Tech News! President Obama proposes overhaul of NSA data collection
Tech News! President Obama proposes overhaul of NSA data collection Google Glass partners with Ray-Ban
Hacker's Tip of the Day: Use Linux Open Source Software
Hacker's Tip of the Day: Use Linux Open Source Software Free
Hacker's Tip of the Day: Use Linux Open Source Software Free *cough* Better *cough*
Using IDEs Eclipse JGrasp BlueJ Netbeans Dr. Java
Chapter 2: Fundamental Data Types Variables and data types Arithmetic Binary data Input/Output Strings
Data Types Every value in a Java program has a type
Data Types Every value in a Java program has a type That is, what kind of data does the value store?
Data Types Every value in a Java program has a type That is, what kind of data does the value store? Binary data: all 1's and 0's
Data Types Every value in a Java program has a type That is, what kind of data does the value store? Binary data: all 1's and 0's How are the 1's and 0's interpreted?
A Familiar Type: A Literal String public class MyFirstProgram { public static void main(string[] args) { System.out.println( This is my first program! ); } }
Other Data Types int (integer) - Any whole number Examples: 15, -50, 0
Other Data Types int (integer) - Any whole number Examples: 15, -50, 0 double (floating-point number) A number with the decimal part Examples: 3.1415, 0.25, 1E6
Expressions Expression : a combination of variables, literals, and operators Examples: 10 + 25 100 * 6-2 100 * (6-2)
Microquiz What are the data types of the following values? 87 12.5 I heart CS
Variables Variable: a named and typed place to store a value int userage = 19;
Variables Variable: a named and typed place to store a value int userage = 19; int is the type of the new variable.
Variables Variable: a named and typed place to store a value int userage = 19; userage is the name of the variable.
Variables Variable: a named and typed place to store a value int userage = 19; 19 is the initial value for the variable.
Variables Variable: a named and typed place to store a value int userage = 19; = is the assignment operator.
Variables Variable: a named and typed place to store a value int userage = 19; double hourlypay = 31.50;
Variables Variable: a named and typed place to store a value int userage = 19; double hourlypay = 31.50; String avatarname = n00bdestroyer ;
Identifier Name Rules - Consist of letters, digits, underscores, and dollar signs (do not use dollar signs) - Cannot use spaces or other symbols - Java keywords are illegal (public, static, void)
Identifier Name Style Rules - Start variable names with a lowercase letter - Capitalize subsequent words in the variable (camelcase) userage hourpay - Start class names with an uppercase letter - Use UpperCamelCase
Identifier Name Style Rules - Choose descriptive variable names! double x3 = mm2 / i6;
Identifier Name Style Rules - Choose descriptive variable names! double x3 = mm2 / i6; double hourlypay = salary / hoursworked;
Assignment Operator int userage = 19;... userage = 20; userage = 19 + 1; userage = userdays / 365;
Other Operators int userage = 19; // Increment userage (add 1 to it) userage = userage + 1;
Other Operators int userage = 19; // Increment userage (add 1 to it) userage = userage + 1; // Increment userage (add 1 to it) userage += 1;
Other Operators int userage = 19; // Increment userage (add 1 to it) userage = userage + 1; // Increment userage (add 1 to it) userage += 1; // Increment userage (add 1 to it) userage++;
Calculator.java
Storing Numbers How many distinct values can you represent given one binary bit?
Storing Numbers How many distinct values can you represent given one binary bit? 8 bits together are called a byte. How many values can be represented with a byte?
Challenge Question What is the largest value that we can store in an int variable? What does this tell us about the number of bits used to store each int variable?
Table of Sizes Bit Value 0 or 1 Byte 8 bits Kilobyte 1024 bytes Megabyte 1024 kilobytes Gigabyte 1024 megabytes Terabyte 1024 gigabytes Petabyte 1024 terabytes Exabyte 1024 petabytes
Java Primitive Data Types Integer Types byte short int long : : : : 1 2 4 8 Floating-Point Types byte bytes bytes bytes float : 4 bytes double : 8 bytes Other Types char : 2 bytes boolean : 1 bit
Binary Numbers How do we represent the decimal number 13 in binary?
Binary Numbers What decimal number does the binary number 10110 represent?
Binary Data Microquiz Convert to binary: 15 21 5 Convert to decimal: 1001 110110 101010
Crazy Microquiz Convert from decimal to base 7. 44 12 Convert from decimal to hexadecimal (base 16) 11 33
There are only 10 types of people in the world: those who understand binary and those who don't.
There are only 10 types of people in the world: those who understand ternary, those who don't, and those who thought this was going to be a binary joke.
Did you know that Halloween is the same day as Christmas? =
Did you know that Halloween is the same day as Christmas? = 25 DEC = 31 OCT
String Variables // Declare a String variable and initialize it String username = mwhitehead ; System.out.println(userName);
String Methods // Convert a String into an integer String useragestring = 23 ; int userage = Integer.parseInt(userAgeString);
More String Methods // Convert a String into an integer String useragestring = 23 ; int userage = Integer.parseInt(userAgeString); // Convert a String into a double String gpastring = 3.42 ; double gpa = Double.parseDouble(gpaString);
More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0);
More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0); // Extract the first nine characters from a String String fullname = Stephanie Brown ; String firstname = fullname.substring(0, 9);
More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0); Inclusive start index // Extract the first nine characters from a String String fullname = Stephanie Brown ; String firstname = fullname.substring(0, 9);
More String Methods // Extract a single character from a String String fullname = Stephanie Brown ; char firstletter = fullname.charat(0); Exclusive end index // Extract the first nine characters from a String String fullname = Stephanie Brown ; String firstname = fullname.substring(0, 9);
More String Methods // Concatenate two strings into one String firstname = Stephanie ; String lastname = Brown ; String fullname = firstname + lastname;
Escape Sequences // Insert a newline character System.out.println( Hello \n World );
Escape Sequences // Insert a newline character System.out.println( Hello \n World ); // Insert a literal backslash (\) System.out.println( Saved in C:\\Documents );
Getting User Input import java.util.scanner;... // Create a Scanner object Scanner myscanner = new Scanner(System.in); // Read in an integer that the user types in int userint = myscanner.nextint();
Getting User Input import java.util.scanner;... // Create a Scanner object Scanner myscanner = new Scanner(System.in); // Read in an integer that the user types in int userint = myscanner.nextint(); // Read in a whole line as a String String line = myscanner.nextline();
User Input : Dialog Box import javax.swing.joptionpane;... // Create a pop-up window String prompt = Please enter your username ; String username = JOptionPane.showInputDialog(prompt);
User Input : Dialog Box import javax.swing.joptionpane;... // Create a pop-up window String prompt = Please enter your username ; String username = JOptionPane.showInputDialog(prompt); String prompt2 = Please enter your password ; String password = JOptionPane.showInputDialog(prompt2);
User Input : Dialog Box import javax.swing.joptionpane;... // Create a pop-up window String prompt = Please enter your username ; String username = JOptionPane.showInputDialog(prompt); String prompt2 = Please enter your password ; String password = JOptionPane.showInputDialog(prompt2); JOptionPane.showMessageDialog(null, Logged in! );
BetterCalculator.java
Break!