Assignments Lecture 5 Complete before Lab 4 Lab 3: Variables and operations Read Sections 2.9-2.13 Arithmetic Operations -Summary Computers excel at performing repeated arithmetic operations quickly and without error. There are 5 common operations: Addition: + Subtraction: - Multiplication: * Division: / Recall: A B = Quotient * B + Remainder If both operands are integer: produces the quotient Else same as A B Modulus: % (produces the remainder) Arithmetic Operations What rules are applied when an expression contains more than 1 operator? int x = 2 + 3 * 4-6 / 2 + 4 * 7 (5+2)? Java uses two rules: Operators are assigned a priority, highest priority operations are performed first Operations of the same priority are evaluated from left to right* 1
Combined Assignment Short Hand Reference Data Types The following are equivalent x += 4; x = x + 4; x -= 17; x = x 17; x *= 10; x = x * 10; x /= 5; x = x / 5; X %= 3; x = x % 3; A variable that is a reference data type stores a reference (or a pointer) to memory. The memory pointed to stores an object of the type specified. This is much different that what we have been discussing. A variable whose type is int, stores a number. In Java, we will create references to objects. Objects are defined by a class Objects are created Java String Class The String class is used to create and store strings of characters (or letters). Declaring a String reference: String city; The variables fullname and city are references to String objects. What is the value stored in fullname and city? Creating a String Object There are 2 different ways to create a String object. Use a string literal and perform an assignment (or combine literals) Use the Java keyword new city = new String( Pittsburgh ); Behind the scenes, how Java creates the String object is a little different in these two cases, but we are going to ignore that. For both: Java will create a String object in memory. The memory address for the object will then be assigned to the variable fullname will have one address and city will have a different address. 2
Using Strings To see the full capability of the Java String class, search for Java String and select the Oracle site Lets start with some common commands. What kinds of things might we want to do with a String? Print it Find out how many characters are in it Convert the case to all lower (or all upper) Find the character at a specific position whatever that means. Outputting a String To print Mary Fudrucker all we need to do is give the name of the String reference to print or println Java will do the rest. The statement System.out.println(fullName); produces: Mary Fudrucker The statement System.out.println( Clients full name is: + fullname); produces: Clients full name is: Mary Fudrucker Getting the number of characters To find out the number of characters are in a string, we ask the string. What do we ask? How do we ask? What: The String class defines a method that returns the number of characters. int length() We can output the number of characters int stringsize = fullname.length(); // call method length() System.out.println( The length of the string is: + stringsize); What is printed? 14. How: We have a reference to the String fullname We call the method length on the object (call using the dot) int stringsize = fullname.length(); 3
We can change case The Java String class allows you to get a reference to a new String object that is in all lower, or upper case: String tolowercase(); String touppercase(); Why might this be useful? Suppose you ask a user to enter yes or no in response to a question as part of a form. They may type: yes, Yes, YES, Yes, YEs, yes, yes, yes.if you convert their response to lower case its easier to compare! Get the name in all UPPER case String allupper = fullname.touppercase(); // calling method System.out.println( The original name is: + fullname); System.out.println( In all upper case is: + allupper); Suppose we wanted to change the name to all upper case, what modification would we make? Use the idea of combined assignment that we saw with algebraic operations fullname = fullname.touppercase(); Getting individual characters It can be useful to be able to get individual characters in a String Want to determine if a user responded yes, or yeah, or Yes, or. Want to determine if a particular character exists Test the word to see if it is a palindrome (reads forwards and backwards) Java String class provide a method to get a character at a specified position: char charat(int position); // position is 0-based More on charat What is the minimum position that we can use in charat? 0 What is the maximum position that we can use in charat? int stringsize = fullname.length(); Maximum position is stringsize 1 What happens if we put in a value that is too big? We get a run-time error. 4
Palindrome Example Pseudo Code: While we have characters left Read character on left end Read character on right end Compare. If different, NOT a palindrome. Done Else test next pair of characters Pseudo Code: int leftpos = 0; int rightpos = s.length() -1; While we have characters left char leftchar = s.charat(leftpos); char rightchar = s.charat(rightpos); Compare. If different NOT a palindrome. Done Else leftpos += 1; rightpos -= 1; Example: Print out Yes or No based on answer String answer; answer = Yep ; answer = answer.tolowercase(); char c = answer.charat(0); // get the first character If the user answered y System.out.println( The user answered Yes ); Otherwise System.out.println( The user answered No ); Recap. How does Java evaluate an expression such as the one below? int n = 2 / 5 * ( 4 + 2 * 2 ) 17 + 3 * 5; Variables are declared: type name; Assign values to variables using operator: = Variable type is: primitive or reference primitive types start with lower case letter - int, byte, short, long, double reference types start with Upper Case letter example: String Java Strings: Declaring, assigning a value, Learned how to call a method. Very Important to Understand. reading number of characters, changing case, getting individual characters a) Java evaluates () first, then *, / and % using a left to right rule, and finally + - using a left to right rule. b) Java evaluates the expression from left to right c) Java evaluates () first, then all *, then all /, then all %, then all +, then all - d) Java cannot evaluate expressions like the one above. Bonus: +1 pt if you provide the correct value of n -2 A lot of people computed n improperly. 2/5*8 evaluate 2/5 first. This is integer division. The quotient of 2/5 is 0. So 2/5*8 becomes 0 * 8 which is 0. The result is n is -1 5
Java supports 2 general data types: primitives and references. Java String objects store sequences of characters. To declare the variable str as String reference, we write: String str; Circle the statements below that will result in the variable str referencing a Java String object that stores Java is fun!. a) str = Java is fun! ; b) str = new String( Java is fun! ); c) str = Java is fun! d) str = String( Java is fun! ); 6