Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]"— Presentation transcript:

1 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!"); }}

2 Creating, Compiling, and Running Programs

3 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) {…..}

4 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

5 //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

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

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

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

9 Numerical Data Types

10 Numeric Operators

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

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

13 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(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1); displays 0.5000000000000001, not 0.5, and System.out.println(1.0 - 0.9); displays 0.09999999999999998, not 0.1.

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

15 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, 9800000000000.34). By default, a floating- point literal is treated as a type double.

16 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)

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

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

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

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

21 Increment and Decrement Operators, cont.

22 Numeric Type Conversion Consider the following expressions: 25/425/4.0+455*8

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

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

25 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;

26 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);

27 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

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

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

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

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

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

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

34 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

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

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

37 public class ComputeArea { /** Main method **/ /** Main method **/ public static void main(String[] args) { public static void main(String[] args) { final static double PI = 3.14159; final static double PI = 3.14159; 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); }}

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


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

Similar presentations


Ads by Google