Basic Computation. Chapter 2

Similar documents
Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Basic Computation. Chapter 2

Basic Computation. Chapter 2

COMP 110 Introduction to Programming. What did we discuss?

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

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

Objectives. Primitive Types, Strings, and Console I/O. Variables and Values. Outline. Naming and Declaring Variables. Variables and Values

A+ Computer Science -

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

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

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

Input. Scanner keyboard = new Scanner(System.in); String name;

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Chapter 5 Lab Methods

Section 2: Introduction to Java. Historical note

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

Chapter. Let's explore some other fundamental programming concepts

A+ Computer Science -

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

Chapter 5 Lab Methods

Data Conversion & Scanner Class

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

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

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

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

Full file at

Chapter 2: Data and Expressions

Robots. Byron Weber Becker. chapter 6

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Computational Expression

Chapter 4 Lab. Loops and Files. Objectives. Introduction

Section 2.2 Your First Program in Java: Printing a Line of Text

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

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

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Introduction to Software Development (ISD) David Weston and Igor Razgon

ECE 122 Engineering Problem Solving with Java

Introduction to Java Unit 1. Using BlueJ to Write Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

Chapter 2: Data and Expressions

Computational Expression

Lecture Set 2: Starting Java

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

Lecture Set 2: Starting Java

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

AP Computer Science Unit 1. Writing Programs Using BlueJ

Chapter 5 Lab Methods

Programming with Java

When we reach the line "z = x / y" the program crashes with the message:

Full file at

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

Chapter 4: Conditionals and Recursion

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

Lecture 8 " INPUT " Instructor: Craig Duckett

Over and Over Again GEEN163

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

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

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

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

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

double float char In a method: final typename variablename = expression ;

CS111: PROGRAMMING LANGUAGE II

2: Basics of Java Programming

CS110: PROGRAMMING LANGUAGE I

AP Computer Science A

AP Computer Science Unit 1. Programs

Full file at

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Supplementary Test 1

Chapter 2: Basic Elements of Java

Defining Classes and Methods

Practice Midterm 1. Problem Points Score TOTAL 50

Full file at

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Full file at

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

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

STUDENT LESSON A7 Simple I/O

Defining Classes and Methods

Console Input and Output

CS 106 Introduction to Computer Science I

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

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

Introduction to Java Applications

MODULE 02: BASIC COMPUTATION IN JAVA

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Elementary Programming

Personal SE. Arrays Pointers Strings

Task #1 The if Statement, Comparing Strings, and Flags

CS 302: Introduction to Programming

Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 2 Exercise Solutions

Midterms Save the Dates!

COMP 202 Java in one week

Transcription:

Basic Computation Chapter 2

Increment and Decrement Operators Used to increase (or decrease) the value of a variable by 1 Easy to use, important to recognize The increment operator count++ or ++count The decrement operator count-- or --count

Increment and Decrement Operators equivalent operations count++; ++count; count = count + 1; count--; --count; count = count - 1;

Increment and Decrement Operators in Expressions after executing int m = 4; int result = 3 * (++m) result has a value of 15 and m has a value of 5 after executing int m = 4; int result = 3 * (m++) result has a value of 12 and m has a value of 5

The Class String We've used constants of type String already. "Enter a whole number from 1 to 99." A value of type String is a Sequence of characters Treated as a single item.

String Constants and Variables Declaring String greeting; greeting = "Hello!"; or String greeting = "Hello!"; or String greeting = new String("Hello!"); Printing System.out.println(greeting);

String Methods An object of the String class stores data consisting of a sequence of characters. Objects have methods as well as data The length() method returns the number of characters in a particular String object. String greeting = "Hello"; int n = greeting.length();

The Method length() The method length() returns an int. You can use a call to method length() anywhere an int can be used. int count = command.length(); System.out.println("Length is " + command.length()); count = command.length() + 3;

Figure 2.4 String Indices Positions start with 0, not 1. The 'J' in "Java is fun." is in position 0 A position is referred to an an index. The 'f' in "Java is fun." is at index 8.

String Methods Figure 2.5a

String Methods Figure 2.5b

String Methods Figure 2.5c

String Methods Figure 2.5d

String Processing No methods allow you to change the value of a String object. But you can change the value of a String variable. View sample program StringDemo listing 2.4 Sample Screen Screen Output Output

Escape Characters How would you print "Java" refers to a language.? The compiler needs to be told that the quotation marks (") do not signal the start or end of a string, but instead are to be printed. System.out.println( "\"Java\" refers to a language.");

Escape Characters Figure 2.6 Each escape sequence is a single character even though it is written with two symbols.

Examples System.out.println("abc\\def"); abc\def System.out.println("new\nline"); new line char singlequote = '\''; System.out.println (singlequote); '

Keyboard and Screen I/O: Outline Screen Output Keyboard Input

Screen Output We've seen several examples of screen output already. System.out is an object that is part of Java. println() is one of the methods available to the System.out object. Alternatively, there is print()

Keyboard Input Java has reasonable facilities for handling keyboard input. These facilities are provided by the Scanner class in the java.util package. A package is a library of classes.

Using the Scanner Class Near the beginning of your program, insert import java.util.scanner; Create an object of the Scanner class Scanner keyboard = new Scanner (System.in) Read data (an int or a double, for example) int n1 = keyboard.nextint(); double d1 = keyboard,nextdouble();

Keyboard Input Demonstration View sample program class ScannerDemo, listing 2.5 Sample Screen Screen Output Output

Some Scanner Class Methods Figure 2.7a

Some Scanner Class Methods Figure 2.7b

nextline()method Caution The nextline() method reads The remainder of the current line, Even if it is empty.

nextline()method Caution Example given following declaration. int n; String s1, s2; n = keyboard.nextint(); s1 = keyboard.nextline(); s2 = keyboard.nextline(); Assume input shown n is set to 42 42 and don't you forget it. but s1 is set to the empty string.

The Empty String A string can have any number of characters, including zero. The string with zero characters is called the empty string. The empty string is useful and can be created in many ways including String s3 = "";