CP122 Computer Science I Binary, Strings, Conditionals, User Input
Everysight Raptor AR cycling glasses ($499) Tech News!
Tech News! Everysight Raptor AR cycling glasses ($499) 42% of US children ages 0-8 have their own tablet
Update Zombie.java
Topics What are binary numbers? How do we get input from the user? What operations can we perform on/with Strings? How can our programs make decisions?
Storing Numbers How many distinct values can you create given one binary bit?
Storing Numbers How many distinct values can you create given one binary bit? 8 bits together are called a byte. How many values can be created 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 : 1 byte short : 2 bytes int : 4 bytes long : 8 bytes Floating-Point Types 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
String Object String welcome = Hi! Thanks for visiting! ;
String Object String welcome = Hi! Thanks for visiting! ; int stringsize = welcome.length();
String Object String welcome = Hi! Thanks for visiting! ; int stringsize = welcome.length(); int firsta = welcome.indexof('a');
More 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);
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 // 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 // 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();
Getting User Input // 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(); // Read in the next token as a String String token = myscanner.next();
User Input : Dialog Box // Create a pop-up window String prompt = Please enter your username ; String username = JOptionPane.showInputDialog(prompt);
User Input : Dialog Box // 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 // 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! );
FancyCalculator.java
Constants public class BasketballScoreboard { public static void main(string[] args) { // Team 1 makes a 3-pointer scoreboard.updateteam1score(3);
Constants public class BasketballScoreboard { public static final int THREE_POINTER = 3; public static final int FIELD_GOAL = 2; public static final int FOUL_SHOT = 1; public static void main(string[] args) { // Team 1 makes a 3-pointer scoreboard.updateteam1score(basketballscoreboard.three_pointer); // Team 2 makes a foul shot scoreboard.updateteam2score(basketballscoreboard.foul_shot);
Instance Variables vs. Local Variables Instance variables are declared inside a class, but outside of any specific method They can be used anywhere within the class That is, their scope is class-wide
Instance Variables vs. Local Variables Local variables are declared within a specific method They can be used only within that method That is, their scope is only method-wide
Instance Variables vs. Local Variables Why would we ever want to use local variables? Aren't instance variables more powerful?
Program Control Flow public class MyProgram { public static void main(string[] args) { // Statement 1 // Statement 2 // Statement 3 // Statement 4
Program Control Flow public class MyProgram { public static void main(string[] args) { // Statement 1 // Statement 2 // Statement 3 // Statement 4
How can we make our programs do different things?
Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( What is your IQ? ); int iq = Integer.parseInt(iqString); if (iq > 150) { System.out.println( You're a genius! );
Make Decisions A boolean condition: public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( evaluates to true or What is your IQ? ); false int iq = Integer.parseInt(iqString); if (iq > 150) { System.out.println( You're a genius! );
Make Decisions The body of the if- public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( statement is only executed What is if your the IQ? ); int iq = Integer.parseInt(iqString); condition is true. if (iq > 150) { System.out.println( You're a genius! );
Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( What is your IQ? ); int iq = Integer.parseInt(iqString); if (iq > 150) { System.out.println( You're a genius! ); else { System.out.println( Sorry! You're not a genius );
Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( What is your IQ? ); int iq = Integer.parseInt(iqString); The body of the elseclause is only executed if (iq > 150) { if the condition is false. System.out.println( You're a genius! ); else { System.out.println( Sorry! You're not a genius );
Make Decisions public static void main(string[] args) { String iqstring = JOptionPane.showInputDialog( Notice: else is not followed by a What condition. is your IQ? ); int iq = Integer.parseInt(iqString); It always executes if the if condition is false. if (iq > 150) { System.out.println( You're a genius! ); else { System.out.println( Sorry! You're not a genius );
Make Multiple Decisions if (iq > 150) { System.out.println( You're a genius! ); else if (iq > 125) { System.out.println( You're pretty smart. ); else if (iq > 100) { System.out.println( You're kinda smart. ); else { System.out.println( I've got some bad news... );
Make Multiple Decisions if (iq > 150) { System.out.println( You're a genius! ); else if (iq > 125) { System.out.println( You're pretty smart. ); else if (iq > 100) { System.out.println( You're kinda smart. ); else { System.out.println( I've got some bad news... ); Each else-if condition is only checked if the above conditions are all false.
Make Multiple Decisions if (iq > 150) { System.out.println( You're a genius! ); else if (iq > 125) { System.out.println( You're The else pretty block smart. ); is only executed if all the above else if (iq > 100) { conditions are false. System.out.println( You're kinda smart. ); else { System.out.println( I've got some bad news... );
Comparison Operators == : equal to < : less than > : greater than <= : less then or equal to >= : greater than or equal to!= : not equal to
Sequential ifs x = 5; y = 17; if (x < 10) { System.out.println( Small x ); if (y > 25) { System.out.println( Big y );
Sequential ifs x = 5; y = 17; if (x < 10) { System.out.println( Small x ); if (y > 25) { System.out.println( Big y ); else { System.out.println( Small y );
String/Object Comparison Operator String name1 = Terry ; String name2 = Tracy ; // DOESN'T WORK LIKE YOU THINK! if (name1 == name2) { System.out.println( A MATCH! );
String/Object Comparison Operator String name1 = Terry ; String name2 = Tracy ; // DOESN'T WORK LIKE YOU THINK! if (name1 == name2) { System.out.println( A MATCH! ); // WORKS! if (name1.equals(name2)) { System.out.println( A MATCH! );
Nested if-statements if (numberhomeruns > 30) { if (numberstrikeouts < 100) { System.out.println( You're a great hitter! ); else { System.out.println( You have power, ); System.out.println( but you miss too often );
boolean Variables boolean mybool = (x < y); if (mybool) { System.out.println( x is smaller than y );
Compound Boolean Conditions if (iq < 150 && iq > 125) { System.out.println( You're pretty smart. );
Compound Boolean Conditions if (iq < 150 && iq > 125) { System.out.println( You're pretty smart. ); Boolean AND
Compound Boolean Conditions if (iq > 150 iq < 50) { System.out.println( You're at one of the extremes ); Boolean OR
Truth Tables for AND and OR
Compound Boolean Conditions if (125 < iq < 150) { System.out.println( You're pretty smart. ); This doesn't work!
Comparing Zombies