Lecture 5. Assignments. Arithmetic Operations. Arithmetic Operations -Summary 1/24/18. Lab 3: Variables and operations. Read Sections

Similar documents
Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

JAVA OPERATORS GENERAL

Building Java Programs

Building Java Programs

CIS 110: Introduction to Computer Programming

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Datatypes, Variables, and Operations

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

CS 106 Introduction to Computer Science I

A variable is a name for a location in memory A variable must be declared

CS313D: ADVANCED PROGRAMMING LANGUAGE

CMPT 125: Lecture 3 Data and Expressions

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Chapter 2: Using Data

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Building Java Programs

Full file at

Chapter 3: Operators, Expressions and Type Conversion

Object-Oriented Programming

Operators. Java operators are classified into three categories:

COMP Primitive and Class Types. Yi Hong May 14, 2015

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Midterms Save the Dates!

MODULE 02: BASIC COMPUTATION IN JAVA

We now start exploring some key elements of the Java programming language and ways of performing I/O

Computational Expression

Ex: If you use a program to record sales, you will want to remember data:

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Program Fundamentals

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Programming with Java

Arithmetic type issues

COMP 202 Java in one week

Unit 3. Operators. School of Science and Technology INTRODUCTION

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

LECTURE 3 C++ Basics Part 2

Introduction to the C++ Programming Language

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Values, Variables, Types & Arithmetic Expressions. Agenda

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

AP Computer Science A

Lecture 3 Tao Wang 1

Chapter 2: Using Data

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CSE 142, Summer 2014

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

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

CSE 142, Summer 2015

Lecture 2: Operations and Data Types

CS 106 Introduction to Computer Science I

SECTION II: LANGUAGE BASICS

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

REVIEW. The C++ Programming Language. CS 151 Review #2

ISA 563 : Fundamentals of Systems Programming

6.096 Introduction to C++ January (IAP) 2009

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Information Science 1

Operators. Lecture 3 COP 3014 Spring January 16, 2018

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Basics of Java Programming

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Exercise: Inventing Language

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Information Science 1

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS 302: Introduction to Programming

GraphQuil Language Reference Manual COMS W4115

CS111: PROGRAMMING LANGUAGE II

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Java Simple Data Types

Operators & Expressions

