Elementary Programming (Part 1) PDF
Document Details
Uploaded by TemptingAgate2936
Liang
Tags
Summary
This document is a guide to elementary programming concepts in Java, covering declarations, statements, expressions, and input/output. It includes example code snippets demonstrating programming constructs and principles.
Full Transcript
Elementary programming (Part 1) : Declarations, statements, Expressions, Input & Output A Sample Program Execution public class ComputeArea { public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // C...
Elementary programming (Part 1) : Declarations, statements, Expressions, Input & Output A Sample Program Execution public class ComputeArea { public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.print("The area for the circle of radius “); System.out.println(radius + " is " + area); } } A Sample Program Execution public class ComputeArea { public static void main(String[] args) { double radius; double area; Comments describe the // Assign a radius program for the radius = 20; programmer or the reader // Compute area area = radius * radius * 3.14159; // Display results System.out.print("The area for the circle of radius "); System.out.println(radius + " is " + area); } } A Sample Program Execution public class ComputeArea { public static void main(String[] args) { double radius; double area; Identifiers are // Assign a radius names of radius = 20; data and modules // Compute area area = radius * radius * 3.14159; // Display results System.out.print("The area for the circle of radius "); System.out.println(radius + " is " + area); } } Identifiers An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. An identifier cannot be true, false, or null. An identifier can be of any length. Variables/Identifiers // Compute the first area radius = 1.0; area = radius * radius * 3.14159; System.out.println("The area is " + area + " for radius "+radius); // Compute the second area radius = 2.0; area = radius * radius * 3.14159; System.out.println("The area is " + area + " for radius "+radius); A Sample Program Execution public class ComputeArea { public static void main(String[] args) { double radius; double area; // Assign a radius Keywords are radius = 20; predefined, reserved // Compute area words. area = radius * radius * 3.14159; // Display results System.out.print("The area for the circle of radius “); System.out.println(radius + " is " + area); } } Keywords Types of Statements Declarations Assignment statements Control Structures (Topic 4 &5) Call to functions (Topic 6) Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius as // double variable; char a; // Declare a to be a // character datatype variable; Declaring and Initializing int x = 1; double d = 1.4; A Sample Program Execution public class ComputeArea { public static void main(String[] args) { double radius; double area; Declaration // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.print("The area for the circle of radius “); System.out.println(radius + " is " + area); } } Named Constants final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3; 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. camelCase naming style For example, Names of variables radius and area, Names of 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 Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; A sample Program Execution public class ComputeArea { public static void main(String[] args) { double radius; double area; // Assign a radius Assignment radius = 20; statement stores value on // Compute area right to data area = radius * radius * 3.14159; on the left // Display results System.out.print("The area for the circle of radius “); System.out.println(radius + " is " + area); } } Numerical Data Types Name Range Storage Size byte –27 to 27 – 1 (-128 to 127) 8-bit signed short –215 to 215 – 1 (-32768 to 32767) 16-bit signed int –231 to 231 – 1 (-2147483648 to 2147483647) 32-bit signed long –263 to 263 – 1 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807) float Negative range: 32-bit IEEE 754 -3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38 double Negative range: 64-bit IEEE 754 -1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308 Reading Input from the Console 1. Import the library utility for Scanner import java.util.Scanner; 2. Create a Scanner object Scanner input = new Scanner(System.in); 3. Use the method nextDouble() to obtain to a double value. For example, System.out.print("Enter a double value: "); double d = input.nextDouble(); 4. Close the input resource. input.close(); Reading Numbers from the Keyboard Scanner input = new Scanner(System.in); int value = input.nextInt(); Method Description nextByte() reads an integer of the byte type. nextShort() reads an integer of the short type. nextInt() reads an integer of the int type. nextLong() reads an integer of the long type. nextFloat() reads a number of the float type. nextDouble() reads a number of the double type. Numeric Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2 Integer Division 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division) Remainder Operator % Very useful in programming. For example, number % 2 is always 0 – even number number % 2 is always 1 – odd number. Suppose today is Saturday. What day is in 10 days? : Saturday is the 6 th day in a week A week has 7 days (6 + 10) % 7 is 2 The 2nd day in a week is Tuesday After 10 days Exponent Operations Using Math.pow(x, y) to calculate xy System.out.println(Math.pow(2, 3)); // Displays 8.0 System.out.println(Math.pow(4, 0.5)); // Displays 2.0 System.out.println(Math.pow(2.5, 2)); // Displays 6.25 System.out.println(Math.pow(2.5, -2)); // Displays 0.16 Number Literals A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements: int i = 34; long x = 1000000; double d = 5.0; Integer Literals An integer literal can be assigned to an integer variable as long as it can fit into the variable. A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type. An integer literal - int type Value range: -231 (-2147483648) to 231–1 (2147483647). Long integer: append it with the letter L or l. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one). Floating-Point Literals Written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. Appending with F or f make a number a float and appending the letter d or D make a number a double by For example, 100.2f or 100.2F for a float number, 100.2d or 100.2D for a double number Augmented Assignment Operators Increment & Decrement Operators Using Increment & Decrement Operator int i = 10; Same effect as int newNum = 10 * i++; int newNum = 10 * i; i = i + 1; int i = 10; Same effect as int newNum = 10 * (++i); i = i + 1; int newNum = 10 * i; Numeric Type Conversion Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * 3.1 + k / 2; Conversion 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. Type Casting Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) What is wrong? int x = 5 / 2.0; END of slides