+ All Categories
Home > Documents > CSE 1341 Honors

CSE 1341 Honors

Date post: 23-Feb-2016
Category:
Upload: inez
View: 28 times
Download: 0 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
22
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 10
Transcript
Page 1: CSE 1341 Honors

CSE 1341 Honors

Professor Mark FontenotSouthern Methodist University

Note Set 10

Page 2: CSE 1341 Honors

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

Page 3: CSE 1341 Honors

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

Page 4: CSE 1341 Honors

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

Page 5: CSE 1341 Honors

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

Page 6: CSE 1341 Honors

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”;

Page 7: CSE 1341 Honors

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

Page 8: CSE 1341 Honors

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

Page 9: CSE 1341 Honors

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

Page 10: CSE 1341 Honors

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

Page 11: CSE 1341 Honors

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

Page 12: CSE 1341 Honors

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;}

Page 13: CSE 1341 Honors

BREAKOUT 1

Page 14: CSE 1341 Honors

BREAKOUT 2

Page 15: CSE 1341 Honors

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

Page 16: CSE 1341 Honors

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.

Page 17: CSE 1341 Honors

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.

Page 18: CSE 1341 Honors

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!

Page 19: CSE 1341 Honors

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) { //…}

Page 20: CSE 1341 Honors

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

Page 21: CSE 1341 Honors

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.

Page 22: CSE 1341 Honors

?


Recommended