Chapter 2: Java Fundamentals cont’d

Slides:



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

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2 JAVA FUNDAMENTALS CONT’D
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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
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.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2: Java Fundamentals
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
1 Operations Making Things Happen (Chap. 3) Expressions.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Introduction to Programming
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
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[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
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.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arithmetic Expressions
Elementary Programming
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Type Conversion, Constants, and the String Object
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Fundamentals 2.
Chapter 2: Java Fundamentals
Expressions and Assignment
elementary programming
CS2011 Introduction to Programming I Elementary Programming
Chapter 2: Java Fundamentals cont’d
Chapter 2: Beginning to Program
Presentation transcript:

Chapter 2: Java Fundamentals cont’d Lecture 4: Monday Sept 11, 2006

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

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

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

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

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 + 165 ) + 85 ) – 65 Average = (a +b +c ) /3;

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

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;

2.7 Conversion between Primitive Data Types Before a value is stored in a variable, Java checks the Data Type of the value and the variable If the data types are compatible then Java performs the conversion automatically  No Error If the data types are not compatible then Java issues an error.

2.7 Conversion between Primitive Data Types A widening conversion is the conversion of a small value to a larger one A narrowing conversion is the conversion of a large value to a smaller one double largest float long int short byte smallest

Widening conversion Example 1: Example 2: double x; int y = 10; x = y; int x; short y =2; x= y;

Narrowing Conversion We have to perform casting i.e. the name of the smaller data type is put in parentheses in front of the value Example: int number; double pi = 3.14; number = (int) pi;

Cast operator Used to convert from one primitive data type to another Must be used for narrowing conversions

Example: int pies = 10, people = 4; double piesPerPerson; piesPerPerson = pies /people; piesPerPerson =(double) pies/people; piesPerPerson =pies/(double) people; piesPerPerson=(double)(pies/people); (double)(10/4) = (double)(2) = 2.0 because it is an integer division 10/4 = 2 because it is an integer division 10.0/4 = 2.5 because one of the numbers is a double 10/4.0 = 2.5 because people is double

Mixed Integer Operations The result of an arithmetic operation that involves only byte, short, or int variables is always an int even if both variables are of data type short or byte Example: short x =5, y =7; short z = x+y; // this statement gives an error short z = (short) ( x+y ); //correct

Mixed Integer Operations If one of the operator’s operands is a double then the result of the operation is a double If one of the operator’s operands is a float then the result of the operation is a float If one of the operator’s operands is a long then the result of the operation is a long

Creating named constants with final A named constant is a variable whose value is read-only and cannot be changed To create a named constant add the word final to declaration An initialization value is required when declaring a constant Example: final double INTEREST_RATE = 0.069;

More about named constants When naming a constant, the variable name should be written in all uppercase characters. Math.PI is a constant that holds the value of pi ( i.e. 3.14159 …) Math.PI is already declared and initialized so it ready to use. Example: double area = Math.PI * radius * radius ;

The String class A String literal is any text enclosed in quotations A String is the DataType of a variable that can store String literals Example of a String variable: String name = “CS 0007”; System.out.println( name );

The String class To determine how many letters are stored in a String variable (name) use name.length(); Example: String mycourse = “CS 0007”; int number = mycourse.length();

String methods charAt(index) index is an integer and specifies the character position in the String This method returns the character at the specified position Example: char letter; String myText = “This is my Text”; letter = myText.charAt(8);

T h i s m y e x t String methods myText.length returns 15 because there are 15 characters T h i s m y e x t 1 2 3 4 5 6 7 8 9 10 11 12 13 14 myText.charAt(8) returns m because m is the letter at position 8

String methods toLowerCase() This method returns a new String that has all of the characters of the original String but in lowercase Example: String bigName = “I am BIG!!”; String smallName = bigName.toLowerCase(); // now smallName holds “i am big!!”

String methods toUpperCase() Same as toLowerCase() but it converts all the characters to uppercase Example: String smallName = “I am Big!!”; String bigName = smallName.toUpperCase(); // now bigName holds “I AM BIG!!”

Example: String message = "Java is Great Fun!"; String upper = message.toUpperCase(); String lower = message.toLowerCase(); char letter = message.charAt(2); int stringSize = message.length(); System.out.println(message); System.out.println(upper); System.out.println(lower); System.out.println(letter); System.out.println(stringSize);

Scope The variable scope is the part of the program that has access to it public class Scope { public static void main(String[] args) System.out.println(value); // ERROR! int value = 100; }

Scope public class Scope { public static void main(String[] args){ int number = 100; System.out.println(number); int number = 200; //ERROR }

Homework due Monday Sept 18 Do Exercise 2 in the programming challenges page 113 Print your code and submit it in class