1 / 29

CS 177 Week 3 Recitation Slides

CS 177 Week 3 Recitation Slides. Basic Math Operations, Booleans, and Character Operations. Announcements. Project 1 is posted this morning. It is due on Sep. 24th 9pm TAs will start consulting hours next week Mon-Thu 6:00pm to 9:00 pm Mentor Program available

opal
Download Presentation

CS 177 Week 3 Recitation Slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations

  2. Announcements Project 1 is posted this morning. It is due on Sep. 24th 9pm TAs will start consulting hours next week Mon-Thu 6:00pm to 9:00 pm Mentor Program available Wed 5:30pm to 7:30pm in LWSN B158 Bonus points 5% extra for total score

  3. QUESTIONS???

  4. Basic Operations Recap

  5. Operations On Integers • b -= 3;  b = b - 3; • d /= 2;  b = b / 2; • b --;  b = b - 1; • +, -, *, / • Addition, subtraction, multiplication and division • Note that division of integers drops the fractional part • +=, -=, *=, /= • a += 4;  a = a + 4; • c *= 5;  c = c * 5; • ++, -- • a ++;  a = a + 1; • % • Remainder of division • a = 10 % 3; // a contains 1

  6. Question • What are the ways we can write to increment variable a by 1? • a = a + 1; • a += 1; • a ++;

  7. Basic Operations w/IntegersCode Example 1 • Output a = 33 b = 3 a = 26 b = 9 • public class IntegerOperations { • public static void main(String[] args) { • int a; • int b; • a = 13 + 20; // a contains 33 • b = a / 11; // b contains 3 • System.out.println("a = " + a); • System.out.println("b = " + b); • a -= 7; • System.out.println("a = " + a); • b = a / b; // Result = 8.66 but int type means 8 • b++; // Increment b from 8 to 9 • System.out.println("b = " + b); • } • }

  8. Basic Operations On Doubles • Same operations as integers • But the fractional part is retained

  9. Basic Operations w/DoublesCode Example 1 • public class DoubleOperations • { • public static void main(String[] args) • { • double a; • double b; • double c; • double d; • a = 4.0 / 3.0; • b = a - 1; • c = b + b + b * 2; • d = 9 / 4; • d--; • System.out.println("a = " + a); • System.out.println("b = " + b); • System.out.println("c = " + c); • System.out.println("d = " + d); • } • } • Output a = 1.3333333333333333 b = 0.3333333333333333 c = 1.3333333333333333 d = 1.0 • Question Variable d is a double or an integer?

  10. Casting in Java • int a = (int) 1.8; // convert double 1.8 into an int 1 • Convert one data type to another • loss of precision • float  int, number is rounded down • Let the Java compiler know that you are aware that you are going to lose some information, but you are ok with that.

  11. Math Library in Java • Java contains a Math class that includes a variety of math functions that can be used to perform other operations on integers and/or doubles • The term “library” just means that it is a collection of predefined methods/functions that a programmer can leverage • Brief example: double x = 16.0; double y; y = Math.sqrt(x); // y will be 4.0 after this line executes

  12. Math Library in Java

  13. Math Operations Code Example • Output a is 5.0 b is 16.0 x is 4.0 y is 7.0 z is 0.8775825618903728 • Note Math.round(y); (int) (y + 0.5); These are the same. • public class MathOperations { • public static void main(String[] args) { • double a = 25; • double b = 4; • double x = 2.0; • double y = 6.731; • double z = 0.5; • a = Math.sqrt(a); • b = Math.pow(b, 2); • x = Math.pow(x, 5) / 8; • y = Math.round(y); • z = Math.cos(z); • System.out.println("a is " + a); • System.out.println("b is " + b); • System.out.println("x is " + x); • System.out.println("y is " + y); • System.out.println("z is " + z); • } • }

  14. Boolean Operations

  15. Boolean Operations Code Example • Output NOT true is false NOT false is true true AND false is false true OR false is true true XOR false is true true AND true is true true XOR true is false • public class BooleanOperations { • public static void main(String[] args) { • boolean test1 = true; • boolean test2 = false; • boolean test3 = test1 && test2; • boolean test4 = test1 || test2; • boolean test5 = test1 ^ test2; • boolean test6 = test1 && test1; • boolean test7 = test1 ^ test1; • System.out.println("NOT " + test1 + " is " + !test1); • System.out.println("NOT " + test2 + " is " + !test2); • System.out.println(test1 + " AND " + test2 + " is " + test3); • System.out.println(test1 + " OR " + test2 + " is " + test4); • System.out.println(test1 + " XOR " + test2 + " is " + test5); • System.out.println(test1 + " AND " + test1 + " is " + test6); • System.out.println(test1 + " XOR " + test1 + " is " + test7); } • }

  16. Characters in Java number char • A digit, a letter or a symbol • Denoted by single quotes in Java • ‘T’, ‘@’, ‘2’ • Represented by numbers in computer • Char ch = ‘T’; // ch contains integer 84// ch contains character ‘T’ • Escape sequences \ • single quote ‘\’’ • New line ‘\n’ • Tab ‘\t’ • Back slash ‘\\’

  17. Character Operations • Characters are actually integers • +, -, ++, --, +=, -= Operations • ASCII number change. Become another character. • char ch = ‘a’ + 2; // ch contains number 99 representing ‘c’ • ch += 3; // ch contains number 102 representing ‘f’ • int dis = ‘g’ – ‘c’; // dis contains 4, no cast required

  18. Character OperationsCode Example • public class CharacterOperations { • public static void main(String[] args) { • char letter; • int number; • letter = ‘l'; // letter contains ‘l' • letter++; // letter contains ‘m' • System.out.println("letter = '" + letter + "'"); • number = letter; // no cast required • System.out.println("number = " + number); • letter = ‘h'; • number = letter - 'a' + 1; • System.out.println("'" + letter + "' is the " + number + "th letter of the alphabet"); • } • } • Output letter = 'm' number = 109 'h' is the 8th letter of the alphabet

  19. Strings in Java • A list of characters • Denoted by double quotes • String str = “Computer”; • +, += operators : concatenation • String str1 = “Java is”; • String str2 = str1 + “ programming language.”; • str1 += “ a cup of coffee.”; • Can concatenate variables of other types as text strings • int a = 1; int b = 3; int c = a + b;String str3 = a + “ + “ + b + “ = “ + c;

  20. String Class • The String class contains several methods that can be used to perform operations on a string.

  21. Methods in String Class • String str = “Boiler Up!”; • Comparison • int a = str.compareTo(“Purdue”); // a contains a negative number • boolean b = str.equals(“Purdue”); // b contains false • length() gives number of characters in the string • int l = str.length(); // l contains 10

  22. Methods in String Class • String str = “Boiler Up!”; • charAt(int i) get the character at position i • char c = charAt(2); // index starts from 0, c contains ‘i’, not ‘o‘ • substring(int i, int j) gives the substring from position i and to the position before j • String str2 = str.substring(0, 6); // str2 contains “Boiler” • String str3 = str.substring(7, str.length()); // str3 contains “Up!”

  23. Strings Class Code Example • public class StringOperations { • public static void main(String[] args) { • String word1; • String word2; • word1 = “chrome"; • word2 = “home"; • System.out.println(“Does " + word1 + " = " + word2 + "?"); • System.out.println( word1.equals(word2) + "!"); • System.out.println("The length of " + word2 + " is: " + word2.length() ); • int position = 3; • System.out.println("Position " + position + " in " + word2 + " is '" + • word2.charAt(position) + "'"); • word1 = "computers are cool"; • word2 = word1.substring(3, 8); • System.out.println(word2); • } • } • Output Is chrome the same as home? false! The length of home is: 4 Position 3 in home is 'e' puter

  24. Question • If we have String word1 = “Purdue”; String word2 = “Purdue”; • This statement will return true or false? word1.equals(word2); • What about this one? word1 == word2;

  25. Wrapper Classes • Each primitive data type in Java has a wrapper class • Integer converts between int  String • String  int • String str = “345”; • int a = Integer.parseInt(str); • int  String • int value = 890; • String str = Integer.toString(value); • Double is similar. double  String • String  double: double d = Double.parseDouble(“345.12”); • double  String : String str = Double.toString(3.14);

  26. Wrapper Classes • Character • gives information about a particular char • Remind from last week: • boolean b = Character.isLowerCase(‘c’); • b = Character.isUpperCase(‘c’); • b = Character.isLetter(‘c’); • b = Character.isDigit(‘c’);

  27. Wrapper ClassesCode Example 1 • Output The integer is 666 The double is 4.51 • public class Wrappers { • public static void main(String[] args) { • String number1 = "666"; • String number2 = "4.51"; • int value1 = Integer.parseInt( number1 ); • double value2 = Double.parseDouble( number2 ); • System.out.println("The integer is " + value1); • System.out.println("The double is " + value2); } • } • What would happen if the parseInt and parseDouble methods were not used? (i.e.: int value1 = number1;)

  28. Wrapper ClassesCode Example 2 • Output U is: a letter: true a digit: false uppercase: true lowercase: false 4 is: a letter: false a digit: true uppercase: false uppercase: false • public class Wrappers { • public static void main(String[] args) { • char c = 'U'; • char d = '4'; • System.out.println(c + " is:"); • System.out.println("a letter:\t" + Character.isLetter(c)); • System.out.println("a digit:\t" + Character.isDigit(c)); • System.out.println("uppercase:\t" + Character.isUpperCase(c)); • System.out.println("lowercase:\t" + Character.isLowerCase(c)); • System.out.println(d + " is:"); • System.out.println("a letter:\t" + Character.isLetter(d)); • System.out.println("a digit:\t" + Character.isDigit(d)); • System.out.println("uppercase:\t" + Character.isUpperCase(d)); • System.out.println("uppercase:\t" + Character.isLowerCase(d)); } • }

  29. Final QUESTIONS???

More Related