Chapter 2 Elementary Programming CS170 Introduction to Computer Science 1 Objectives To write Java programs to perform simple calculations ( § 2.2). To use identifiers to name variables, constants, methods, and classes ( § 2.3). To use variables to store data ( §§ 2.4-2.5). To program with assignment statements and assignment expressions ( § 2.5). To use constants to store permanent data ( § 2.6). To declare Java primitive data types: byte, short, int, long, float, double, and char ( §§ 2.7 – 2.9). To use Java operators to write numeric expressions ( §§ 2.7–2.8). To represent characters using the char type ( § 2.9). To represent a string using the String type ( § 2.10). To obtain input from the console using the Scanner class ( §§ 2.11-2.12). To become familiar with Java documentation, programming style, and naming conventions ( § 2.13). To distinguish syntax errors, runtime errors, and logic errors ( § 2.14). To debug logic errors ( § 2.15). (GUI) To obtain input using the JOptionPane input dialog boxes ( § 2.16). Introducing Programming with an Example Computing the Area of a Circle 1. Read in the radius of a circle 2. Compute the area of the circle 3. Print out a message with the computed result
Variables A variable is a name for a location in memory used to hold a data value. Type, name and contents Using a variable Declaring a variable – type and name Instructs the compiler to reserve a portion of main memory to hold a particular type of value referred by a particular name Assign a value to a variable Syntax: Variable Definition typeName variableName ; Example: int luckyNumber; Purpose: To define a new variable of a particular type Syntax: Assignment Syntax: variableName = expression ; Example: luckyNumber = 12; luckyNumber = 5+7; Purpose: To assign a new value to a previously defined variable.
Data Types Fundamental or primitive data types and object types 8 primitive types 6 number types: four integer types and two floating point types 1 character type 1 boolean type Numerical Data Types Name Range Storage Size –27 (-128) to 27–1 (127) byte 8-bit signed –215 (-32768) to 215–1 (32767) short 16-bit signed –231 (-2147483648) to 231–1 (2147483647) 32-bit signed int –263 to 263–1 long 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 Floating point numbers 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. Integers are stored precisely. Therefore, calculations with integers yield a precise integer result.
Number demo An excellent tool to demonstrate how numbers are stored in a computer was developed by Richard Rasala. You can access it at http://www.ccs.neu.edu/jpt/jpt_2_3/bitdisplay/applet.htm Identifiers An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). Rules for Java identifier 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. (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. Identifiers Conventions variable names start with a lowercase letter class names start with an uppercase letter Meaningful names Camel case E.g. luckyNumber
Self Check Which of the following are legal identifiers? 1. Greeting1 g void 101dalmatians Hello, World <greeting> Define a variable to hold your name. Use camel case in 2. the variable name. Declaring Variables - Examples int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; Syntax: Assignment variableName = expression ; Example: luckyNumber = 12; luckyNumber = 5+7; Purpose: To assign a new value to a previously defined variable.
Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; Declaring and Initializing in One Step int x = 1; double d = 1.4; Expressions An expression is a combination of one or more operators and operands that perform a calculation Operands might be numbers, variables, or other source of data Arithmetic expressions
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 Division and remainder Division operator Performs integer division when both operands are integers 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 Remainder operator 5 % 2 yields 1 (the remainder of the division) Remainder Operator How to determine whether a number is even or odd? Suppose today is Saturday and you and your friends are going to meet in 10 days. 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 2 nd day in a week is Tuesday After 10 days
Arithmetic Expressions 3 4 x 10 ( y 5 )( a b c ) 4 9 x 9 ( ) 5 x x y is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y) How to Evaluate an Expression Arithmetic rules apply for evaluating a Java expression. 3 + 4 * 4 + 5 * (4 + 3) - 1 (1) inside parentheses first 3 + 4 * 4 + 5 * 7 – 1 (2) multiplication 3 + 16 + 5 * 7 – 1 (3) multiplication 3 + 16 + 35 – 1 (4) addition 19 + 35 – 1 (5) addition 54 - 1 (6) subtraction 53 Compute Area Problem 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); } }
animation Trace a Program Execution allocate memory public class ComputeArea { for radius /** Main method */ public static void main(String[] args) { radius no value 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); } } animation Trace a Program Execution public class ComputeArea { /** Main method */ memory public static void main(String[] args) { double radius; radius no value double area; area no value // Assign a radius radius = 20; allocate memory // Compute area for area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } animation Trace a Program Execution assign 20 to radius public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; radius 20 double area; area no value // 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); } }
animation Trace a Program Execution public class ComputeArea { /** Main method */ memory public static void main(String[] args) { double radius; radius 20 double area; area 1256.636 // Assign a radius radius = 20; // Compute area compute area and assign area = radius * radius * 3.14159; it to variable area // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } animation Trace a Program Execution public class ComputeArea { /** Main method */ memory public static void main(String[] args) { double radius; radius 20 double area; area 1256.636 // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; print a message to the console // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } Literals A literal is a constant value that appears directly in the program. Number literal double radius = 5.0; int i = 34; long x = 1000000; String literal System.out.println(“Hello World!”);
Recommend
More recommend