1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Data Types and Expressions
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
Chapter 2 Data Types, Declarations, and Displays.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
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.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Primitive Variables.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
CSC 107 – Programming For Science. The Week’s Goal.
Primitive Variables.
A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 2. Program Construction in Java. 01 Java basics.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Building Java Programs Chapter 2
Java Programming Review 1
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Unit 3: Variables in Java
Building Java Programs Chapter 2
Names of variables, functions, classes
Chapter 2 Primitive Data Types and Operations
Building Java Programs
Variables and Constants
Presentation transcript:

1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while int float double main... Two ids, Tax and Rate Can’t be used as identifiers They are reserved word $ “ ;,... No special chars No numbers 1 123

2 A variable: A name of a location in memory to hold value A value of what? int a; String A; 245  

3 Text String TextTel: EBCDICE385937A40F2 F8F7F2F4F9 ASCII54656C2A Binary:

4 ASCII 7-bit code 7-bit code American National Standard Code for Information Interchange Arabic ASCII BinaryHex Char ASCII BinaryHex a b c …………… A B C ….…….… : A #

5 BitsMinimumMaximum Integers Short Signed Unsigned0255 Integer Signed ,76832,767 Unsigned065,535 Long Signed 32 -2,147,483,6482,147,483,647 Unsigned04,294,967,295 BitsRange Floating Points Single   Double   Numbers inside Computers short Java’s Types int long float double

6 8 primitive data types in Java short i,j; int no; long sum; float total; double tax; char grade; byte b; boolean yes_or_no; i = 3; j = 3.14; tax = 8.25/100; grade = ‘A’; grage = ‘B’; grade = 97; yes_or_no = true; yes_or_no = false; Every variable has to be declared before we use it.

7 Variables and Assignments L-value = R-value Assignment operator Everything that can receive a value Everything that can give a value variable function expression literal The data types in both sides have to be consistent. If not, type-casting will occur.

8 Assignment examples L-value = R-value total = i+j; average = total/2; sum = average*10*tax_rate; i = gcd(24,18); illegal 3 = j; gcd(24,18) = 2+3; i+1 = j+3; (3+7)*2-4-2*5;i = 3/2-0.5;i = i = 3/ ; i+2;i = JOptionPane.showInputDialog("Input y") i = Integer.parseInt( );

9 More on Assignments L-value = R-value This operation itself will give a value L-value = R-value L-value = a = b = c = i+j+3+gcd(15,6); illegal: a = b+1 = c = i+j+3+gcd(15,6);

10 Arithmetic operators in Java +-*/% (x % y) is the remainder of x divided by y Relational and logical operators in Java Resulting true or false +-*/% (x % y) is the remainder of x divided by y >===!= && ||! &|^ (20 < y) && (x <= i)

11 Logical Operators (1 || 0) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x = 5) (((x % 2) == 0) && ((x % 3) == 0)) || && ! Assume x = 10 true false

12 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F

13 The Syntax of if and if/else statements if ( condition ) { statement list; } if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if/else statement. Reserved words A Boolean expression (logical expression). In Java, 0 is false, any non-zero values will be considered as true. true false A reserved word can’t be used as an identifier

14 Example double Cash=0; Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ")); if (Cash <= 0) JOptionPane.showMessageDialog(null, "You don't have any money!"); Price = (2.99*5)*(1+0.08); if ((Cash-Price) < 0) JOptionPane.showMessageDialog(null, "You don't have enough money!"); else JOptionPane.showMessageDialog(null, "Your change: "+ (Cash-Price)); Initialize Cash to value 0;

15 A compound statement: a group of statements double Cash=0; Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ")); if (Cash <= 0) JOptionPane.showMessageDialog(null, "You don't have any money!"); Price = (2.99*5)*(1+0.08); if ((Cash-Price) < 0) { JOptionPane.showMessageDialog(null, "You don't have enough money!"); JOptionPane.showMessageDialog(null, "Bring more money!"); } else JOptionPane.showMessageDialog(null, "Your change: "+ (Cash-Price));

16 Save some time: avoid doing the same thing twice double Cash=0; double Change; Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ")); if (Cash <= 0) JOptionPane.showMessageDialog(null, "You don't have any money!"); Price = (2.99*5)*(1+0.08); Change = Cash-Price; if (Change < 0) { JOptionPane.showMessageDialog(null, "You don't have enough money!"); JOptionPane.showMessageDialog(null, "Bring more money!"); } else JOptionPane.showMessageDialog(null, "Your change: "+ (Change));