A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]

Slides:



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

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 Chapter 2: Elementary Programming Shahriar Hossain.
Introduction to Java Programming, 4E Y. Daniel Liang.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
Programming with Java Instructor : Neelima Gupta.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Unit 2 Elementary Programming In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 02 Title: Elementary Java Programming Reference: Farrell Ch 1, Liang Ch 2.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
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.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Liang, Introduction to Java Programming1 Primitive Data Types and Operations Gang Qian Department of Computer Science University of Central Oklahoma.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Topic 2: Input / Output Formatting Lecturer:
Chapter 2 Elementary Programming 1. Motivations 2 In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2 Elementary Programming
Unit 2 Elementary Programming
Chapter 2 Elementary Programming
Lecture 3: Operators, Expressions and Type Conversion
Multiple variables can be created in one declaration
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Numerical Data Types.
Chapter 2 Elementary Programming
elementary programming
Anatomy of a Java Program
Chapter 2 Primitive Data Types and Operations
Chapter 2: Beginning to Program
Presentation transcript:

A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); }}

Creating, Compiling, and Running Programs

More on Compiling and Execution  When saving source code, file name and class name should be the same:  class My1stJava{ } -> My1stJava.java  Any Java class that wishes to be executed on its own, needs to have a “main” method:  public static void main(String[] args) {…..}

More on Compiling and Execution cont.  To compile, type javac My1stJava.java  If no error, a My1stJava.class is created for you. This is your byte code that can be executed anywhere that has a JVM installed  To run, type java My1stJava. JVM will look for My1stJava.class for execution

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); }} Trace a Program Execution Enter main method

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); }} Trace a Program Execution Execute statement

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); }} Trace a Program Execution print a message to the console

Variable Declaration  Name  Data type  Initialize with value double radius = 2.0;

Numerical Data Types

Numeric Operators

Integer Division +, -, *, /, and % 5 / 2 yields an integer / 2 yields a double value % 2 yields 1 (the remainder of the division)

Remainder Operator Remainder is very useful in programming. For example: - Determine whether a number is even or odd. - Determine position within a cycle

Floating-Point Numbers are Approximated Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example, System.out.println( ); displays , not 0.5, and System.out.println( ); displays , not 0.1.

Number Literals A number literal is a notation that we use to represent values int i = 34; long x = ; double d = 5.5;

Default Numeric Data Type  In Java, an integer (e.g. 1 or 2,147,483,647) literal is by default a type int.  Floating-point literals are written with a decimal point (e.g. 5.0, ). By default, a floating- point literal is treated as a type double.

Choosing Other then Default Data Type One can choose a data type other then the defaults to hold values represented by their number literals: - 5L will force Java to store the value of 5 using long (64bit) instead of int (32bit) -5.0F will force Java to store the value represented by the literal 5.0 using float (32bit) instead of double (64bit)

Scientific Notation Floating-point literals can also be specified in scientific notation, for example, e+2, same as e2, is equivalent to , and e-2 is equivalent to E (or e) represents an exponent and it can be either in lowercase or uppercase.

Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

Shortcut Assignment Operators OperatorExampleEquivalent +=i += 8i = i + 8 -=f -= 8.0f = f *=i *= 8i = i * 8 /=i /= 8i = i / 8 %=i %= 8i = i % 8

Increment and Decrement Operators OperatorNameDescription ++varpreincrementThe expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++postincrementThe expression (var++) evaluates to the original value in var and increments var by 1. --varpredecrementThe expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var--postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.

Increment and Decrement Operators, cont.

Numeric Type Conversion Consider the following expressions: 25/425/ *8

Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int.

Operator-Operand Defined  x = 10 * 16  Variable = operand_1 operator operand_2

Type Casting Implicit casting double d = 3; (type widening) double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) int i = (int)3.9; (Fraction part is truncated) What is wrong?int x = 5 / 2.0;

Character Data Type char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = 'a'; char ch = 'a'; System.out.println(++ch); System.out.println(++ch);

Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace \b\u0008 Tab \t\u0009 New line \n\u000A Carriage return \r\u000D Backslash \\\u005C Single Quote \ ' \u0027 Double Quote \ " \u0022

Appendix B: ASCII Character Set ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

Casting between char and Numeric Types int i = ' a ' ; // Same as int i = (int) ' a ' ; char c = 97; // Same as char c = (char)97;

Outputting to Screen double radius = 2.0;  Text strings in “” are printed exactly  System.out.println(“radius”); => radius  Text strings without “” are interpreted as variable identifiers  System.out.println( radius ); => 2.0  + sign is used to combined together text string output with variable value  System.out.println(“Length of radius is: “+radius+” in.”);  Length of radius is : 2.0 in.

Programming Style and Documentation  Appropriate Comments  Naming Conventions  Proper Indentation and Spacing Lines  Block Styles

Appropriate Comments Summary of: - Program ’ s function - Algorithm used for implementation - Type of input - Type of output if any

Naming Conventions  Choose meaningful and descriptive names.  Variables and method names:  Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea.

Naming Conventions, cont.  Class names:  Capitalize the first letter of each word in the name. For example, the class name ComputeArea.  Constants:  Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE

Proper Indentation and Spacing  Indentation  Indent two spaces.  Spacing  Use blank line to separate segments of the code.

public class ComputeArea { public static void main(String[] args) { final static double PI = ; double radius; double area; radius = 20; area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + " is " + area); }}

public class ComputeArea { /** Main method **/ /** Main method **/ public static void main(String[] args) { public static void main(String[] args) { final static double PI = ; final static double PI = ; double radius; double radius; double area; double area; // Assign a radius // Assign a radius radius = 20; radius = 20; // Compute area // Compute area area = radius * radius * PI; area = radius * radius * PI; // Display results // Display results System.out.println("The area for the circle of radius " + System.out.println("The area for the circle of radius " + radius + " is " + area); radius + " is " + area); }}

Misc.  Words are case sensitive  All statements must be closed with a semicolon “;”