Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.

Similar presentations


Presentation on theme: "CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz."— Presentation transcript:

1 CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz

2 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

3 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

4

5 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

6 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.

7 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.

8 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

9 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

10 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 – to The relational operators: int = = equal to != not equal 64-bit integers in the range – to < less than <= less or equal long > greater than >= greater or equal 32-bit floating-point numbers in the range ± 1.4 x to ± x 10-38 float The arithmetic operators except % 64-bit floating-point numbers in the range ± 4.39 x to ± 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

11 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"

12 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’;

13 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.

14 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 = ; final double LIGHTSPEED = 3.0e10.0 ;

15 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

16 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

17 Expressions, Statement and Block
Expression statements. // assignment statement aValue = ; // 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 = ; Control statement: Will be covered later. Source

18 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

19 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.

20 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.

21 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 */

22 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”); }


Download ppt "CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz."

Similar presentations


Ads by Google