Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

COMP 14 Introduction to Programming
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Chapter 2: Basic Elements of C++
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Chapter 2-3: Basic Elements of Java. Chapter Objectives Discover how to use arithmetic operators. Examine how a program evaluates arithmetic expressions.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2: Basic Elements of Java
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
COMP 110 Introduction to Programming Mr. Joshua Stough August 29, 2007 Monday/Wednesday/Friday 3:00-4:15 Gardner Hall 307.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2: Using Data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSCI 51 Introduction to Programming Dr. Joshua Stough January 27, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 2: Basic Elements of C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2: Basic Elements of C++. Objectives In this chapter, you will: – Become familiar with the basic components of a C++ program, including functions,
COMP 14 Introduction to Programming Mr. Joshua Stough January 24, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Basic Elements of Java
Computer Programming BCT 1113
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Chapter 2: Basic Elements of Java
elementary programming
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 2 Basic Elements of Java

Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols, and identifiers Explore primitive data types Discover how to use arithmetic operators Examine how a program evaluates arithmetic expressions Explore how mixed expressions are evaluated

Chapter Objectives Learn about type casting Become familiar with the String type Learn what an assignment statement is and what it does Discover how to input data into memory by using input statements Become familiar with the use of increment and decrement operators

Chapter Objectives Examine ways to output results using output statements Learn how to import packages and why they are necessary Discover how to create a Java application program Explore how to properly structure a program, including using comments to document a program

Introduction Computer program: a sequence of statements whose objective is to accomplish a task Programming: process of planning and creating a program

The Basics of a Java Program Java program: collection of classes There is a main method in every Java application program Token: smallest individual unit of a program

Special Symbols

Word Symbols int float double char void public static throws return

Java Identifiers Names of things Consists of and must begin with: –Letters –Digits –The underscore character (_) –The dollar sign ($)

Illegal Identifiers

Data Types Data type: set of values together with a set of operations

Primitive Data Types

Floating-Point Data Types –Float: precision = 6 or 7 –Double: precision = 15 Boolean: two values –True –False

Integral Data Types

Values and Memory Allocation for Integral Data Types

Arithmetic Operators and Operator Precedence Five Arithmetic Operators –+ addition –- subtraction –* multiplication –/ division –% mod (modulus) operator (integer operands only) Unary operator: operator that has one operand Binary operator: operator that has two operands

Order of Precedence 1.* /%(same precedence) 2.+-(same precedence) Operators in 1 have a higher precedence than operators in 2. When operators have the same level of precedence, operations are performed from left to right

Expressions Integral expressions Floating-point or decimal expressions Mixed expressions

Integral Expressions All operands are integers Examples: * x – y/7 x + 2 * (y – z) + 18

Floating-point Expressions All operands are floating-point numbers Examples: 12.8 * 17.5 – x * y

Mixed Expressions Operands of different types Examples: / Integer operands yield an integer result; floating- point numbers yield floating-point results If both types of operands are present, the result is a floating-point number Precedence rules are followed

Type Conversion (Casting) Used to avoid implicit type coercion Syntax (dataTypeName) expression Expression evaluated first, then type converted to dataTypeName Examples: (int)( ) = 14 (int)(7.9) + (int)(6.7) = 13

The class String Used to manipulate strings String –Sequence of zero or more characters –Enclosed in double quotation marks –Null or empty strings have no characters –Numeric strings consist of integers or decimal numbers –Length is the number of characters in a string

Parsing Numeric Strings String to int Integer.parseInt(strExpression) String to float Float.parseFloat(strExpression) String to double Double.parseDouble(strExpression) *strExpression: expression containing a numeric string

Input Named constant –cannot be changed during program execution –declared by using the reserved word final –initialized when it is declared Variable (name, value, data type, size) –content may change during program execution –must be declared before it can be used –may not be automatically initialized –if new value is assigned, old one is destroyed –value can only be changed by an assignment statement or an input (read) statement

Input Standard input stream object: System.in Input numeric data to program –Separate by blanks, lines, or tabs To read a line of characters: 1.Create an input stream object of the class BufferedReader 2.Use the method readLine

Increment and Decrement Operators ++ increments the value of its operand by 1 -- decrements the value of its operand by 1 Syntax –Pre-increment: ++variable –Post-increment: variable++ –Pre-decrement: --variable –Post-decrement: variable--

Strings and the Operator + Operator + can be used to concatenate two strings or a string and a numeric value or character Example:

Output Standard output object: System.out Methods –print –println –flush Syntax System.out.print(stringExp); System.out.println(stringExp); System.out.flush();

Commonly Used Escape Sequences

Packages, Classes, Methods, and the import Statement Package: collection of related classes Class: consists of methods Method: designed to accomplish a specific task

import Statement Used to import the components of a package into a program Reserved word import java.io.*; imports the (components of the) package java.io into the program Primitive data types and the class String –Part of the Java language –Don’t need to be imported

Creating a Java Application Program Syntax of a class Syntax of the main method

Programming Style and Form Know common syntax errors and rules Use blanks appropriately Semicolon: statement terminator Important to have well-documented code Good practice to follow traditional rules for naming identifiers

More on Assignment Statements variable = variable * (expression); is equivalent to variable *= expression; Similarly, variable = variable + (expression); is equivalent to: variable += expression;

Programming Examples Convert Length Program –Input: Length in feet and inches –Output: Equivalent length in centimeters Make Change Program –Input: Change in cents –Output: Equivalent change in half-dollars, quarters, dimes, nickels, and pennies

Chapter Summary Basic Elements of a Java program include: –The main method –Reserved words –Special symbols –Identifiers –Data types –Expressions –Input –Output –Statements

Chapter Summary To create a Java application it is important to understand: –Syntax rules –Semantic rules –How to manipulate strings and numbers –How to declare variables and named constants –How to receive input and display output –Good programming style and form