CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
The Java Programming Language
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Outline Java program structure Basic program elements
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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 (
DAT602 Database Application Development Lecture 5 JAVA Review.
Java: Chapter 1 Computer Systems Computer Programming II.
Java Language and SW Dev’t
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
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.
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.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
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.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 1.2 Introduction to C++ Programming
Working with Java.
Chapter 4 Assignment Statement
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2: Data Types, Variables, Operators, and Expressions
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
Statements, Comments & Simple Arithmetic
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to C++ Programming
Variables and Arithmetic Operators in JavaScript
Introduction to Java Programming
Variables ICS2O.
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Chapter 2: Java Fundamentals
Units with – James tedder
elementary programming
Anatomy of a Java Program
Focus of the Course Object-Oriented Software Development
Chapter 2: Introduction to C++.
CSE 142, Spring 2012 Building Java Programs Chapter 1
Zorah Fung University of Washington, Spring 2015
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Variables in C Topics Naming Variables Declaring Variables
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz

Semantic vs. Syntax Syntax: The formal rules for legal statements in the language. Semantics: The meaning of the statements - what happens when the statement is executed. Source: Here

Programming Errors Syntax Semantic (Logical) Run-Time Syntax errors are mistakes in the use of the Java language, and are analogous to spelling or grammar mistakes in a language like English. Common syntax errors include: leaving out a keyword Leaving out a ; putting a keyword in the wrong place leaving out a symbol, such as a colon, comma or brackets misspelling a keyword Program does not run until you fix these errors. Logical errors are the most difficult to fix. They occur when the program runs without crashing, but produces an incorrect result. Common logical errors include: using the wrong variable name getting operator precedence wrong making a mistake in a boolean expression off-by-one, and other numerical errors Program run until the end, but it gives wrong results. A problem which was not detected when the program was parsed, but is only revealed when a particular line is executed. Common run-time errors include: division by zero performing an operation on incompatible types accessing a list element, dictionary value or object attribute which doesn’t exist trying to access a file which doesn’t exist. Program is terminated unexpectedly while it is running. It does run all the code. Source: Here

Inputs We need to store the data we get for the problem into different locations under different names. Data are stored inside computer’s memory . You can imagine the memory as a group of locations, where each location has 4 features: Name. Data Type Value Address

Identifiers Identifiers are names of various program elements in the code that uniquely identify the elements. They are the names of things like variables or functions to be performed. They're specified by the programmer and should have names that indicate their purpose.

Identifiers Rules Are made of letters, digits and underscores. Must begin with a letter or an underscore Does not have a space Can take any length. Not a reserved word (keywords). It is case sensitive. Use meaningful name.

Names Conventions Identifiers should adhere to standard conventions. Variable names, for example, should begin with a lowercase letter. Names of constants are usually all capital letters. Class names always start with capital letters Object, method and variable names use mixed case: The first letter is lowercase If an identifier contains more than one word, each subsequent word begins with an uppercase letter

Keywords keyword: An identifier that you cannot use, because it already has a reserved meaning in the Java language. Complete list of Java keywords: abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

Summary of the Primitive Types A data type is defined by a set of values called the domain and a set of operations. The following table shows the data domains and common operations for all eight of Java’s primitive types: Type Domain Common operations The arithmetic operators: byte 8-bit integers in the range –128 to 127 + add * multiply - subtract / divide short 16-bit integers in the range –32768 to 32767 % remainder 32-bit integers in the range –2146483648 to 2146483647 The relational operators: int = = equal to != not equal 64-bit integers in the range –9223372036754775808 to 9223372036754775807 < less than <= less or equal long > greater than >= greater or equal 32-bit floating-point numbers in the range ± 1.4 x 10-45 to ± 3.4028235 x 10-38 float The arithmetic operators except % 64-bit floating-point numbers in the range ± 4.39 x 10-322 to ± 1.7976931348623157 x 10308 The relational operators double 16-bit characters encoded using Unicode The relational operators char The logical operators: boolean the values true and false && add || or ! not

Strings and string literals string: A sequence of text characters (not just letters) that can be printed or manipulated in a program. literal: a representation of a value of a particular type String literals in Java start and end with quotation mark characters "This is a string"

Variable Declarations In Java, you must declare a variable before you can use it. The declaration establishes the name and type of the variable and, in most cases, specifies the initial value as well. A variable must be declared before it is used in any other statement A variable must be initialized (assigned a value) before it is used in an expression Declaration and initialization can occur at the same time: int age = 21; Or in separate lines of code: char mInitial; mInitial = ‘M’;

Variable Declarations type name = value; The most common form of a variable declaration is where type is the name of a Java primitive type or class, name is an identifier that indicates the name of the variable, and value is an expression specifying the initial value. Most declarations appear as statements in the body of a method definition. Variables declared in this way are called local variables and are accessible only inside that method. Variables may also be declared as part of a class. These are called instance variables and are covered in Chapter 9.

Named constants A variable is a named memory location that can hold a value of a specific data type; as we have seen, the value stored at this location can change throughout the execution of a program If we want to maintain a value in a named location, we use the Java keyword final in the declaration and immediately assign the desired value; with this mechanism, we declare a named constant. Some examples: final int LUCKY = 7; final double PI = 3.14159; final double LIGHTSPEED = 3.0e10.0 ;

Expressions, Statement and Block An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. 1 * 2 * 3 Flag1==Flag2 T > 5 Source

Expressions, Statement and Block Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;). Assignment expressions Any use of ++ or -- Method invocations Object creation expressions Types of Statement. Expression statements. Declaration statements and  Control flow statements. Source

Expressions, Statement and Block Expression statements. // assignment statement aValue = 8933.234; // increment statement aValue++; // method invocation statement System.out.println("Hello World!"); // object creation statement Bicycle myBike = new Bicycle(); A declaration statement declares a variable. // declaration statement double aValue = 8933.234; Control statement: Will be covered later. Source

Expressions, Statement and Block A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example, BlockDemo, illustrates the use of blocks: class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one else { // begin block 2 System.out.println("Condition is false."); } // end block 2 } Source

Comments In this class: You will loose 10 points in this class for every assignment that you failed to write proper comments for it. Check the following slides to see the requirements of the comments.

Comments Comments can be put in many standard places. Most all programs have a "comment header" at the top of each file, naming the author and explaining what the program does. Most programmers also place a comment at the start of every method, describing the method's behaviour. Lastly, we can use comments inside methods to explain particular pieces of code. Comments provide important documentation. At this stage in our learning, it is not very useful to write comments, because we only know println statements. More complicated programs span hundreds or thousands of lines, and it becomes very difficult to remember what each method is doing. Comments provide a simple description. When multiple programmers work together, comments help one programmer understand the other's code.

Comments If we have // anywhere on a line, everything following this on the line is a comment – ignored. For more than a line, put your comments between /* Put your comments here */

Comments example /* Olivia Scott CS 180, Spring 2017 1/25/2017 This program prints a hello message */ public class PartOfSong { /* Runs the overall program to print the hello word on the console. */ public static void main(String[] args) { // print hello message System.out.println(“Hello”); }