Chapter 2: Basic Elements of Java

Slides:



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

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++
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of 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++
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++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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,
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
CompSci 230 S Programming Techniques
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of Java
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Basic Elements of Java
Computer Programming BCT 1113
Elementary Programming
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.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Chapter 2 Variables.
elementary programming
Chapter 2: Introduction to C++.
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 2: Basic Elements of Java Java Programming: From Problem Analysis to Program Design, Second Edition

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. Java Programming: From Problem Analysis to Program Design, Second Edition

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. Java Programming: From Problem Analysis to Program Design, Second Edition

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. Java Programming: From Problem Analysis to Program Design, Second Edition

Introduction Computer program: A sequence of statements designed to accomplish a task. Programming: The process of planning and creating a program. Java Programming: From Problem Analysis to Program Design, Second Edition

The Basics of a Java Program Java program: A collection of classes. There is a main method in every Java application program. Token: The smallest individual unit of a program. Java Programming: From Problem Analysis to Program Design, Second Edition

Special Symbols Java Programming: From Problem Analysis to Program Design, Second Edition

Word Symbols void int public float static double throws char return Java Programming: From Problem Analysis to Program Design, Second Edition

Java Identifiers Names of things. Consists of: Letters Digits The underscore character (_) The dollar sign ($) Must begin with a letter, underscore, or the dollar sign. Java Programming: From Problem Analysis to Program Design, Second Edition

Illegal Identifiers Java Programming: From Problem Analysis to Program Design, Second Edition

Data Types A set of values together with a set of operations. Java Programming: From Problem Analysis to Program Design, Second Edition

Primitive Data Types Java Programming: From Problem Analysis to Program Design, Second Edition

Primitive Data Types Floating-point data types: Boolean: Float: Precision = 6 or 7 Double: Precision = 15 Boolean: True False Java Programming: From Problem Analysis to Program Design, Second Edition

Integral Data Types Java Programming: From Problem Analysis to Program Design, Second Edition

Values and Memory Allocation for Integral Data Types Java Programming: From Problem Analysis to Program Design, Second Edition

Arithmetic Operators and Operator Precedence Five arithmetic operators: + addition - subtraction * multiplication / division % mod (modulus) Unary operator: An operator that has one operand. Binary operator: An operator that has two operands. Java Programming: From Problem Analysis to Program Design, Second Edition

Order of Precedence * / % (same precedence) + - (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. Java Programming: From Problem Analysis to Program Design, Second Edition

Expressions Integral expressions Floating-point or decimal expressions Mixed expressions Java Programming: From Problem Analysis to Program Design, Second Edition

Integral Expressions All operands are integers. Examples: 2 + 3 * 5 3 + x – y / 7 x + 2 * (y – z) + 18 Java Programming: From Problem Analysis to Program Design, Second Edition

Floating-Point Expressions All operands are floating-point numbers. Examples: 12.8 * 17.5 – 34.50 x * 10.5 + y - 16.2 Java Programming: From Problem Analysis to Program Design, Second Edition

Mixed Expressions Operands of different types. Examples: 2 + 3.5 6 / 4 + 3.9 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. Java Programming: From Problem Analysis to Program Design, Second Edition

Type Conversion (Casting) Used to avoid implicit type coercion. Syntax: (dataTypeName) expression Expression evaluated first, then type converted to: dataTypeName Examples: (int)(7.9 + 6.7) = 14 (int)(7.9) + (int)(6.7) = 13 Java Programming: From Problem Analysis to Program Design, Second Edition

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. Java Programming: From Problem Analysis to Program Design, Second Edition

Input Named constant Cannot be changed during program execution. Declared by using the reserved word final. Initialized when it is declared. Example 2-11 final double CENTIMETERS_PER_INCH = 2.54; final int NO_OF_STUDENTS = 20; final char BLANK = ' '; final double PAY_RATE = 15.75; Java Programming: From Problem Analysis to Program Design, Second Edition

Input 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. Example 2-12 double amountDue; int counter; char ch; int x, y; Java Programming: From Problem Analysis to Program Design, Second Edition

Input The Assignment Statement variable = expression; Example 2-13 int i, j; double sale; char first; String str; i = 4; j = 4 * 5 - 11; sale = 0.02 * 1000; first = 'D'; str = "It is a sunny day."; Java Programming: From Problem Analysis to Program Design, Second Edition

Input Standard input stream object is System.in. Input numeric data to program. Separate by blanks, lines, or tabs. To read data: Create an input stream object of the class Scanner. Use the methods such as next, nextLine, nextInt, and nextDouble. Java Programming: From Problem Analysis to Program Design, Second Edition

Input Example 2-16 static Scanner console = new Scanner(System.in); int feet; int inches; Suppose the input is 23 7 feet = console.nextInt(); //Line 1 inches = console.nextInt(); //Line 2 Java Programming: From Problem Analysis to Program Design, Second Edition

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-- Java Programming: From Problem Analysis to Program Design, Second Edition

Strings and the Operator + Operator + can be used to concatenate two strings, or a string and a numeric value or character. Example 2-20 String str; int num1, num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; After this statement executes, the string assigned to str is: "The sum = 1226"; Java Programming: From Problem Analysis to Program Design, Second Edition

Strings and the Operator + Example 2-20 String str; int num1, num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; After this statement executes, the string assigned to str is: "The sum = 1226"; Consider the following statement: str = "The sum = " + (num1 + num2); In this statement, because of the parentheses, you first evaluate num1 + num2. Because num1 and num2 are both int variables, num1 + num2 = 12 + 26 = 38. After this statement executes, the string assigned to str is: "The sum = 38"; Java Programming: From Problem Analysis to Program Design, Second Edition

Output Standard output object is System.out. Methods: Syntax: print println Syntax: System.out.print(stringExp); System.out.println(stringExp); System.out.println(); Java Programming: From Problem Analysis to Program Design, Second Edition

Commonly Used Escape Sequences Java Programming: From Problem Analysis to Program Design, Second Edition

Packages, Classes, Methods, and the import Statement Package: A collection of related classes. Class: Consists of methods. Method: Designed to accomplish a specific task. Java Programming: From Problem Analysis to Program Design, Second Edition

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. Java Programming: From Problem Analysis to Program Design, Second Edition

Creating a Java Application Program Syntax of a class: Syntax of the main method: Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Style and Form Know common syntax errors and rules. Use blanks appropriately. Use a semicolon as a statement terminator. Important to have well-documented code. Good practice to follow traditional rules for naming identifiers. Java Programming: From Problem Analysis to Program Design, Second Edition

More on Assignment Statements variable = variable * (expression); is equivalent to: variable *= expression; Similarly, variable = variable + (expression); variable += expression; Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Examples Convert Length program: Make Change 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. Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Summary Basic elements of a Java program include: The main method Reserved words Special symbols Identifiers Data types Expressions Input Output Statements Java Programming: From Problem Analysis to Program Design, Second Edition

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. Java Programming: From Problem Analysis to Program Design, Second Edition