CSE 1341 Honors
Professor Mark FontenotSouthern 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 – operator placed before the variable– Increment happens before value is used in
containing expression.
int x = 5;System.out.println(x);System.out.println(++x);System.out.println(x);
566
Output
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
int x = 5;System.out.println(x);System.out.println(x++);System.out.println(x);
556
Output
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
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”;
Strings in RAMString myName = “Mark Fontenot”;
myName is a reference variable. Its data type is String.
myNameMark Fontenot
an Object of type String. A reference variable oftype String
Main memory/RAM
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;
String temp = “mark”;
if(temp.equals(“bob”)){ System.out.println(“yes”);} else { System.out.println(“no”);}
String s1 = “Mark”;String s2 = “mark”;
if(s1.equals(s2)){ System.out.println(“yes”);} else { System.out.println(“no”);}
These methods return boolean values (either true or false)
Can compare to string literal or another string object
String ComparisonString s1 = “Mark”;String s2 = “mark”;
if(s1.equals(s2)){ System.out.println(“yes”);} else { System.out.println(“no”);}
String s1 = “Mark”;String s2 = “mark”;
if(s1.equalsIgnoreCase(s2)){ System.out.println(“yes”);} else { System.out.println(“no”);}
Takes case of differentletters into account
Ignores case when comparing
Some Other String Methods• length()– How many characters are in the string
• charAt(int val)– 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
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
String Example• Implement a method that will return the
number of vowels in the string
public static int countVowels (String input) {
int counter = 0;// will hold the count of vowels for (int i = 0; i < input.length(); i++) { char temp = input.charAt(i); if(temp == ‘a’ || temp == ‘e’ || temp == ‘i’ || temp == ‘o’ || temp == ‘u’) { counter ++; } } return counter;}
BREAKOUT 1
BREAKOUT 2
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
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.
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.
Scanner s = new Scanner (System.in);
s.nextInt();
s.nextDouble();
Creating a scanner object
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.
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!
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;int b = 20;if (a < b) { //…}
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
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.
?