CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Logic & program control part 2: Simple selection structures.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
CS0007: Introduction to Computer Programming
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 13 / 2006 Instructor: Michael Eckmann.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Expressions, Data Conversion, and Input
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 08 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.
Multiple variables can be created in one declaration
Primitive Types and Expressions
Presentation transcript:

CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? wrapper classes comment method terminology comparing data –using the relational operators for primitive types –using methods for class types Scanner class for getting user input more methods in String class logical operators

“Wrapper classes” Double is not the same as double. double is a primitive type, Double (with a capital D) is a class. There are what are called “wrapper” classes for all the primitive types. They are: Integer int Double double Character char Float float Boolean boolean Byte byte Long long Short short Michael Eckmann - Skidmore College - CS Spring 2008

using wrapper classes for conversion We used a method in a few of the wrapper classes to convert a String into the wrapper class' associated primitive type. How did we do that? Does anyone remember? Michael Eckmann - Skidmore College - CS Spring 2008

using wrapper classes for conversion We used a method in a few of the wrapper classes to convert a String into the wrapper class' associated primitive type. How did we do that? Does anyone remember? int homers_int; String homers_str = “49”; homers_int = Integer.parseInt(homers_str); int weight; String weight_str = “182.5”; weight = Double.parseDouble(weight_str); Michael Eckmann - Skidmore College - CS Spring 2008

method terminology void – another “type” in Java which is used when no value is needed. Michael Eckmann - Skidmore College - CS Spring 2008

method terminology Parameter – methods have 0 or more parameters. These specify what types of values are used when a method call is made. Calling a method – invoking a method by giving its name in a statement in your program: –e.g. –System.out.println(“Hey”); // method call for println method –height = Integer.parseInt(height_str); // method call for parseInt method Note: A String is being passed in as a parameter for println. Same for parseInt. Michael Eckmann - Skidmore College - CS Spring 2008

method terminology Return type of a method – This is what type the result of a method call gives. e.g. –System.out.println(“Hey”); // nothing is returned (void)‏ –height = Integer.parseInt(height_str); // an int is returned The return type and number of parameters and types are all specified in the definition of a method. For the Java API methods, we can look this stuff up online. Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Comparing primitive type variable values is different than comparing variables of non-primitive types. Recall the primitive types: char, byte, short, int, long, float, double, boolean. String, Integer, Double, and many others which we have yet to see are classes which can be types for variables. These kinds of variables are not of the primitive types. These kids of variables are also called objects or object references. Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Example chars char some_char = 'a', first_letter = 'E', a_digit = '7'; // note: a char literal is always in single quotes. Examples of how to compare chars for equality (some_char == 'a')‏ (some_char == 'b')‏ (some_char == a_digit)‏ Also could compare less than, greater than, etc. for chars What would that mean? Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Example ints (same for byte, long, short)‏ int height = 65, length = 4, width = 6; Examples of how to compare ints (height < 70)‏ (width == length)‏ Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Example booleans boolean done_yet = false, old_enough = true; Examples of how to compare booleans (done_yet == true)‏ (done_yet)‏ (old_enough)‏ (!old_enough)‏ (done_yet == old_enough)‏ Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Example doubles (same for floats)‏ double weight = 5.6; // note: a decimal number literal is assumed by Java to be // a double (not a float) 5.6f is how to create a float literal Examples of how to compare doubles (weight > 5.1)‏ (weight == 5.6) // problem because doubles (and floats)‏ // are not stored exactly (depending on the value)‏ // so it is dangerous to check for equality – don't do it. Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Example Strings String name = “Mike”; // note: a String literal is enclosed in double quotes Example of how not to compare Strings (name == “Mike”) /* sometimes works sometimes doesn't because since name is an object (not a variable of a primitive type), when compared using the comparison operators, Java compares the memory locations of name and “Mike”, not the values. */ Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Example Strings String name = “Mike”; Example of how to compare Strings (use method(s) in the String class for this purpose) (name.equals(“Mike”)) // or (name.equalsIgnoreCase(“mike”))‏ Here we are calling methods in the String class. If you had to guess, what is the type of the value that is returned by these methods? Michael Eckmann - Skidmore College - CS Spring 2008

Comparing data Another way to compare Strings is to use the compareTo method of the String class. The calling String (the one to the left of the. ) is compared lexicographically to the String that is passed in as a parameter. If the calling String is less than the parameter String the result is a negative number. If they are equal, the result is 0. Otherwise the result is a positive number. What is the type of the value that is returned by this method? Michael Eckmann - Skidmore College - CS Spring 2008