Computer Components. Software{ User Programs. Operating System. Hardware

Declaration and Memory

Lecture 3: Variables and assignment

CHAPTER 3 BASIC INSTRUCTION OF C++

I. Variables and Data Type week 3

Getting started with Java

Java Simple Data Types

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Logical Operators and switch

CSC 1214: Object-Oriented Programming

Java Basic Programming Constructs

Course Outline. Introduction to java

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Full file at

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Transcription:

Assignments Lecture 5 Complete before Lab 4 Lab 3: Variables and operations Read Sections 2.9-2.13 Arithmetic Operations -Summary Computers excel at performing repeated arithmetic operations quickly and without error. There are 5 common operations: Addition: + Subtraction: - Multiplication: * Division: / Recall: A B = Quotient * B + Remainder If both operands are integer: produces the quotient Else same as A B Modulus: % (produces the remainder) Arithmetic Operations What rules are applied when an expression contains more than 1 operator? int x = 2 + 3 * 4-6 / 2 + 4 * 7 (5+2)? Java uses two rules: Operators are assigned a priority, highest priority operations are performed first Operations of the same priority are evaluated from left to right* 1

Combined Assignment Short Hand Reference Data Types The following are equivalent x += 4; x = x + 4; x -= 17; x = x 17; x *= 10; x = x * 10; x /= 5; x = x / 5; X %= 3; x = x % 3; A variable that is a reference data type stores a reference (or a pointer) to memory. The memory pointed to stores an object of the type specified. This is much different that what we have been discussing. A variable whose type is int, stores a number. In Java, we will create references to objects. Objects are defined by a class Objects are created Java String Class The String class is used to create and store strings of characters (or letters). Declaring a String reference: String city; The variables fullname and city are references to String objects. What is the value stored in fullname and city? Creating a String Object There are 2 different ways to create a String object. Use a string literal and perform an assignment (or combine literals) Use the Java keyword new city = new String( Pittsburgh ); Behind the scenes, how Java creates the String object is a little different in these two cases, but we are going to ignore that. For both: Java will create a String object in memory. The memory address for the object will then be assigned to the variable fullname will have one address and city will have a different address. 2

Using Strings To see the full capability of the Java String class, search for Java String and select the Oracle site Lets start with some common commands. What kinds of things might we want to do with a String? Print it Find out how many characters are in it Convert the case to all lower (or all upper) Find the character at a specific position whatever that means. Outputting a String To print Mary Fudrucker all we need to do is give the name of the String reference to print or println Java will do the rest. The statement System.out.println(fullName); produces: Mary Fudrucker The statement System.out.println( Clients full name is: + fullname); produces: Clients full name is: Mary Fudrucker Getting the number of characters To find out the number of characters are in a string, we ask the string. What do we ask? How do we ask? What: The String class defines a method that returns the number of characters. int length() We can output the number of characters int stringsize = fullname.length(); // call method length() System.out.println( The length of the string is: + stringsize); What is printed? 14. How: We have a reference to the String fullname We call the method length on the object (call using the dot) int stringsize = fullname.length(); 3

We can change case The Java String class allows you to get a reference to a new String object that is in all lower, or upper case: String tolowercase(); String touppercase(); Why might this be useful? Suppose you ask a user to enter yes or no in response to a question as part of a form. They may type: yes, Yes, YES, Yes, YEs, yes, yes, yes.if you convert their response to lower case its easier to compare! Get the name in all UPPER case String allupper = fullname.touppercase(); // calling method System.out.println( The original name is: + fullname); System.out.println( In all upper case is: + allupper); Suppose we wanted to change the name to all upper case, what modification would we make? Use the idea of combined assignment that we saw with algebraic operations fullname = fullname.touppercase(); Getting individual characters It can be useful to be able to get individual characters in a String Want to determine if a user responded yes, or yeah, or Yes, or. Want to determine if a particular character exists Test the word to see if it is a palindrome (reads forwards and backwards) Java String class provide a method to get a character at a specified position: char charat(int position); // position is 0-based More on charat What is the minimum position that we can use in charat? 0 What is the maximum position that we can use in charat? int stringsize = fullname.length(); Maximum position is stringsize 1 What happens if we put in a value that is too big? We get a run-time error. 4

Palindrome Example Pseudo Code: While we have characters left Read character on left end Read character on right end Compare. If different, NOT a palindrome. Done Else test next pair of characters Pseudo Code: int leftpos = 0; int rightpos = s.length() -1; While we have characters left char leftchar = s.charat(leftpos); char rightchar = s.charat(rightpos); Compare. If different NOT a palindrome. Done Else leftpos += 1; rightpos -= 1; Example: Print out Yes or No based on answer String answer; answer = Yep ; answer = answer.tolowercase(); char c = answer.charat(0); // get the first character If the user answered y System.out.println( The user answered Yes ); Otherwise System.out.println( The user answered No ); Recap. How does Java evaluate an expression such as the one below? int n = 2 / 5 * ( 4 + 2 * 2 ) 17 + 3 * 5; Variables are declared: type name; Assign values to variables using operator: = Variable type is: primitive or reference primitive types start with lower case letter - int, byte, short, long, double reference types start with Upper Case letter example: String Java Strings: Declaring, assigning a value, Learned how to call a method. Very Important to Understand. reading number of characters, changing case, getting individual characters a) Java evaluates () first, then *, / and % using a left to right rule, and finally + - using a left to right rule. b) Java evaluates the expression from left to right c) Java evaluates () first, then all *, then all /, then all %, then all +, then all - d) Java cannot evaluate expressions like the one above. Bonus: +1 pt if you provide the correct value of n -2 A lot of people computed n improperly. 2/5*8 evaluate 2/5 first. This is integer division. The quotient of 2/5 is 0. So 2/5*8 becomes 0 * 8 which is 0. The result is n is -1 5

Java supports 2 general data types: primitives and references. Java String objects store sequences of characters. To declare the variable str as String reference, we write: String str; Circle the statements below that will result in the variable str referencing a Java String object that stores Java is fun!. a) str = Java is fun! ; b) str = new String( Java is fun! ); c) str = Java is fun! d) str = String( Java is fun! ); 6