21
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

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

Embed Size (px)

Citation preview

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 + 32.0;

System.out.println(fahr + " degrees Fahrenheit");

}//end main method }//end CtoF class

Identifiers

Examples: public, main, CtoF, celNames 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#

ConventionsClass names begin with upper caseMethod names and begin with lower caseBraces

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 indentedSkip lines for readability (white space)

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Primitive TypesData and objects have types.Data that is a primitive type has a single

simple value.1, 6.7, -1000000Are 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, 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

Reference TypesCan contain multiple pieces of dataMay also have methods that operate on

those valuesExample – String, Scanner

“Hello”Contains 5 characters

Name of the class is the data typeCalled 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 dataStores 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 valueSyntax

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 StatementFigure 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 doubledouble 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