motivations chapter 2 elementary
play

Motivations Chapter 2: Elementary In the preceding chapter, you - PDF document

8/24/18 Motivations Chapter 2: Elementary In the preceding chapter, you learned how to Programming create, compile, and run a Java program. Starting from this chapter, you will learn how to solve CS1: Java Programming practical problems


  1. 8/24/18 Motivations Chapter 2: Elementary In the preceding chapter, you learned how to Programming create, compile, and run a Java program. Starting from this chapter, you will learn how to solve CS1: Java Programming practical problems programmatically. Through these problems, you will learn Java primitive data Colorado State University types and related subjects, such as variables, constants, data types, operators, expressions, and Original slides by Daniel Liang input and output. Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Objectives Introducing Programming with an To write Java programs to perform simple computations (§2.2). ✦ Example 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). Listing 2.1 Computing the Area of a Circle ✦ To use constants to store permanent data (§2.7). ✦ To name classes, methods, variables, and constants by following their naming conventions (§2.8). ✦ To explore Java numeric primitive data types: byte , short , int , long , float , and double (§2.9.1). ✦ This program computes the area of the circle. To read a byte , short , int , long , float , or double value from the keyboard (§2.9.2). ✦ To perform operations using operators + , - , * , / , and % (§2.9.3). ✦ To perform exponent operations using Math.pow(a, b) (§2.9.4). ✦ To write integer literals, floating-point literals, and literals in scientific notation (§2.10). ✦ Note: Clicking the green button displays the source code To write and evaluate numeric expressions (§2.11). ✦ ComputeArea with interactive animation. You can also run the code in To obtain the current system time using System.currentTimeMillis() (§2.12). ✦ a browser. Internet connection is needed for this button. To use augmented assignment operators (§2.13). ✦ Run To distinguish between postincrement and preincrement and between postdecrement and predecrement (§2.14). ✦ Note: Clicking the blue button runs the code from To cast the value of one type to another type (§2.15). ✦ Windows. If you cannot run the buttons, see To describe the software development process and apply it to develop the loan payment program (§2.16). ✦ IMPORTANT NOTE: If you cannot run the buttons, see To write a program that converts a large amount of money into smaller units (§2.17). ✦ www.cs.armstrong.edu/liang/javaslidenote.doc. To avoid common errors and pitfalls in elementary programming (§2.18). ✦ Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 4 rights reserved. rights reserved. animation animation Trace a Program Execution Trace a Program Execution allocate memory public class ComputeArea { public class ComputeArea { for radius memory /** Main method */ /** Main method */ public static void main(String[] args) { public static void main(String[] args) { radius no value double radius; radius no value double radius; double area; double area; area no value // Assign a radius // Assign a radius radius = 20; radius = 20; allocate memory for area // Compute area // Compute area area = radius * radius * 3.14159; area = radius * radius * 3.14159; // 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); } } } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 6 rights reserved. rights reserved. 1

  2. 8/24/18 animation animation Trace a Program Execution Trace a Program Execution assign 20 to radius public class ComputeArea { public class ComputeArea { memory /** Main method */ /** Main method */ public static void main(String[] args) { public static void main(String[] args) { 20 radius radius 20 double radius; double radius; double area; double area; 1256.636 area no value area // Assign a radius // Assign a radius radius = 20; radius = 20; compute area and assign it // Compute area // Compute area to variable area area = radius * radius * 3.14159; area = radius * radius * 3.14159; // 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); } } } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 7 8 rights reserved. rights reserved. animation Reading Input from the Console Trace a Program Execution public class ComputeArea { 1. Create a Scanner object memory /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); radius 20 double radius; double area; 2. Use the method nextDouble() to obtain to a double area 1256.636 value. For example, // Assign a radius radius = 20; System.out.print("Enter a double value: "); // Compute area Scanner input = new Scanner(System.in); print a message to the area = radius * radius * 3.14159; double d = input.nextDouble(); console // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } Run ComputeAreaWithConsoleInput } Run ComputeAverage Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 9 10 rights reserved. rights reserved. Identifiers Variables ✦ An identifier is a sequence of characters that consist of // Compute the first area letters, digits, underscores (_), and dollar signs ($). radius = 1.0; ✦ An identifier must start with a letter, an underscore (_), area = radius * radius * 3.14159; or a dollar sign ($). It cannot start with a digit. System.out.println("The area is “ + ✦ An identifier cannot be a reserved word. (See Appendix area + " for radius "+radius); A, “Java Keywords,” for a list of reserved words). ✦ An identifier cannot be true , false , or // Compute the second area null . radius = 2.0; area = radius * radius * 3.14159; ✦ An identifier can be of any length. System.out.println("The area is “ + area + " for radius "+radius); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 11 12 rights reserved. rights reserved. 2

  3. 8/24/18 Declaring Variables Assignment Statements int x; // Declare x to be an x = 1; // Assign 1 to x; // integer variable; radius = 1.0; // Assign 1.0 to radius; double radius; // Declare radius to a = 'A'; // Assign 'A' to a; // be a double variable; char a; // Declare a to be a // character variable; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 13 14 rights reserved. rights reserved. Named Constants Declaring and Initializing in One Step final datatype CONSTANTNAME = VALUE; ✦ int x = 1; final double PI = 3.14159; ✦ double d = 1.4; final int SIZE = 3; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 15 16 rights reserved. rights reserved. Naming Conventions Naming Conventions, cont. ✦ Class names: ✦ Choose meaningful and descriptive names. – Capitalize the first letter of each word in ✦ Variables and method names: the name. For example, the class name ComputeArea . – Use lowercase. If the name consists of several words, concatenate all in one, use lowercase ✦ Constants: for the first word, and capitalize the first letter of each subsequent word in the name. For – Capitalize all letters in constants, and use underscores to connect words. For example, the variables radius and area , and example, the constant PI and the method computeArea . MAX_V ALUE Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 17 18 rights reserved. rights reserved. 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend