Java Basics. SP17 ICS 111 Ed Meyer

Similar documents
Building Java Programs

Building Java Programs

Building Java Programs

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

Midterm Review 01. SP17 ICS 111 Ed Meyer

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

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

Topic 4 Expressions and variables

CS 302: Introduction to Programming

Full file at

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lecture 3 Operators MIT AITI

CSE 142, Summer 2015

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

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Operators. Java operators are classified into three categories:

Basic Operations jgrasp debugger Writing Programs & Checkstyle

CSE 142, Summer 2014

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

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

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

Chapter 2: Using Data

COMP String and Console I/O. Yi Hong May 18, 2015

Declaration and Memory

DATA TYPES AND EXPRESSIONS

Lecture 2: Operations and Data Types

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions

Object-Oriented Programming

CIS 110: Introduction to Computer Programming

Building Java Programs Chapter 2

JAVA OPERATORS GENERAL

Homework Set 1- Fundamentals

CS111: PROGRAMMING LANGUAGE II

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

Lecture 3: Variables and assignment

CMPT 125: Lecture 3 Data and Expressions

Course Outline. Introduction to java

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

MODULE 02: BASIC COMPUTATION IN JAVA

Fundamentals of Programming CS-110. Lecture 3

CS111: PROGRAMMING LANGUAGE II

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

Week 2: Data and Output

JAVA Programming Fundamentals

CS 106 Introduction to Computer Science I

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

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

ISA 563 : Fundamentals of Systems Programming

The Java language has a wide variety of modifiers, including the following:

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

AP Computer Science Homework Set 1 Fundamentals

Chapter 2: Data and Expressions

CS313D: ADVANCED PROGRAMMING LANGUAGE

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

COMP 202 Java in one week

Expressions and Control Statements

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Table of Contents Date(s) Title/Topic Page #s. Abstraction

CT 229. Java Syntax 26/09/2006 CT229

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

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

Computer Science is...

First Java Program - Output to the Screen

Chapter 2: Objects and Primitive Data

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Lecture 7. Instructor: Craig Duckett OUTPUT

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

AP Computer Science A

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Java Basic Programming Constructs

Datatypes, Variables, and Operations

AP Computer Science Summer Work Mrs. Kaelin

COMP 110 Introduction to Programming. What did we discuss?

LECTURE 3 C++ Basics Part 2

Variables and data types

CSC 1214: Object-Oriented Programming

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

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

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Unit 3. Constants and Expressions

Control Structures in Java if-else and switch

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

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

Section 2: Introduction to Java. Historical note

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

CHAPTER 2 Java Fundamentals

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

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

Transcription:

Java Basics SP17 ICS 111 Ed Meyer

Assignment 01 Write a Java program that: Prints a 2-line computer-related joke or a 4-line poem Uses print and println at least once Follows the Coding Standard Full details in Digital Dropbox Due Tues. Jan. 24 by 11:55pm Submit only the.java file 2

Quiz 01 Due Wed. Jan. 25 by 11:55pm Password: quiz01 15 Questions Multiple choice, True/False Based on lectures: Week 01 Day 02 Week 02 Day 01 and Day 02 (today) Attempts: 1 Time: 20 minutes 3

Last Time Print complex messages Escape Sequences \ Concatenation + Programming concepts Variables Constants Data types Declaring vs Initializing 4

Today Using String variables Arithmetic Expressions Assignment and Comparisons 5

String Variables What is a String? is a sequence of characters or words It is NOT a primitive data type Made up of characters 6

Breaking Down a String String message = "It's over 9,000!"; I t ' s o v e r 9, 0 0 0! The string is 16 characters long. 7

More on Strings Strings are very commonly used They are special They have object properties They have primitive type properties Can be declared like a primitive data type 8

Strings are Useful Help us label output Give instructions to the user Build more complex strings using concatenation 9

The String Data Type It is a Java object String JavaDoc It may be declared As a Java object As a primitive data type 10

Declaring and Initializing a String As you would expect from previous examples String message = "I choose you, Pikachu!"; Declaring and initializing as a primitive data type prim_data_type var_name = value; 11

Printing a Previously Declared String String message = "I choose you, Pikachu!"; // Pretend there is more code here System.out.println(message); Notice you don't need the quotes when you use the variable name. 12

When do you put the quote? If you want to use a string that is inside a variable No quotes are needed, just use the variable name If you are hard-coding a message You need to quote the message If you are initializing a string variable You need to quote the message 13

Printing Two Strings String location = "a bottle"; System.out.print("This message was found in"); System.out.println(location); This message was found ina bottle 14

Printing Two Strings String location = "a bottle"; System.out.print("This message was found in "); System.out.println(location); This message was found in a bottle 15

Arithmetic Expressions 16

Arithmetic Expressions add + subtract multiply * divide / modulus % Asterisk: Shift + 8 Follows the PEMDAS rule as you would expect Modulus has same priority as * and / 17

Adding Example int i = 20; int j = 5; int total = 0; total = i + j; These are declaration and initializing statements. 18

Adding Example int i = 20; int j = 5; int total = 0; total = i + j; This is an assignment statement. Take the thing on the right, and store it in the left. 19

More about Modulus % Calculate the remainder of a division Examples: 23 % 5 =? 20

More about Modulus % Calculate the remainder of a division Examples: 23 % 5 = 3 17 % 3 =? 21

More about Modulus % Calculate the remainder of a division Examples: 23 % 5 = 3 17 % 3 = 2 16 % 4 =? 22

More about Modulus % Calculate the remainder of a division Examples: 23 % 5 = 3 17 % 3 = 2 16 % 4 = 0 23

Assignment, Comparisons, and Output

Why Declare All Your Variables Because Java says... If you use the same piece of data more than once, declaring a variable is very useful. Declare all variables at the beginning Set aside all the boxes you will need in the program 25

Declaration Example int i = 20; int j = 5; int resulta = 0; int results = 0; int resultm = 0; int resultd = 0; You can reuse the same code with different values of i and j. resulta = i + j; results = i j; resultm = i * j; resultd = i / j; 26

Comparisons If we compare two items, the result will be true or false Boolean! Primitive data type: boolean 27

Boolean Operators Comparison Symbol Greater than > Less than < Greater than or Equal to >= Less than or Equal to <= Equal to == (double equals) 28

Comparison Example int x = 7; int y = 19; boolean isgreater = true; boolean issmaller = false; isgreater = x > y; issmaller = x < y; 30

Assignment and Comparison In programming, = does not mean "is equal to" = means assignment Take the thing on the right, and put it in the left == means "is equal?" It is a question It compares and gives a Boolean result 31

Assignment and Comparison Example int x = 5; int y = 20; int total = 0; boolean isequal = true; total = x + y; isequal = x == y; 32

Assignment and Comparison Example isequal = x == y; Evaluate the stuff on the right first Take the thing on the right, and put it in the left 33

What will print? String output = ""; output = output + "Hi, my name is"; output = output + "Ed"; System.out.print(output); Hi, my name ised 34

More on System.out A program is worthless without feedback print and println have parameters Parameters are things inside parenthesis ( ) Information required to execute an action System.out.println() will print a blank line System.out.print() will throw an error If there is nothing in the parenthesis Java expects a parameter to print 35

A Bad Example int x = 5; int y = 20; int total = 0; boolean isequal = true; total = x + y; isequal = x == y; System.out.println(total); System.out.println(isEqual); 36

Let's Do Some Examples 37