© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Elementary Programming
Chapter 2 Data Types, Input, and Output Section 1 - Java Data Types Section 2 - Declaring and Initializing Variables Section 3 - Arithmetic Operators.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
COMP String and Console I/O Yi Hong May 18, 2015.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Programming Fundamentals 2: Libraries/ F II Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
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.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Introduction to programming in java
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Foundations of Programming: Java
CompSci 230 S Programming Techniques
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 4 Assignment Statement
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Data Conversion & Scanner Class
Chapter 3 Assignment Statement
Chapter 3 Java Input/Output.
Java Programming: From Problem Analysis to Program Design, 4e
Topic 11 Scanner object, conditional execution
INPUT STATEMENTS GC 201.
BIT115: Introduction to Programming
الكلية الجامعية للعلوم التطبيقية
Introduction to Classes and Methods
A+ Computer Science INPUT.
Chapter 3 Input/Output.
A+ Computer Science INPUT.
F II 6. Using Libraries Objectives
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Math class services (functions)
Presentation transcript:

© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is assigned to x x = y + 2; the value of an expression (y + 2) is assigned to x x = z; the value of another variable ( z ) is assigned to x

© 2007 Lawrenceville Press Slide 2 Chapter 4 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; x 5 10 The box starts out empty

© 2007 Lawrenceville Press Slide 3 Chapter 4 Primitive Data Types TypeStorage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit One byte is 8 bits Like

© 2007 Lawrenceville Press Slide 4 Chapter 4 The Scanner Class  Part of the java.util package  Type:  import java.util.Scanner;  At the top of your program to use the Scanner  A Scanner object reads in text and numbers  Methods include: next() – reads a word nextLine() – read a sentence nextInt() – read a number nextDouble() – read a number nextBoolean() – read true/false close() – stop reading

© 2007 Lawrenceville Press Slide 5 ExceptionsExceptions When using the Scanner class, if you tell the program to read the wrong type of data it will generate an error. An exception is an error that happens when the program is running. – –nextLine() can read any kind of data, Strings and numbers, it reads the entire line a person types in – –next() reads just one word – –nextDouble() reads a number with a decimal point – –nextInt() only reads whole numbers – –nextBoolearn() only reads true/false

© 2007 Lawrenceville Press Slide 6 Greeting Program Greeting Program This program will ask a person for their name and greet them personally import java.util.Scanner; public class Greeting { public static void main(){ Scanner keyboard = new Scanner(); System.out.print(“Enter your name: “); String name = keyboard.nextLine(); System.out.println(“Hello, “+name); } } To allow people to type in information while a program is running, you need the Scanner The class is called Greeting The method is called main Before you can use the Scanner you have to create a Scanner object This is called a prompt This reads what is typed at the keyboard and stores it in the name variable Print the word Hello followed by the contents of the variable name (+ means concatenation)