Presentation is loading. Please wait.

Presentation is loading. Please wait.

A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.

Similar presentations


Presentation on theme: "A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010."— Presentation transcript:

1 A First Look at Java Chapter 2 1/29 & 2/2 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 A Simple Java Program Identifiers Displaying Text Comments Data Types Primitive Types Reference Types Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

4 Chapter Topics Variables Declarations Assignments Simple Input From the Keyboard Constants Unnamed Constants Named Constants Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

5 Objectives After studying this chapter, you should be able to: Create valid Java identifiers Write Java statements that display text Describe the primitive data types int, double, char, and boolean Declare variables and assign them values Read numbers and characters from the keyboard Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

6 Displaying Text Two commands System.out.println outputs newline character after the output System.out.print leaves the cursor on the same line. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

7 Displaying Text System.out.print(“Black”); System.out.println(“bird”); System.out.println(“sings.”); Output: Blackbird sings. | Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

8 Comments Documents the program Comments Begin line with double slash //  Ends with the end of the line. Span multiple lines between slash-star combination. /*...... */ Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

9 A Simple Java Program 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

10 public class CtoF { /** This program converts Celsius to Fahrenheit. */ public static void main(String[] args) { double cel = 20; double fahr; fahr = 1.8 * cel + 32.0; System.out.println(fahr + " degrees Fahrenheit"); }//end main method }//end CtoF class

11 A Simple Java Program Note definition of a class Begins and ends with brace { … } Note declaration of main method public static void main(String[] args) Also begins and ends with brace Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

12 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

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

14 Conventions Class names begin with upper case Method names and begin with lower case Braces  Each on its own line (optional) eclipse puts it on the line with the heading  Always in pairs  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

15 Primitive Types Data that is a primitive type has a single simple value. Examples: individual characters, numbers, boolean values – 'Q', 55.78, true Are not objects Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

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

17 Typical Primitive 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

18 Typical Primitive Types char Holds just one character 'z', '*', '2' boolean true or false Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

19 Reference Types Can contain multiple pieces of data May also have methods that operate on those values Example – String “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

20 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

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

22 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

23 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

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

25 Reference Variable String str = “Hello”;

26 Notes on Output + operator in print statements  Add numbers (5+6)  Concatenate Strings. "to" + "day" --> "today" 15 + " inches" --> 15 inches Strings that span multiple lines Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

27 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?

28 Try a Program Write a program that sets a variable to a person's name and another variable to their former age. It will output their name and their new age on their birthday. Sample output: John is 21 today.

29 Another Example Write a program to find the sales tax on an item. Assign the cost of the item to a variable. Set a price variable to cost of the item. Output the item's name and the tax.

30 Scanner and Constants 2/5/15 & 2/9/15

31 Keyboard Input Use Scanner class from Java Class Library Must use import statement: import java.util.Scanner; Next, create Scanner object Scanner keyboard = new Scanner(System.in); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 Package nameClass name

32 Keyboard Input Keyboard is an object which can perform methods. Use method to receive data from keyboard, store in variable int x = keyboard.nextInt(); String word = keyboard.next(); Let’s change the Age program so that it asks the user for the name and age. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

33 Other Scanner Methods double rate = keyboard.nextDouble(); String words = keyboard.nextLine();

34 //program to give new age on a birthday. import java.util.Scanner; public class Birthday { public static void main(String[] args) { String name; int age; //Create a Scanner Scanner kbd = new Scanner(System.in); //Input name System.out.print("Name?"); name = kbd.nextLine(); //Input age. System.out.print("Age?"); age = kbd.nextInt(); age = age + 1; System.out.println(name + " is " + age + " today."); kbd.close(); }

35 Constants

36 Unnamed constants Literal values – integers, floating-point, boolean E format may be used for doubles 1.5e2 --> 150 8e4 - decimal maybe left off, still a double 5.1e-4 = ? Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

37 Constants Named constants Similar to declaring variables, but values can't be changed. Use reserved word final Gives descriptive name, avoids typing mistakes All caps by convention final double TAX =.06; final int DOZEN = 12; final boolean NOT_DONE = false; Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

38 Participation Input and Constants Write a program to input the cost of a car in dollars. Output the sales tax on the car. Tax rate is 6%  Use a constant. 1. Input cost – use a Scanner 2. Find tax 3. Output tax I'll submit the program. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

39 Use of Math class For the programming assignment you will need to use Math.sqrt( ) and Math.PI.

40 Questions What type of object is used to handle input? What package does the Scanner belong to? What how is a constant different from other variables?


Download ppt "A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010."

Similar presentations


Ads by Google