Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.

Similar presentations


Presentation on theme: "More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010."— Presentation transcript:

1 More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

2 Assignment See my website for the new assignment.

3 Chapter Topics  Identifiers  Comments  Data Types int and double  Variables Declarations Assignments Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

4 Temperature Conversion An online friend lives in Calgary. She always tells me the temperature up there in degrees Celsius. I wrote a program to convert the temperature to Fahrenheit. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

5 import java.util.Scanner; public class CtoF { /* This program converts Celsius to Fahrenheit. */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print (" Enter Degrees Celcius:“); double cel = scan.nextDouble( ); double fahr = 1.8 * cel + 32.0; System.out.println(fahr + " degrees Fahrenheit"); }//end main method }//end CtoF class

6 Identifiers Examples: public, main, CtoF, cel Names of things within the program  Made up of letters, digits, underscore, and $  Must not start with a digit  Case sensitive Categories  Names you invent  Names chosen by another programmer  Identifiers part of the Java language Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

7 Valid Identifiers? taxes2013 _gold_mines 99bottles money$ main News two#

8 Conventions Class names begin with upper case Method names and begin with lower case Braces  eclipse puts open brace on the line with the heading  Line up indentation of the closing brace with whatever it is ending.  End brace commented(optional) Lines within braces indented Skip lines for readability (white space) Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

9 Primitive Types Data and objects have types. Data that is a primitive type has a single simple value. 1, 6.7, -1000000 Are not objects Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

10 Numeric Types int integer, positive/negative counting numbers. 1, -10, 0, 20000 double Numbers with a decimal part 1.5, -9.0, 3.14159, 0.0 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

11 Reference Types Can contain multiple pieces of data May also have methods that operate on those values Example – String, Scanner “Hello” Contains 5 characters Name of the class is the data type  Called a “class type” or “reference type” Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

12 Variables Used to store a piece of data Stores it at a certain memory location Identifier used to name variable Should begin with lower case letter Subsequent words use upper case Should be meaningful Example: salesTaxRate Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

13 Variables Must be declared – Data type – Identifier Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

14 Assignment Statement Assignment gives a variable a value Syntax variable = expression ; Expression evaluated Value stored in memory location specified by variable Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

15 Assignment Statement Figure 2-3 Note values before, after Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

16 Variables in a Program Effects of sequence of assignments Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

17 Reference Variable String str = “Hello”;

18 Questions What are the rules for writing an identifier (name) in Java? What primitive type would you use to store the weight of a car in tons? On which side of the = does the variable in an assignment statement belong?

19 nextDouble Scanner Method Used to input a double double rate = keyboard.nextDouble();

20 Example Write a program to find the sales tax on an item. Input the cost of the item into a variable. Find the tax and output it.

21 Next Arithmetic Expressions


Download ppt "More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010."

Similar presentations


Ads by Google