Elementary Programming Chapter2: Elementary Programming
Agenda Contents of chapter 2 Objectives of chapter 2 Numerical data types Trace an execution of a calculation program Reading input from console Identifiers guidelines Declaring variables Constants Numeric operators Integer division Remainder operator Example of using remainder operators
Contents of chapter 2 Solve problems with programming Java primitive data types Related subjects to primitive data types such as : Variables Constants Operators Expressions Input and output.
Objectives of chapter2: To write Java programs to perform simple calculations (§2.2). To obtain input from the console using the Scanner class (§2.3). To use identifiers to name variables, constants, methods, and classes (§2.4). To use variables to store data (§§2.5-2.6). To program with assignment statements and assignment expressions (§2.6). To use constants to store permanent data (§2.7). To declare Java primitive data types: byte, short, int, long, float, double, and char (§§2.8.1). To use Java operators to write numeric expressions (§§2.8.2–2.8.3). To display current time (§2.9).
Objectives of chapter2: To use short hand operators (§2.10). To cast value of one type to another type (§2.11). To compute loan payment (§2.12). To represent characters using the char type (§2.13). To compute monetary changes (§2.14). To represent a string using the String type (§2.15). To become familiar with Java documentation, programming style, and naming conventions (§2.16). To distinguish syntax errors, runtime errors, and logic errors and debug errors (§2.17). (GUI) To obtain input using the JOptionPane input dialog boxes (§2.18).
Numerical data types:
Trace a Program Execution animation Trace a Program Execution public class ComputeArea { /** Main method */ 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.println("The area for the circle of radius " + radius + " is " + area); } memory radius no value allocate memory for radius
Trace a Program Execution animation Trace a Program Execution public class ComputeArea { /** Main method */ 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.println("The area for the circle of radius " + radius + " is " + area); } memory radius no value area no value allocate memory for area
Trace a Program Execution animation Trace a Program Execution assign 20 to radius public class ComputeArea { /** Main method */ 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.println("The area for the circle of radius " + radius + " is " + area); } radius 20 area no value
Trace a Program Execution animation Trace a Program Execution public class ComputeArea { /** Main method */ 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.println("The area for the circle of radius " + radius + " is " + area); } memory radius 20 area 1256.636 compute area and assign it to variable area
Trace a Program Execution animation Trace a Program Execution public class ComputeArea { /** Main method */ 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.println("The area for the circle of radius " + radius + " is " + area); } memory radius 20 area 1256.636 print a message to the console
Reading input from console Create a Scanner object Scanner input = new Scanner(System.in); 2. Use the methods next(), nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), or nextBoolean() to obtain to a string, byte, short, int, long, float, double, or boolean value. Example: System.out.print("Enter a double value: "); double d = input.nextDouble();
Identifiers guidelines: An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). It must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be true, false, or null. An identifier can be of any length.
Assignment Statements Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;
Declaring and Initializing in One Step int x = 1; double d = 1.4;
Declaration structure: Constants Constant variable value cannot be changed and all data type may be declared as constant using final. Declaration structure: final datatype CONSTANTNAME = VALUE; Example: final double PI = 3.14159; final int SIZE = 3;
Numeric operators
1 the remainder of the division Integer Division / for the Quotient % for the Remainder Type of the output depends on the 2 input types Example: output input 2 integer 5/2 2.0 double 5.0/2 1 the remainder of the division 5%2
Remainder Operator Remainder is very useful in programming. Example:an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd.
Example of using remainder operator: Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression: Note: Week starts on Monday Day1: Monday, Day2: Tuesday, Day3: Wednesday, Day4: Thursday, Day5:Firday, Day6:Saturday, Day7: Sunday