Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,

Slides:



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

Chapter 1: Computer Systems
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
The Java Programming Language
Outline Java program structure Basic program elements
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.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
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
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
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.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Working with Java.
Chapter 4 Assignment Statement
Lecture 2: Data Types, Variables, Operators, and Expressions
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
null, true, and false are also reserved.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 2: Basic Elements of Java
Chapter 1: Computer Systems
Expressions and Assignment
elementary programming
Focus of the Course Object-Oriented Software Development
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Review… Yong Choi BPA CSUB

Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword, public is called an access modifier and Java code usually begin with an access modifier –public, indicates that this code can be accessed by all objects and can be extended, or used, as a basis for another class. (opposite to private) –If you omit the keyword, public, you limit the access to this class - private

Class Name The access modifier is followed by the word, class, and the class name. The class name (“HelloWorldApp”) is assigned by a programmer. It should be a user-friendly word that is not on the list of key words.list of key words By using understandable and user-friendly word for class, objects, and variables, you can not only avoid confusions but also increase understandability of your program. –class X vs. class GrandTotal

A Key (reserved) words list abstract boolean break byte case catch char class const continu e default do int interface long native new package private protected public return short static double else extends final finally float for goto if implements import instanceof strictfp super switch synchroniz ed this throw throws transient try void volatile while

Requirements for the Class Name A class name should begin with a letter of the alphabet. –includes any non-English letter, such as  or , an underscore, or a dollar sign. A class name can contain only letters, digits, underscore, or a dollar signs. A class name cannot be a Java programming key words such as public or class. A class name cannot be one of following values: true, false, or null.

More about Class Name It is a Java language industry standard to begin class names with an upper case letter and employ uppercase letters as need to improve readability. - ClassName The Java compiler expects the file name to match the same class name that you assigned at the beginning of your program. Java is case sensitive. – The compiler considers differently.

Using Curly Braces Programmers enclose the contents of all classes within curly braces ({ and }).curly braces For every opening curly brace in a java program, there must be a corresponding closing brace. The placement of the opening and closing curly braces is not important to the compiler.

Precedence of Arithmetic Operator OperatorMeaningprecedence -unary minushigest +unary plushigest *multiplicationmiddle /divisionmiddle %remaindermiddle + addition or concatenation low -subtractionlow

More Arithmetic Operators NEVER use the lower case 'l' because it is easily confused with a digit '1'. –123l (last one is L) vs. 1231

Declaring a Variable Java declaration: –Variable-Type Variable-Name Example of declaration: –float fltDollarAmt; –int intNum = 23; Multiple declarations of the same data type can be made in a single de declaration: –float fltDollarAmt, fltCurrBalance, fltNewTotal; Multiple declarations of the different data type can NOT be made in a single de declaration: –float fltDollarAmt, int intTotal; - incorrect

Syntax of Variable Declaration Start with lower case letter Remember: it’s case sensitive! –TOTAL and total are different names. Must start with a letter, dollar sign, or underscore –Do not start with a digit. Must contain only letters, dollar signs, underscores, or digits –Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. A name can not contain the space character.

Syntax of Variable Declaration A name can be any length. A name can not be a reserved word. A name must not already be in use in this part of the program. –Must not be a reserved word –“Camelback” naming style: COBOL: Current-Balance Java: currentBalance –Good idea to include data type in name: fltCurrentBalance An ending semicolon