java.util.Scanner Scanner input_scan = new Scanner(System.in); methods that you can call on your scanner object include: –nextInt() --- reads an int from keyboard (store what's returned into an int)‏ –nextDouble() --- reads a double from keyboard –nextLine() --- reads a line (as a String) from keyboard –next() --- reads a “word” from the keyboard --- which is a string of nonwhitespace chars delimited by whitespace. whitespace is \n, blank space character, \t, \r Michael Eckmann - Skidmore College - CS Spring 2008

java.util.Scanner nextLine() --- reads a line (as a String) from keyboard –this method “consumes” the \n but does not make it part of the String that is returned. String s1, s2; Scanner my_scan = new Scanner(System.in); s1 = my_scan.nextLine(); s2 = my_scan.nextLine(); –if input is: CS106 Introduction to Computer Science I –there is a \n after CS106 in the input (i.e. user hit enter key), but s1 will be “CS106” and s2 will be “Introduction to Computer Science I” neither will have \n as a character in its String. Michael Eckmann - Skidmore College - CS Spring 2008

other String class methods boolean equals(String s)‏ boolean equalsIgnoreCase( String s)‏ char charAt(int index)‏ int length()‏ String toUpperCase()‏ String toLowerCase()‏ All of these methods are called by using a String object followed by the. (dot operator) then the name of the method (and any necessary parameters within the parens). Michael Eckmann - Skidmore College - CS Spring 2008

other String class methods Examples (let's put some of this code in a program): –String name = “Joe”, name2, lastname; –char a_char; –if (name.length() > 5)‏ –a_char = name.charAt(2); –name2 = name.toUpperCase(); if (lastname.compareTo(“Jones”) < 0)‏ System.out.println(lastname + “ comes before Jones”); else if (lastname.compareTo(“Jones”) > 0)‏ System.out.println(“Jones comes before ” + lastname); else System.out.println(“Jones is the same as ” + lastname); Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators Not ! And & or && Or | or || The double ones (&& and ||) are “short circuit” operators --- if left operand is sufficient to determine truth or falsity, then the right operand is not evaluated. The single ones (& and |) always evaluate both operands. This really only matters if you have side-effects, e.g. Like a ++ or -- Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators Precedence first !, then && then ||. Parentheses are still performed first. Should be used to force the order that the programmer desires. Not operator, !, takes one operand to its right. And operator, &&, works on two operands Or operator, ||, works on two operands Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators NOT: !true = false, !false = true AND: true && true = true true && false = false false && true = false false && false = false Michael Eckmann - Skidmore College - CS Spring 2008 OR: true || true = true true || false = true false || true = true false || false = false

Logical operators Examples: boolean end_of_file = false; //... if (! end_of_file)‏ //... if (!(age >=21))‏ //... Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators if (!(age >=21))‏ //... // how else might we write the above? if ((age = 65))‏ // get a discount at the movies... if (you_are_rich && you_are_good_looking)‏ System.out.println(“You got it made.”); // what types must you_are_rich and you_are_good_looking, be? Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators if ((age >=21) && (++count > 10))‏ //... here if age is not >= 21 then regardless of what's on the right hand side of the &&, the whole thing will be false (because false AND anything = false), because && is the “short circuit” format of the AND operator, (++count > 10) will not be evaluated. if ((age >=21) & (++count > 10))‏ //... here, even if age is not >= 21 the right hand side WILL be evaluated What's the difference in the result? Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators && (short circuit AND) vs. & (non-short circuit AND)‏ if ((age >=21) && (++count > 10))‏ //... if ((age >=21) & (++count > 10))‏ //... The difference is, that 1 will be added to count (and compared to 10) regardless of whether or not age is >= 21 in the second if, but in the first if, 1 will be added to count (and compared to 10) only if age is indeed >=21. Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators if ((height >= 72) || (countPeople() <= 100))‏ //... here if height is >= 72, then the whole condition is guaranteed to be true (because true OR anything = true) and the || causes the right hand side NOT to be evaluated. Therefore, the method countPeople() would not be called if ((height >= 72) | (countPeople() <= 100))‏ //... here even if height is >=72, the righthand side WILL be evaluated because of the use of the non-short circuit OR |. Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators || (short circuit OR) vs. | (non-short circuit OR)‏ if ((height >= 72) || (countPeople() <= 100))‏ //... if ((height >= 72) | (countPeople() <= 100))‏ //... The difference is, that countPeople() method will ALWAYS be called in the second if but won't be called in the first if if the left hand side evaluates to true Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators if ((height >= 72) || (countPeople() <= 100))‏ //... Would countPeople() be called (and it's returned value compared to 100) if height happens to be < 72? Why or why not? Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators if ((height >= 72) || (countPeople() <= 100))‏ //... Would countPeople() be called (and it's returned value compared to 100) if height happens to be < 72? Why or why not? Yes it would because the LHS being false doesn't determine the outcome of the whole condition, even though we are using the || short-circuit version of OR. Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators if ((age >=21) && (++count > 10))‏ //... Would ++count > 10 occur if age is >= 21? Why or why not? Michael Eckmann - Skidmore College - CS Spring 2008

Logical operators if ((age >=21) && (++count > 10))‏ //... Would ++count > 10 occur if age is >= 21? Why or why not? Yes it would because the LHS being true doesn't determine the outcome of the whole condition, even though we are using the && short-circuit version of AND. Michael Eckmann - Skidmore College - CS Spring 2008