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.

Slides:



Advertisements
Similar presentations
Chapter Three Arithmetic Expressions and Assignment Statements
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Computer Programming Lab(4).
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Book of ANSI C Fourth Edition
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.
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.
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.
Chapter 2 Elementary Programming
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.
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:
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Elementary Programming
Lecture 2 Data Types Richard Gesick.
Multiple if-else boolean Data
Dialogues and Wrapper Classes
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Building Java Programs
Building Java Programs
Branching Chapter 5 11/14/16 & 11/15/
Multiple if-else boolean Data
Building Java Programs Chapter 2
elementary programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Variables in C Topics Naming Variables Declaring Variables
Building Java Programs
Building Java Programs
Presentation transcript:

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