200 likes | 383 Views
char N int Bitwise operators Stings homework. Agenda. Is this legal?. int aInt = 10; char aChar = ‘a’; aInt = aChar; => ok? yes aInt = ‘a’; => ok? yes aInt = 10 + aChar; => ok? yes int aInt = 10;
E N D
char N int • Bitwise operators • Stings • homework Agenda
Is this legal? int aInt = 10; char aChar = ‘a’; aInt = aChar; => ok? yes aInt = ‘a’; => ok? yes aInt = 10 + aChar; => ok? yes int aInt = 10; char aChar = aInt; => ok? no aChar=(char)aInt; char aChar = 10; => ok? No Conclusion: assign a char or char literal to an int variable is ok, but not the other way around
char N String char aChar = ‘A’ ; String aStr = “B” ; aChar = aStr; => ok? aStr = aChar; => ok? aStr = aChar + “ “ ; => ok • Conversion from char to string. • Conversion from string to char aChar = aStr.CharAt(0); aChar = aStr.CharAt(1); => ok?
Bitwise operators • Applied to binary only • Boolean operators- && || • Bitwise operators- & | • Bitwise AND - & 0 1 1 0 0 1 0 0 1
Bitwise OR : | 0 1 1 0 0 1 0 1 1 • Bitwise exclusive OR: (XOR) ^ 0 1 1 0 0 1 0 1 0 • Short circuit evaluation: the left operand is evaluated first. If the result of the entire expression can be determined by the value of the left operand, then no other operands will be evaluated
Bitwise NOT- ~ 0 => 1 1 => 0 • AND (9010 & 10710 ) 9010 0 1 0 1 1 0 1 0 10710 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1 0 • OR (9010 | 10710 ) 9010 0 1 0 1 1 0 1 0 10710 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 1
XOR ^ (9010 ^ 10710 ) 9010 0 1 0 1 1 0 1 0 10710 0 1 1 0 1 0 1 1 0 0 1 1 0 0 0 1 • NOT ~ • 00000000 00000000 00000000 00101110 11111111 11111111 11111111 11010001 most significant bit(msb) 0 – positive 1 - negative
Bitwise operations • Decimal shifting to the right 283.0 -> 28.3 dividing by 10 • Decimal shifting to the left 283.0 -> 2830 multiplying by 10 • Binary shifting to the right: >> 32 >> 3 shift the bits(representing number 7) to the right 3 times 2*2*2=8 32/8 = 4 • Binary shifting to the left: << 7 << 2 shift the bits to the left 2 times
A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition
Strings • Strings are always a big problem for programmers, because to manipulate Strings can be very inefficient for the computer with regards to memory usage and processing time. • Hence the String class which handles all issues that can occurs. • Class String (java.lang.String)
Sting declaration: String alpha = new String("abc"); => ok? String alpha = "abc"; => ok? • String are immutable, can’t be changed. value of a String Object once created cannot be modified. Any modification on a String object creates a new String object. • Until a String object is assigned a value, it refers to null. Calling a method from a null String object generates the exception NullPointerException. String s1 = null; //s1 is a null reference String s2 = new String(); //s2 is an empty character sequence
Concatenation operator • Int five = 5; • String state = “Hawaii-”; • String tvShow = state + five + “-0”; What is the value of tvShow? Date d1 = new Date(8, 2, 1947); Date d2 = new Date(2, 17, 1948); String s = “My birthday is “ + d2; => s? String s2 = d1 + d2; => ok? String s3 = d1.toString() + d2.toString(); => ok?
String methods • length() • substring(int start, int end) returns a substring of the string, which starts at start position and ends one character before the end position. • substring(int start) returns a substring of the string, which starts at start position and extends to the end of the string. • toLowerCase() • toUpperCase()
trim() returns a copy of the string with all leading and trailing spaces removed. • replaceFirst(String str, String str2) returns a string with the first occurrence of str replaced by str2. • replaceAll(String str, String str2) returns a string with all occurrences of str replaced by str2. • indexOf(String str): “ABC”.indexOf(“B”) returns the integer corresponding to the location of the first occurrence of str in the string. Otherwise –1 is returned
String text; text = “HELlO"; text = text.toLowerCase(); System.out.println(text); What do we call this? Pass by… HELLO text hello
public static void main(String[] args) { String aStr="ABC"; changeStr(aStr); System.out.println(aStr); => ? char[] charArry={'A','B','C'}; for(char ele:charArry)System.out.print(ele); System.out.println((char)32); changeStr(charArry); for(char ele:charArry)System.out.print(ele); changeStr(charArry[0], charArry[1],charArry[2]); }
public static void changeStr(String bStr) { bStr="CDE"; } public static void changeStr(char[] bStr) { bStr[0]='D'; bStr[1]='E'; bStr[2]='F'; } public static void changeStr(char a, char b, char c) { a=‘X'; b=‘Y'; c=‘Z'; }
Word guessing game WordGuess is played between the computer and a single player. The secret word is BRAIN. At the start of the game, six dashes are displayed (––––––), one for each letter of the word. The player is repeatedly prompted for a letter guess. When a letter matching one in the word is guessed, the letter replaces the corresponding dash. Letters may be entered as uppercase or lowercase. However, only uppercase letters should be displayed. If the player enters an exclamation point (!), it is game over. Alternatively, the player can continue to guess letters until the entire word is revealed. The games ends when player enters all the letters or palyer enters !
The WordGuess algorithm 1. Display a row of dashes to represent the word. 2. Prompt the user for a letter guess. 3. If the letter guessed is part of the word, then display that letter in place of the corresponding dash. 4. Repeat steps 2 and 3 until all the letters have been guessed or an exclamation point has been entered by the user. 5. If an exclamation point has been entered, display “Game over” 6. If the player correctly guesses the entire word or all the letters have been guessed, then display “Game over”
Homework • Read Barran’s chapter 1 and bring the textbook to class. • Finish the whole project “Word Guess” and email me the source code