CS251L REVIEW Derek Trumbo UNM

Similar documents
String. Other languages that implement strings as character arrays

Programming with Java

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS251L REVIEW Derek Trumbo UNM

Index COPYRIGHTED MATERIAL

Introduction to Programming Using Java (98-388)

Lab 14 & 15: String Handling

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Searching and Strings. IST 256 Application Programming for Information Systems

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

5/23/2015. Core Java Syllabus. VikRam ShaRma

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 1301 Ch 8, Part A

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

USING LIBRARY CLASSES

STRINGS AND STRINGBUILDERS. Spring 2019

Getting started with Java

H212 Introduction to Software Systems Honors

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

Java Foundations: Unit 3. Parts of a Java Program

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

More non-primitive types Lesson 06

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables


2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Java Fall 2018 Margaret Reid-Miller

Class. Chapter 6: Data Abstraction. Example. Class

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

"Hello" " This " + "is String " + "concatenation"

Advanced Object Oriented Programming EECS2030Z

Character Strings COSC Yves Lespérance. Lecture Notes Week 6 Java Strings

Computational Expression

Basic Operations jgrasp debugger Writing Programs & Checkstyle

PROGRAMMING FUNDAMENTALS

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE

Building Java Programs

Object interactions Lecture 6

( &% class MyClass { }

Chapter 2: Data and Expressions

1Z Java SE 5 and 6, Certified Associate Exam Summary Syllabus Questions

Chapter 1 Introduction to java

JAVASCRIPT BASICS. JavaScript String Functions. Here is the basic condition you have to follow. If you start a string with

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Lecture Notes for CS 150 Fall 2009; Version 0.5

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Building Java Programs

CSE 142 Su 04 Computer Programming 1 - Java. Objects

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Java Foundations Certified Junior Associate

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

COURSE 1 PROGRAMMING III OOP. JAVA LANGUAGE

Main loop structure. A Technical Support System. The exit condition. Main loop body

Ohad Barzilay and Oranit Dror

Welcome to the Using Objects lab!

Creating Classes and Objects

Lec 3. Compilers, Debugging, Hello World, and Variables

Java Object Oriented Design. CSC207 Fall 2014

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Getting started with Java

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

1B1b Classes in Java Part I

Java Classes and Objects

Computer Programming, I. Laboratory Manual. Final Exam Solution

Creating Strings. String Length

CS111: PROGRAMMING LANGUAGE II

13 th Windsor Regional Secondary School Computer Programming Competition

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Pace University. Fundamental Concepts of CS121 1

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

More on variables and methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Interaction with Android

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

CSE 142, Summer 2014

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

String related classes

Week 6: Review. Java is Case Sensitive

JAVA OPERATORS GENERAL

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

A Founda4on for Programming

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

3. Java - Language Constructs I

CS 302: Introduction to Programming in Java. Lecture 11 Yinggang Huang. CS302 Summer 2012

Strings and Arrays. Hendrik Speleers

Transcription:

CS251L REVIEW 2010.8.30 Derek Trumbo UNM

Arrays Example of array thought process in Eclipse

Arrays Multi-dimensional arrays are also supported by most PL s 2-dimensional arrays are just like a matrix (monthly accident counts for past decade, 0 == 2000): int[][] acccount = new int[10][12]; acccount[0][0] = 3; System.out.println(accCount[3][4]);

Arrays We can also provide the contents of an array when we declare it but don t provide size information int[][] multi = new int[][] {{1, 2, 3, {4, 5, 6; Row 0 1 1 2 3 4 5 6* int asteriskcell = multi[1][2];

Strings Strings in Java are not primitives Strings are fully-fledged objects that encapsulate an array of characters Java uses string pooling to minimize duplication of String objects in memory String objects are immutable Immutable objects are those objects whose internal state does not change after that object is initially created

Strings Just as == identifies if two primitive data types have the same value, the == when applied to two object references identifies if the references point to the exact same object However, when comparing strings in Java, we rarely care if two strings are the same object but rather care more if the two strings contain the same characters

Strings String s1 = "Neo"; String s2 = s1; s1 s2 Application Memory Neo s1 == s2 is true, s1.equals(s2) is true

Strings String s1 = "Neo"; String s2 = new String("Neo"); s1 s2 Application Memory Neo Neo s1 == s2 is false, s1.equals(s2) is true

Strings A method, equals(), in the String class performs this comparison for you String s1 = "ABC"; String s2 = "abc"; boolean same = s1.equals(s2); boolean sameic = s1.equalsignorecase(s2);

Strings Become familiar with the Java API for the String class: http://download.oracle.com/javase/6/docs/api/java/ lang/string.html Some useful methods are: charat, endswith, equals, equalsignorecase, indexof, lastindexof, length, startswith, substring, tolowercase, touppercase, trim

Strings Strings in Java get some royal treatment the concatenation operator ( + ) made special for them At the same level of precedence as the regular arithmetic + operator, this operator combines a String object and another primitive data type or object into a larger String object String day = "Monday"; int date = 3; double temp = 89.4; String msg = day + "/" + date + ": T=" + temp; System.out.println(msg);

Methods The code in useful programs can always be divided into areas of responsibility Most generic term for these are procedures In C/C++ these areas are called functions when they don t belong to a class, and those within classes are called member functions (since they are members of the class) In Java, we call these areas of responsibility methods and they always exist within some class

Methods Methods exist to decompose the problem into smaller units of work for many reasons: Readability Stability (debug-ability) Code reuse (maintainability)

Methods Every method in Java exists within some class Methods have these parts: Access modifier (public, protected, private, package) Optional static keyword Return type (any primitive data type or class reference) Method name (in lower-camel case, e.g. setthething ) Parameter list (zero or more, comma-delimited) Exception list (if the method declares that it throws ex.) Method body (one or more lines of Java in {)

Methods Before you learn about object oriented programming, many methods you write in your apps may be static. This allows the method to execute without first having an instance of the class that s in existence (more will be covered on this later on in course)

Methods public class MyClass { public static void main(string[] args) { aaa(); public static void aaa() { bbb(); public static void bbb() { System.out.println("Hello World");

Methods public class MyClass { public static void main(string[] args) { aaa(); public static void aaa() { int result = bbb(); System.out.println("bbb = " + result); public static int bbb() { return 42;

Methods public class MyClass { public static void main(string[] args) { aaa(5); public static void aaa(int param) { bbb("saturn", param * 2.5); public static void bbb(string a, double b) { System.out.println(a + b);

Variables & Literals Variables are storage locations you can declare in code, whose values can change over time, and which are referenced by a name of your choosing Literals are those constant values of any primitive data type (and strings) that are placed directly into the code

Variables & Literals public class MyClass { public static void main(string[] args) { double value = 40.05 * 2003.44 * 2003.44 / 78; String msg = "Value is " + value + "!"; System.out.println(msg);

Variables & Literals public class MyClass { public static void main(string[] args) { double value = 40.05 * 2003.44 * 2003.44 / 78; String msg = "Value is " + value + "!"; System.out.println(msg); Variables both declarations and uses

Variables & Literals public class MyClass { public static void main(string[] args) { double value = 40.05 * 2003.44 * 2003.44 / 78; String msg = "Value is " + value + "!"; System.out.println(msg); Literals primitives and strings

Variables & Literals public class MyClass { public static void main(string[] args) { double mass = 40.05; double velocity = 2003.44; int radius = 78; double centripetalforce = mass * Math.pow(velocity, 2) / radius; String msg = "Centripetal Force: " + centripetalforce; System.out.println(msg);

Variables & Literals public class MyClass { public static void main(string[] args) { double mass = 40.05; double velocity = 2003.44; int radius = 78; double centripetalforce = mass * Math.pow(velocity, 2) / radius; String msg = "Centripetal Force: " + centripetalforce; System.out.println(msg); Variables both declarations and uses

Variables & Literals public class MyClass { public static void main(string[] args) { double mass = 40.05; double velocity = 2003.44; int radius = 78; double centripetalforce = mass * Math.pow(velocity, 2) / radius; String msg = "Centripetal Force: " + centripetalforce; System.out.println(msg); Literals primitives and strings