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.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2 Elementary Programming
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
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.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Chapter 2 Variables.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Lecture 4 – Scanner & Style
Lecture 2 Data Types Richard Gesick.
Multiple if-else boolean Data
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Building Java Programs
Branching Chapter 5 11/14/16 & 11/15/
Multiple if-else boolean Data
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Presentation transcript:

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

Assignment See my website for the new assignment.

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

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

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

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

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

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

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

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

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

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  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

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

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

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

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

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

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 Figure 2-4 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”;

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

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?

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.

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.

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

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

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

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

//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(); }

Constants

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

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

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

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

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?