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
Assignment See my website for the new assignment.
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
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
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 ; System.out.println(fahr + " degrees Fahrenheit"); }//end main method }//end CtoF class
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
Valid Identifiers? taxes2013 _gold_mines 99bottles money$ main News two#
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
Primitive Types Data and objects have types. Data that is a primitive type has a single simple value. 1, 6.7, Are not objects Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Numeric Types int integer, positive/negative counting numbers. 1, -10, 0, double Numbers with a decimal part 1.5, -9.0, , 0.0 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
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
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
Variables Must be declared – Data type – Identifier Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
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
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
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
Reference Variable String str = “Hello”;
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?
nextDouble Scanner Method Used to input a double double rate = keyboard.nextDouble();
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.
Next Arithmetic Expressions