Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 11

CSE110 Lecture 11

Principles of Programming with Java
Review
(202005)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE110 Principles of Programming with Java Lecture 11: Review Javier

    Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 Comparing

    Strings • Remember that a character string in Java is not a primitive data type. • We cannot use the relational operators to compare strings • Do NOT do this String str1 = "apple"; String str2 = "banana"; if (str1 == str2){//we are not supposed to do this }
  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Comparing

    Strings String str1 = "apple"; String str2 = "banana"; //do NOT do this if (str1 == str2){ } // Instead we need to do: if (str1.equals(str2)){ }
  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Comparing

    Strings • The following does not also compare two strings: String str1 = "apple"; String str2 = "banana"; //do NOT do this if (str1 < str2){ } // Instead we need to do: if (str1.compareTo(str2) < 0){ }
  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Example

    int result = str1.compareTo(str2); if (result < 0) { //if str1 is smaller than str2 } else if (result > 0){ //if str1 is larger than str2 } else if (result == 0) //if str1 is identical to str2 }
  6. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Comparing

    Strings • Comparing strings is based on a character set, it is called a lexicographic ordering • It uses Unicode of each character in strings. • Also, short strings come before longer strings with the same prefix (lexicographically) Therefore "book" comes before "bookcase"
  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Disclaimer

    The previous questions are just examples. They are NOT a comprehensive list of topics Review All the Slides for a full list of topics The exam is NOT limited to the topics covered in the previous questions. For instance printf() was not in the previous examples; but could be part of your exam
  8. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Day

    2 1. What is a compiler? 2. What is Java byte code? 3. Give an example of a lexical error in Java 4. Give an example of a syntactic error in Java 5. Give an example of a semantic error in Java
  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Day

    3 1. What is a variable? 2. What is a method? 3. What is a class? 4. Create a program that prints “This is my exam”. Including the code for creating a class and main method. 5. Write 3 reserved words in Java
  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Day

    4 1. Write a multi-line comment with your name and ASUID 2. Mention the 8 primitive types defined in Java 3. Is the following statement correct? char = ' ' '; 4. Is the following statement correct? boolean result = 5 + 2 > 5 - 1 ; 5. What is the result of (-20 % 3 -2)
  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Day

    5, 6 1. What is the result of 1 % 2 - 3 / 4 * 5 / 6 + 7 2. Is the following correct? If so, What is the result? 1 % 2 - 3 / 4 * 5 / 6 + 7 + “Hello” 3. Is the following correct? If so, What is the result? ”Hello” + 1 % 2 - 3 / 4 * 5 / 6 + 7 4. Is the following correct? If so, What is the result? float f = 2.5f + 2.5f; System.out.println(f); 5. Is the following correct? If so, What is the result? System.out.println( Math.pow(5.0,2.0) );
  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 Day

    7 1. What is a package? 2. What is the next instruction for? JOptionPane.showInputDialog(“this is a message”); 3. What is the next instruction for? Integer.parseInt(“12345”); 4. What is JOptionPane? 5. What is showInputDialog?
  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Day

    8, 9 int x = 75; y = 95; z = 1; if (x < y && x < z) { if (y < z) System.out.println( x + " " + y + " " + z ); else System.out.println( x + " " + z + " " + y ); } else if (x > y && x > z) { if (y < z) System.out.println( y + " " + z + " " + x ); else System.out.println( z + " " + y + " " + x ); } else { if (y < z) System.out.println( y + " " + x + " " + z); else System.out.println( z + " " + x + " " + y); }
  14. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Day

    10 public class Example { public static void main(String args[]) { int age = 18; switch (age) { case 16: System.out.println("You are under 18."); break; case (18): System.out.println("You are eligible for vote."); break; case (65): System.out.println("You are senior citizen."); break; default: System.out.println("Please give the valid age."); break; } } }
  15. CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020

    Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.