1 / 22

CSE 1341 Honors

CSE 1341 Honors. Professor Mark Fontenot Southern Methodist University Note Set 10. Overview. Increment/Decrement Operators More on Strings Introduction to Objects/classes /primitives. Increment Operator. ++ adds one to a numerical variable PreIncrement

inez
Download Presentation

CSE 1341 Honors

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. CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 10

  2. Overview • Increment/Decrement Operators • More on Strings • Introduction to Objects/classes/primitives

  3. Increment Operator • ++ • adds one to a numerical variable • PreIncrement • operator placed before the variable • Increment happens before value is used in containing expression. Output intx = 5; System.out.println(x); System.out.println(++x); System.out.println(x); 5 6 6

  4. Increment Operator • ++ • adds one to a numerical variable • PostIncrement– • Operator is placed after the variable • increment happens after the value of the variable is used in the containing expression Output intx = 5; System.out.println(x); System.out.println(x++); System.out.println(x); 5 5 6

  5. Decrement Operator • -- • Subtracts one from a numeric variable. • Predecrement • comes before the variable and is applied before the variable’s value is used in the containing expression • Postdecrement • comes after the variable and is applied after the variable's value is used in the containing expression

  6. More on Strings • String = sequence of characters • example: “CSE 1341 Honors” • Different from char – only one character ‘c’ • notice the difference in the quotes. • A String with one character is different than a char too • ‘c’ is not the same as “c” • Stored differently inside the computer • String is a data type in Java String myName = “Mark Fontenot”;

  7. Strings in RAM String myName = “Mark Fontenot”; myName is a reference variable. Its data type is String. Main memory/RAM myName Mark Fontenot A reference variable of type String an Object of type String.

  8. Some Useful String Methods • Concatenate Strings using the + operator • Compare the equality using .equals() method String s1 = “Southern Methodist”; String s2 = “University”; String name = s1 + “ “ + s2; These methods return boolean values (either true or false) String s1 = “Mark”; String s2 = “mark”; if(s1.equals(s2)){ System.out.println(“yes”); } else { System.out.println(“no”); } String temp = “mark”; if(temp.equals(“bob”)){ System.out.println(“yes”); } else { System.out.println(“no”); } Can compare to string literal or another string object

  9. String Comparison String s1 = “Mark”; String s2 = “mark”; if(s1.equals(s2)){ System.out.println(“yes”); } else { System.out.println(“no”); } Takes case of different letters into account String s1 = “Mark”; String s2 = “mark”; if(s1.equalsIgnoreCase(s2)){ System.out.println(“yes”); } else { System.out.println(“no”); } Ignores case when comparing

  10. Some Other String Methods • length() • How many characters are in the string • charAt(intval) • Access a particular character in the string • val represents the index of a character in the string 1 3 4 1 H [0] [1] [2] [3] [4] Index of each character – they always start at zero

  11. Read a String from Scanner Scanner s = new Scanner (System.in); String name; System.out.print("Please enter your name: "); name = s.next(); name = s.nextLine(); Will read up to the first space character… Consider if the user typed in Southern Methodist Will read up to the next <enter> key pressed… Consider if the user typed in Southern Methodist

  12. String Example • Implement a method that will return the number of vowels in the string public static intcountVowels (String input) { int counter = 0;// will hold the count of vowels for (inti = 0; i < input.length(); i++) { char temp = input.charAt(i); if(temp == ‘a’ || temp == ‘e’ || temp == ‘i’ || temp == ‘o’ || temp == ‘u’) { counter ++; } } return counter; }

  13. Breakout 1

  14. Breakout 2

  15. Classes and Objects - 1 • Object-Oriented Programming • software development methodology • development is based on looking at the problem from the perspective of what objects are interacting in the system. • i.e. University Registration System • Students • Professors • Room Locations • Courses with multiple class sections

  16. Classes and Objects - 2 • Each object contains some data • i.e. every student has an id and name, but values may be different for each student • Each object has an interface to interact with • interface allows us to access and manipulate the data in a controlled fashion. • i.e. Functionality to retrieve the students name from the object or update the students GPA.

  17. Some Object Semantics • We communicate with an object by sending messages to the object. • In Java, we send messages by calling methods on the object. Creating a scanner object Scanner s = new Scanner (System.in); s.nextInt(); s.nextDouble(); Sending message to scanner obj to tell it to read the next integer from the keyboard. Sending message to scanner obj to tell it to read the next double from the keyboard.

  18. Primitive vs. Reference Variables • Primitive Variable – a variable with a primitive data type • primitive data types are part of the core Java language • short, int, long, char, byte, boolean, double, float. • Reference Variable – a variable with a non-primitive data type • EVERYTHING ELSE!

  19. Primitive Variables • Can be declare with out “creating” using the keyword new. • We access the variable directly • Doesn’t have an interface of methods through which to access functionality and data. • Programmer can use the relational operators to compare int a = 10; intb = 20; if (a < b) { //… }

  20. Reference Variables • Used to refer to objects • Must use the new operator to create an object of a particular type • new Scanner(System.in); • new returns a reference (memory location) that is stored in the reference variable • Scanner s = new Scanner (System.in); returns a memory location that is then stored in s

  21. The Java API • API = Application Programming Interface • Contains about 3000+ classes that are already implemented and tested • You can use this so as to not re-invent the wheel. • Scanner is a perfect example • Scanner class contains the code to create a scanner object that can allow you to read from a data source. • We’ll explore this in much more depth throughout the semester.

  22. ?

More Related