Assignment Statements An assignment statement changes the value that is held in a variable. public class Example5 { public static void main ( String[] args ) { long payAmount; //a declaration without an initial value payAmount = 123; //an assignment statement System.out.println("The variable contains: " + payAmount ); }

Syntax of assignment Statements variableName = expression; –The equal sign "=" means "assignment operator. –variableName is the name of a variable that has been declared somewhere in the program. –expression is a collection of characters that calls for a value. –Errors may occur if the lefthand variable is not the same variable type that the righthand expression evaluates to.

Syntax of assignment Statements An assignment statement asks for the computer to perform two steps, in order: 1.Evaluate the expression (i.e., calculate a value.) 2.Store the value in the variable. For example, the assignment statement: sum = ; asks for two actions: 1.Evaluate the Expression — is calculated, yielding Puts the value (40) in the variable, which is sum.

The Assignment Operator We’ve already used this operator to initialize variables. –float fltCurrBalance = F; –fltNewTotal = fltCurrBalance; It can also be used to change the value of an existing variable.

Expressions An expression is a combination of literals, operators, variables, and parentheses used to calculate a value. This (slightly incomplete) definition needs some explanation: –literal — characters that directly mean a value, like: –operator — a symbol like plus ("+") or times ("*") that asks for doing arithmetic. –variable — a section of memory containing a value. –parentheses — "(" and ")". When the expression on the right gets complicated you need to know the two steps to figure out what happens.

Expressions (con’t) This might sound awful. Actually, this is stuff that you know from algebra, like: (32 - y) / ( x + 5 ), the character "/" means "division." Not just any mess will work (of course). The following: 32 - y) / ( x 5 + ) is not a syntactically correct expression. There are rules for this, but the best rule is that an expression must look OK as algebra.

Casting What happens when a numeric value is assigned into a numeric variable of unlike type? Double d int i i = 45; - OK because int to int d = i; - Ok because int to double (automatic conversion – see next slide). The 45.0 is stored in d. i = d; - Java will refuse to compile an assignment statement requiring narrowing conversion. - so need Casting

Casting Casting is the process of performing a deliberate change of data type. Java will automatically perform widening conversion. –fltCurrBalance = intLastBalance; –The integer will automatically be converted to floating point.

Casting One data type can be explicitly converted to another by a programmer. Double d int i i = (int) 3.14; - i equals 3 Must be careful to use!!

Casting Order of widening conversion: byte short int long float double

Increment Operator The increment operator ++ adds one to a variable. –counter = counter + 1 ; // add one to counter –Counter ++ Usually the variable is an integer type (byte, short, int, or long) but it can be a floating point type (float or double.) The two plus signs must not be separated. Usually they are written immediately adjacent to the variable.

How to Use Increment Operator The increment operator can be used as part of an arithmetic expression, as in the following: int sum = 0; int counter = 10; sum = counter++ ; System.out.println("sum: "+ sum " + counter: " + counter );

Example of Increment Operator The expression on the right of the = can be more complicated, as in the following fragment: int value = 10 ; int result = 0 ; result = value++ * 2 ; System.out.println("value: " + value + " result: " + result );

Example of Without Increment Operator The following is same as previous example. int value = 10 ; int result = 0 ; result = value * 2 ; value = value + 1 ; System.out.println("value: " + value + " result: " + result );

Expression of Increment Operator The increment operator must be applied to a variable. It cannot be applied to a larger arithmetic expression. The following is incorrect: int x = 15; int result; result = (x * 3 + 2)++ ; // Wrong!

Prefix Increment Operator The increment operator ++ can be put in front of a variable. When it is put in front of a variable (as in ++counter ) it is called a prefix operator. When it is put behind a variable (as in counter++ ) it is called a postfix operator. Both ways increment the variable. However: –++counter means increment before using. –counter++ means increment after using.

Decrement Operator The operator -- is a postfix and a prefix decrement operator. The postfix operator decrements a variable after using its value; the prefix operator increments a variable before using its value. ExpressionOperationExampleResult x-- use the value, then subtract 1 int x = 10; int y; y = x-- ; x is 9; y is 10 --x subtract 1, then use the value int x = 10; int y; y = --x ; x is 9; y is 9

Example of Decrement Operator int x = 99; int y = 10; y = --x ; System.out.println("x: " + x + " y: " + y ); Advice for using Prefix and Postfix Increments and Decrements –Don’t use them always. –Sometimes they look too confusing!