1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar.

Slides:



Advertisements
Similar presentations
Chapter 2: Java Fundamentals cont’d
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
Class 7.
Lecture 2 Basics of Java Programming Language Slides produced by Antonio Martinez Java Programming: From the Ground Up.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
CMT Programming Software Applications
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.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
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.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to 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.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 2: Java Fundamentals
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
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[]
Java Fundamentals Part Topics –Simple java program –The print and println Methods –Java API –Variables and Literals –Primitive Data Types –Arithmetic.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
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,
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
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.
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 2 Variables.
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
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
2.1 Parts of a C++ Program.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar

2 Outline  2.1 The Parts of a Java Program  2.2 The print and println Methods, and the Java Standard Class Library  2.3 Variables and Literals  2.4 Primitive Data Types  2.5 Arithmetic Operators  2.6 Combined Assignment Operators  2.7 Conversion Between Primitive Types  2.8 Creating Named Constants with final  2.9 The String Class  2.10 Scope  2.11 Comments  2.12 Programming Style  2.13 Reading Keyboard Input  2.14 Dialog Boxes  2.15 Common Errors to Avoid

3 Review  Parts of the program Comments: start with //  Are ignored by the compiler Class header : public class Name Method header: public static void main( String args[] ) Statements

4 Review  System.out.print: displays on the screen what we put in “”  System.out.println: same as print except that it moves the cursor to a new line after displaying the text

5 Review Moves the cursor to \n a new line \t the next tab stop \b one character back \r To the beginning of the line \\ Prints a backslash \’ Prints a single quote \” Prints a double quotation mark

6 Review  Variables: Declare: type and identifier Initialize: give it value ~ literal  Literals:  Identifiers: Start with letter, _, or $ Have only numbers, letters, _ and $ Cannot be a keyword

7 Printing a variable  2 ways: On its own: System.out.println( identifier); Combined with text: System.out.println( “The result is: “+ identifier );

8 Example // This program has a variable. public class Variable { public static void main(String[] args) { int value; value = 5; System.out.print("The value is "); System.out.println(value); }

9 2.4 Primitive Data Types  Each variable has a data type which is the type of data that can be stored in the memory location allocated  Here is a list of primitive(numeric) data types:

Primitive Data Types cont’d Data type SizeRange byte1 byteInteger between -128 to 127 short2 bytesInteger between to int4 bytesInteger between to long8 bytesInteger between to float4 bytesFloating point numbers with 7 digits of accuracy double8 bytesFloating point numbers with 15 digits of accuracy No digits after decimal point 15 digits after decimal point 7 digits after decimal point

11 Declaration revisited  Datatype Variablename;  Examples: int hours; byte minutes; short month; float average; double distance

12 The Integer Data Types // This program has variables of several of the integer types. public class IntegerVariables{ public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days. checking = -20; miles = 105; minutes = 120; days = ; System.out.println("We have made a journey of " + miles + " miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.println("About " + days + " days ago Columbus " + "stood on this spot."); } We have made a journey of 105 miles. It took us 120 minutes. Our account balance is $-20. About days ago Columbus stood on this spot. No Commas

13 The Floating Data Types // This program demonstrates the double data type. public class Sale { public static void main(String[] args) { double price, tax, total; price = 29.75; tax = 1.76; total = 31.51; System.out.println("The price of the item " + "is " + price); System.out.println("The tax is " + tax); System.out.println("The total is " + total); } The price of the item is The tax is 1.76 The total is 31.15

14 The Floating Data Types  float number = 23.5;  float number = 23.5f;  float number = 23.5F; ERROR CORRECT

15 The boolean Data Type  a boolean variable can have two values: true false  Example: boolean bool; bool = true; System.out.println(bool); bool = false; System.out.println(bool); true false

16 The char Data Type  used to store characters  character literals are enclosed in single quotes  Example: char letter; letter = 'A'; System.out.println(letter); letter = 'B'; System.out.println(letter); ABAB

17 The char Data Type  Characters are internally represented by numbers.  Java uses Unicode which is a set of numbers that are used as codes for representing characters  The codes are found in appendix A on the CD

18 Example char letter; letter = 65; System.out.println(letter) letter = 66; System.out.println(letter); ABAB

19 Variable Assignment and Initialization  assignment statement is used to put a value into a variable. Ex: x = 15;  always put the identifier on the left of the equal sign and the literal to the right of the equal sign. Ex: 15 = x; is incorrect  you can assign the value of one identifier to another. Ex: x = y; now x has the value stored in y

20 Valid Variable Declaration  int month =2, days = 28;  float distance = 35.2f;  int c = 8, y =10, x, v=2;

Arithmetic Operators  3 types of operator: unary: require a single operand.  There is one unary operand: -9 (the negation operator)  --5 is 5 binary: require two operands ternary: require three operands

22 Arithmetic operators Operator MeaningTypeExample +AdditionBinarytotal = cost+tax; -SubtractionBinarycost = total-tax; *MultiplicationBinaryTax = cost*rate; /DivisionBinarysaleprice=original/2 ; %ModulusBinaryremainder=value% 3;

23 Examples of Statements with Arithmetic Operators  amount = x + y;  amount = ;  Minutes = 800 – call;  Tip = bill * 0.18;  Slice = cake /8; Notice that the identifier is to the left of the = Notice that the arithmetic operator is to the right of the =

24 Example int counter = 0; //counter =0 counter = counter + 1; //counter =1 counter = counter + 39; //counter=40 counter = counter – 8; //counter=32 counter = counter * 5; //counter=160 counter = counter / 2; //counter= 80

25 The % operator  Returns the remainder of the division  Examples; 4%5 is 4 30%6 is 0 22%7 is %100 is %10 is 5

26 Exercise  Write the following in a Java file: double amount = 137/5; System.out.println(“Amount is : “ + amount ); amount = 137.0/5; System.out.println(“Amount is : “ + amount );

27 Integer Division  Dividing an integer by an integer gives an integer  the remainder is ignored  Examples: 5/4 is 1 17/3 is 5

28 Operator Precedence  What is the result of:  Polynomial = 1+2*3+ 6/2 -2;  Is it ? (1+2)*3 + 6/(2-2) 1+(2*3) +(6/2)-2 (1+2)*3 + (6/2)-2

29 Precedence Rules  Always evaluate *, / and % before + and –  Always negate before any calculations  *, / and % have same precedence  + and – have same precedence  If equal precedence then evaluate from left to right except for negations where we evaluate from right to left

30 Precedence examples  Polynomial = 1+2*3+ 6/2 – 2; Polynomial has the value of =8  Polynomial = –1 + 5 – 2; // 2  Polynomial = –(–3) + –(–5); //8

31 Grouping with parentheses  You can use parentheses to force the evaluation of a formula  Examples: x * ( y + z*z ) instead of x*y + z*z x * ( y * ( z ) + 85 ) – 65 Average = (a +b +c ) /3;

32 The Math class  value = Math.pow( x,y); // now value holds x to the power of y  value = Math.sqrt( x); //now value holds the square root of x

33 Combined Assignment Operators +=x += 1;x = x + 1; –=x –= 1;x = x – 1; *=x *= 1;x = x * 1; /=x /= 1;x = x / 1; %=x %= 1;x = x % 1;