Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.

Slides:



Advertisements
Similar presentations
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
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.
COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Basic Elements of C++ Chapter 2.
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
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.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Basic Computation Module 2. Objectives Describe the Java data types used for simple data Write Java statements to declare variables, define named constants.
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.
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.
Chapter 2 Elementary Programming
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2: Java Fundamentals
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
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.
Primitive Types, Strings, and Console I/O Chapter 2.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank M. Carrano. ISBN © 2009 Pearson Education, Inc.,
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 21 Primitive Types, Strings, and Console I/O Recitation Week 2.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank M. Carrano. ISBN © 2008 Pearson Education, Inc.,
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Expressions and Assignment
Basic Computation Chapter 2.
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Variables.
Chapter 2: Java Fundamentals cont’d
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions

Computer Programming with Java2 Contents Variables Primitive Types Assignment Statements Constants Arithmetic Operators The String class Documentation Keyboard and Screen I/O

Computer Programming with Java3 Variables Definition Variables in a program are used to store data such as numbers and letters. Values are the number or letter or other data item in a variable A variable can have its value changed. Identifiers are the variable names and must satisfy the spelling rules in Chapter 1.

Computer Programming with Java4 public class EggBasket { public static void main(String[] args) { int numberOfBaskets, eggsPerBasket, totalEggs; System.out.println("Enter the number of eggs in each basket:"); eggsPerBasket = SavitchIn.readLineInt(); System.out.println("Enter the number of baskets:"); numberOfBaskets = SavitchIn.readLineInt(); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); eggsPerBasket = eggsPerBasket - 2; totalEggs = numberOfBaskets * eggsPerBasket; … Display 2.1: A Simple Java Program

Computer Programming with Java5 Primitive Types Primitive Data Types integers: byte, short, int, long e.g.) 0, 1, -1, 2, -2 floating point: float, double e.g.) 9.9, , others: char, boolean e.g.) char char symbol; symbol= ‘A’; e.g.) boolean true or false

Computer Programming with Java6

7 Assignment Statements Assignment operator Variable = Expression or Values; e.g.) amount = 3.99; score = numberOfCards + handicap; Declaration and Initialization Specialized assignment operators +=, -=, /=, *=, %= e.g.) amount += 5; equals to amount = amount + 5;

Computer Programming with Java8 Assignment Statements (1) Assignment Compatibilities Principle of Compatibility you cannot put a value of one type in a variable of another type. you can assign a value of any type on the following list to a variable of any type that appears further down the list byte -> short -> int -> long -> float -> double if you want to assign a value of type double to a variable of type int, you must change the type of the value using a type cast

Computer Programming with Java9 Assignment Statements (2) Type Casting the changing of the type of a value from its normal type to some other type. (Type_Name) Expression e.g.) double guess = 7.8; int answer = (int) guess; (correct) double distance = 9.0; int points = distance; (incorrect)

Computer Programming with Java10 Constants Number constants It is literal values like 2 or 3.7 and its value do not change in a Java program. integer e.g.) 2, 3, 0, -3, 752, +12 (correct) 1,000 (incorrect) floating-point e.g.) 2.1, , 10 8 : or 8.65e8

Computer Programming with Java11 Constants (1) Named Constants a name be defined for constant Type Variable = Constant; e.g.) double PI = ;

Computer Programming with Java12 public class CircleCalculation { public static void main(String[] args) { double radius; //in inches double area; //in square inches System.out.println("Enter the radius of a circle in inches:"); radius = SavitchIn.readLineDouble(); area = * radius * radius; System.out.println("A circle of radius " + radius + " inches"); System.out.println("has an area of " + area + " square inches."); } Display 2.9: Comments and Indenting

Computer Programming with Java13 Arithmetic Operators Basic Operators +, -, *, / it can used to combine variables and/or numbers Arithmetic Expression

Computer Programming with Java14 Arithmetic Operators (1) Increment and Decrement Operators it can be used to increase or decrease the value of a variable by one. ++, -- e.g.) count = count + 1; count++; count = count - 1; count--;

Computer Programming with Java15 Arithmetic Operators (2) Precedence Rules (pp ) when the computer is deciding which of two operators to perform first and the order is not dictated by parentheses, then it does the operator of higher precedence before the operator of lower precedence. Some operators have equal precedence, in which the order of operations is determined by the left-to-right order of the operators.

Computer Programming with Java16 Higher Precedence First: the unary operators: +, -, ++, --, ! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Fourth: the boolean operators:, = Fifth: the boolean operators: ==, != Sixth: the boolean operators: & Seventh: the boolean operators: | Eighth: the boolean operators: && Ninth: the boolean operators: || Lowest Precedence Display 3.13: Precedence Rules

Computer Programming with Java17 The String Class String class it can be used to store and process strings of characters. String methods see text pp zero-based index String tmp = “Java is fun.”; char tmp_len = tmp.length();

Computer Programming with Java18 public class StringDemo { public static void main(String[] args) { String sentence = "Text processing is hard!"; int position; position = sentence.indexOf("hard"); System.out.println(sentence); System.out.println(" "); System.out.println("The word \"hard\" starts at index " + position); sentence = sentence.substring(0, position) + "easy!"; System.out.println("The changed string is:"); System.out.println(sentence); } Display 2.7: Using the String class

Computer Programming with Java19 The String Class (1) Escape characters it escape from the usual meaning of a character. \”: double quote, \’: single quote, \\: backslash, \n: new line, \r: carriage return, \t: tab e.g.) The word “hard” starts at index 19 System.out.println(“The word “hard” starts at index “+19); -> “The word” //incorrect System.out.println(“The word \”hard\” starts at index “+19); //correct

Computer Programming with Java20 Documentation Documentation style Truth on S/W development A Project: Development (20~30%), Maintenance (70~80%) if the program is not easy to read and understand, it will not be easy to maintain it. Rule of thumb Use meaningful names for variables Self-documenting thanks to a very clean style and very well-chosen variable names (and other names)

Computer Programming with Java21 Documentation (1) Comments //, /*…*/: Things written into your program that help a person understand the program, but that are ignored by the compiler. e.g.) String sentence; //Spanish version /*This program should only be used…. */ Indenting

Computer Programming with Java22 Keyboard and Screen I/O I/O input and output of program data Screen Output System.out.{println, print}() e.g.) System.out.println(“Enter the number of eggs..:”); System.out.println(“Lucky number = “+13+”Secret number = “+ number);