Statements, Comments & Simple Arithmetic

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CMT Programming Software Applications
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2: Introduction to C++.
Introduction to C Programming
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™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Comments are for people Header comments supply basic information about the artifact.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
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.
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
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
By Mr. Muhammad Pervez Akhtar
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
C++ First Steps.
Introduction to Java Applications
Chapter 6 JavaScript: Introduction to Scripting
Working with Java.
GC211 Data Structure Lecture 1 Sara Alhajjam.
Chapter 2 - Introduction to C Programming
Lecture 2: Data Types, Variables, Operators, and Expressions
Chapter 2 Introduction to Java Applications
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
Introduction to Scripting
Introduction to Programming in Java
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Starting JavaProgramming
5 Variables, Data Types.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
T. Jumana Abu Shmais – AOU - Riyadh
Units with – James tedder
Units with – James tedder
Introduction to C++ Programming
elementary programming
Anatomy of a Java Program
Chapter 2: Introduction to C++.
Introduction to Java Applications
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Just Enough Java 17-May-19.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Statements, Comments & Simple Arithmetic 4 Statements, Comments & Simple Arithmetic

Previously What’s Java? Some history Characteristics Components Type of programs Development JVM OS Application Storage Library (y) Web Server Storage Browser Library (y) OS Servlet Applet applet Interpreted language, … JSE, JEE and JME JDK JVM, OS Type: Applications, Applets, Servlets and Libraries Development environment Eclipse

Overview Hello World! Reserved words & Identifiers Code Blocks Methods Statements Style & Comments Simple Arithmetic & Operators

Finally - a Java Program! class HiThere { /** * This is the entry point for an application. * @param astrArgs the command line arguments. */ public static void main(String[] astrArgs) { // Starting of the main method System.out.println("Hello World!"); } // main() } // end class HiThere

Reserved words & Identifiers class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); } // main() } // end class HiThere Reserved words or keywords are part of the language class used to define a class public access keyword - public, private, protected and ‘default’ static type related to belonging void special return type Java is case sensitive We represent reserved words in red text. Methods with the static keyword belong to the class when the keyword is not present then the method belong to the object (object is an instantiation of a class, holds the data with its values)

Reserved words & Identifiers HiThere is an identifier This is a name we make up to identify a component of the program (in this case a class) Must be a single word (no blank characters) Must start with a letter, ‘_’, ‘£’ or ‘$’ Following characters can also be numbers Cannot contain any white space

Reserved words & Identifiers The name of a classes always start in uppercase class HiThere The name of methods always start in lowercase public static void main(String[] astrArgs) The name of variables always start in lowercase The name of static final variables (‘constants’) always in uppercase private static final int SEC_PER_MINUTE = 60;

Reserved words & Identifiers A format to build identifiers Camel case – no word separator space, underscore Variable names prefix with their type (lowercase) public static void main(String[] astrArgs) array array of Strings array of Strings meaning word

Code Blocks class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); } // main() } // end class HiThere Braces ( i.e. { } ) delimit an isolated block of code All programs have several (usually many) blocks Braces must be balanced Braces are often nested Control flows use also braces to delimit the block

Methods class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); } // main() } // end class HiThere Methods contain blocks of functional code Methods are named by an identifier This is a method called main - applications execute their main method on starting (application entry point) NB the syntax of main must be exactly as shown

Statements class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); } // main() } // end class HiThere The program contains a single statement Statements are terminated by a semi-colon println This statement calls a library method called System.out.println Methods may be provided with an argument (data), which is contained in brackets

Statements The argument of println is a string class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); } // main() } // end class HiThere The argument of println is a string A string is a sequence of characters Java strings are delimited by double quotes

Style class HiThere { public static void main(String[] astrArgs) { System.out.println("Hello World!"); } // main() } // end class HiThere indents Line breaks and indents are ignored by the compiler, but help greatly with readability Use meaningful names

Comments class HiThere { public static void main(String[] astrArgs) { /** * This is the entry point for an application. * @param astrArgs the command line arguments. */ public static void main(String[] astrArgs) { // Starting of the main method System.out.println("Hello World!"); } // main() } // end class HiThere }

Comments Comments are ignored by the compiler Comments may be delimited by /* */ For several lines of comment A type of multiline comment is the documentation comments which start with /** and end with */ Comments may be delimited by // to the end of the line Single line comment

Arithmetic Arithmetic is accomplished by "numeric operators“ Binary Operators + addition - subtraction * multiplication / division % remainder int iNum = 0; iNum = 7 + 2; iNum = 7 – 2; iNum = 7 * 2; iNum = 7 / 2; iNum = 7 % 2; 9 5 14 3 1 3 http://download.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html 1 2 3 1

Arithmetic 9 5 14 3 1 Arithmetic Unary Operators += addition -= subtract *= multiply /= divide %= remainder iNum += 2; iNum -= 2; iNum *= 2; iNum /= 2; iNum %= 2; int iNum = 7; 9 5 14 3 1

Arithmetic System.out.print("The answer is "); System.out.println(2 + 2); class Arithmetic { /** * Adds two to two and displays the result. * @param astrArgs the command line arguments, not used. */ public static void main(String[] astrArgs) { System.out.print("The answer is "); System.out.println(2 + 2); } // main() } // end class Arithmetic The answer is 4

Operators Equality and Relational Operators == equal to if (iNum == 1) { != not equal to if (iNum != 1) { > greater than if (iNum > 1) { >= greater than or equal to if (iNum >= 1) { < less than if (iNum < 1) { <= less than or equal if (iNum == 1) { int iNum = 7;

Operators Assignment Operator Conditional Operators && conditional-AND || conditional-OR Type Comparison Operator instanceof compares an object to a specified type int iNum = 7; int iCount = 0; if (iNum == 7 && iCounter == 0) { true && true  true true && false  false false && true  false false && false  false true || true  true true || false  false false || true  true false || false  false ? : ternary (shorthand for if-then-else statement) <condition> ? <statement if true> : <statement if false> if (iNum == 7 || iCounter == 0) {

Documentation Statement and Blocks Operators http://download.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html Operators http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html Sun Certified Programmer for Java 5 - Study Guide

Lets look at HelloWorld again! Eclipse Lets look at HelloWorld again!