Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 Chapter 2: Elementary Programming Shahriar Hossain.
Introduction to Java Programming, 4E Y. Daniel Liang.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
Programming with Java Instructor : Neelima Gupta.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Programming Style and Documentation Objective(s) F To become familiar with Java Style and Documentation Guidelines.
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.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
S.W.Ma/CIM/LWL41211/2 Prog. IIA Page 1 Lecture 2&3 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables,
Chapter 2 Elementary Programming
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.
Liang, Introduction to Java Programming1 Primitive Data Types and Operations Gang Qian Department of Computer Science University of Central Oklahoma.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Topic 2: Input / Output Formatting Lecturer:
Chapter 2 Elementary Programming 1. Motivations 2 In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Topic 2 Elementary Programming
Unit 2 Elementary Programming
Elementary Programming
Lecture 3: Operators, Expressions and Type Conversion
Chapter 2 Elementary Programming
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Elementary Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Chapter 2 Elementary Programming
elementary programming
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive Data Types –Byte, short, int, long, float, double, char, boolean F Expressions F Operators, Precedence, Associativity, Operand Evaluation Order: ++, --, *, /, %, +=, -=, *=, /=, %=, ^, &, |, +, -, F Style and Documentation F Syntax Errors, Runtime Errors, and Logic Errors

Introducing Programming with an Example Example 2.1 Computing the Area of a Circle This program reads the radius from the keyboard and computes the area of the circle. ComputeAreaRun

The MyInput Class MyInput This class contains the methods for reading an int, a double, or a string from the keyboard. The methods are readInt, readDouble, and readString

Identifiers F An identifier must start with a letter, an underscore, or a dollar sign. F An identifier cannot contain operators, such as +, -, and so on. F An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words).  An identifier cannot be true, false, or null. F An identifier can be of any length.

Variables // Compute the first area radius = 1.0; area = radius*radius* ; System.out.println("The area is “ + area + " for radius "+radius); // Compute the second area radius = 2.0; area = radius*radius* ; System.out.println("The area is “ + area + " for radius "+radius);

Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable;

Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;

Declaring and Initializing in One Step F int x = 1; F double d = 1.4; F float f = 1.4; Is this statement correct?

Constants final datatype CONSTANTNAME = VALUE; final double PI = ; final int SIZE = 3;

Numerical Data Types byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits double 64 bits

Number Literals F int i = 34; F long l = ;  float f = 100.2f; or float f = 100.2F;  double d = 100.2d or double d = 100.2D;

Operators +, -, *, /, and % 5/2 yields an integer /2 yields a double value % 2 yields 1 (the remainder of the division)

Shortcut Operators OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8

Increment and Decrement Operators F x = 1; F y = 1 + x++; F y = x; F y = 1 + x--; F y = x; Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i.

Assignment Expressions and Assignment Statements Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expressions can be statements: variable op= expression; // Where op is +, -, *, /, or % ++variable; variable++; --variable; variable--;

Numeric Type Conversion Consider the following statements: byte i = 100; long l = i*3+4; double d = i*3.1+l/2; int x = l; (Wrong) long l = x;(fine,implicit casting)

Type Casting F double F float F long F int F short  byte

Type Casting, cont. Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) What is wrong?int x = 5/2.0;

Character Data Type char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter = '\u000A'; (Unicode) Special characters char tab = ‘\t’;

Unicode Format Description Escape Sequence Unicode Backspace \b\u0008 Tab \t\u0009 Linefeed \n\u000a Carriage return \r\u000d

The boolean Type and Operators boolean lightsOn = true; boolean lightsOn = false; F && (and) (1 < x) && (x < 100)  || (or) (lightsOn) || (isDayTime) F ! (not)!(isStopped)

The & and | Operators If x is 1, what is x after this expression? (1 > x) & ( 1 > x++) If x is 1, what is x after this expression? (1 > x) && ( 1 > x++) How about (1 = x) | (1 > x++)? (1 = x) || (1 > x++)?

Operator Precedence F var++, var—  ++var, --var F Casting F !  *, /, %  +, - , >=  ==, !=; F & F ^ F | F && F ||  =, +=, -=, *=, /=, %=

Operator Associativity When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation. All binary operators except assignment operators are left-associative. a + b – c – d is equivalent to ((a – b) + c) – d Assignment operators are right-associative. Therefore, the expression a = b += c = 5 is equivalent to a = (b += (c = 5))

Operand Evaluation Order The precedence and associativity rules specify the order of the operators, but do not specify the order in which the operands of a binary operator are evaluated. Operands are evaluated from left to right in Java. The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated.

Operand Evaluation Order, cont. If no operands have side effects that change the value of a variable, the order of operand evaluation is irrelevant. Interesting cases arise when operands do have a side effect. For example, x becomes 1 in the following code, because a is evaluated to 0 before ++a is evaluated to 1. int a = 0; int x = a + (++a); But x becomes 2 in the following code, because ++a is evaluated to 1, then a is evaluated to 1. int a = 0; int x = ++a + a;

Programming Style and Documentation F Appropriate Comments F Naming Conventions F Proper Indentation and Spacing Lines F Block Styles

Appropriate Comments Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instruction, date, and a brief description at the beginning of the program.

Naming Conventions F Choose meaning and descriptive names. F Variables and method names: –Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea.

Naming Conventions, cont.  Class names: –Capitalize the first letter of each word in the name. For example, the class name ComputeArea. F Constants: –Capitalize all letters in constants. For example, the constant PI.

Proper Indentation and Spacing  Indentation –Indent two spaces. F Spacing –Use blank line to separate segments of the code.

Block Styles  Use next-line style for braces.

Programming Errors F Syntax Errors –Detected by the compiler F Runtime Errors –Causes the program to abort F Logic Errors –Produces incorrect result

Example 2.2 Computing Mortgages ComputeMortgageRun This program lets the user enter the interest rate, number of years, and loan amount and computes monthly payment and total payment.

Example 2.3 Computing Changes This program lets the user enter the amount in decimal representing dollars and cents and output a report listing the monetary equivalent in single dollars, quarters, dimes, nickels, and pennies. Your program should report maximum number of dollars, then the maximum number of quarters, and so on, in this order. ComputeChangeRun