Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
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.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Hello, world! Dissect HelloWorld.java Compile it Run it.
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;
Basic Elements of C++ Chapter 2.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Lesson 2: First Java Programs. Objectives: –Discuss why Java is an important programming language. –Explain the Java virtual machine and byte code. –Choose.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Elementary Programming
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 2: Java Fundamentals
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Lesson 4 Input. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are using. The reason it is not.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
2.5 Edit, Compile, and Execute Figure 2-3 illustrates the edit, compile and execute steps. Java bytecode.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Computing Fundamentals
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Text by: Lambert and Osborne
Multiple variables can be created in one declaration
Basic Elements of C++ Chapter 2.
INPUT STATEMENTS GC 201.
Chapter 2.
IDENTIFIERS CSC 111.
Introduction to C++ Programming
An Introduction to Java – Part I, language basics
Java Variables, Types, and Math Getting Started
MSIS 655 Advanced Business Applications Programming
Variables, Identifiers, Assignments, Input/Output
Chapter 3 – Introduction to C# Programming
Introduction to Primitives
Unit 3: Variables in Java
Data Types and Maths Programming Guides.
Presentation transcript:

Chapter 2 Input, Variables and Data Types

JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are using. JAVA input is not straightforward and is different depending on the JAVA environment that you are using. The reason it is not straightforward, is that JAVA is a completely object-oriented language, whereas every method must be an object. The reason it is not straightforward, is that JAVA is a completely object-oriented language, whereas every method must be an object. In other words, there is not a simple built-in command that is universal amongst compilers (i.e. cin for C++, input for Basic) In other words, there is not a simple built-in command that is universal amongst compilers (i.e. cin for C++, input for Basic)

Example Program with Input import TerminalIO.KeyboardReader; public class Convert { public static void main(String [ ] args) { KeyboardReader reader = new KeyboardReader(); double fahrenheit; double celsius; System.out.print(“Enter degrees Fahrenheit: “); fahrenheit = reader.readDouble(); celsius = (fahrenheit – 32.0) * 5.0/9.0; System.out.print(“The equivalent in Celsius is “); System.out.println(celsius);reader.pause();}}

Example Program Explanation The import statement incorporates a file from your computer into your program. The import statement incorporates a file from your computer into your program. The KeyboardReader class which is imported gives us the functionality to get input from the keyboard. The KeyboardReader class which is imported gives us the functionality to get input from the keyboard. Without this statement you will not be able to input information. Without this statement you will not be able to input information. import TerminalIO.KeyboardReader;

Example Program Explanation This statement instantiates or creates a KeyboardReader object. This statement instantiates or creates a KeyboardReader object. It’s name “reader” is an arbitrary name and can be called anything that you want. It’s name “reader” is an arbitrary name and can be called anything that you want. NOTE: An object is always an instance of a class and must be created, or instantiated before being used. NOTE: An object is always an instance of a class and must be created, or instantiated before being used. i.e. SomeClass someobject = new SomeClass() i.e. SomeClass someobject = new SomeClass() KeyboardReader reader = new KeyboardReader();

Example Program Explanation This defines two numeric variables that will be used to hold the input and calculate the result for output. This defines two numeric variables that will be used to hold the input and calculate the result for output. double means that the numbers are floating point numbers (i.e. they can contain decimals) double means that the numbers are floating point numbers (i.e. they can contain decimals) double fahrenheit; double celsius;

Example Program Explanation This statement creates a prompt in the output window. This statement creates a prompt in the output window. Prompt – A print or println statement that tells the user at the console window that something is to be entered. Prompt – A print or println statement that tells the user at the console window that something is to be entered. You must always prompt the user for the input. You must always prompt the user for the input. System.out.print(“Enter degrees Fahrenheit: “);

Example Program Explanation This statement gets the value that is entered from the keyboard into the variable “fahrenheit” This statement gets the value that is entered from the keyboard into the variable “fahrenheit” The reader object waits for a number to be input and the enter key to be pressed. The reader object waits for a number to be input and the enter key to be pressed. Once that is done, it returns that value to “fahrenheit”. Once that is done, it returns that value to “fahrenheit”. fahrenheit = reader.readDouble();

Example Program Explanation This statement contains the actual calculation for converting fahrenheit to celsius. This statement contains the actual calculation for converting fahrenheit to celsius. This is called an assignment statement. This is called an assignment statement. In an assignment statement, you cannot have any calculations on the left hand side of the =. You can only have variables. In an assignment statement, you cannot have any calculations on the left hand side of the =. You can only have variables. On the right hand side, you can have variables and math operators. On the right hand side, you can have variables and math operators. celsius = (fahrenheit – 32.0) * 5.0/9.0;

Example Program Explanation These statements place the output in the console window. These statements place the output in the console window. The first statement labels the data that is being output. The first statement labels the data that is being output. The second statement prints out the value that is contained in the memory location for celsius. The second statement prints out the value that is contained in the memory location for celsius. System.out.print(“The equivalent in Celsius is “); System.out.println(celsius);

Example Program Explanation This statement is used to prevent the terminal window from immediately disappearing after the JVM executes the last statement. This statement is used to prevent the terminal window from immediately disappearing after the JVM executes the last statement. It is only needed in some environments. It is only needed in some environments. In the environment that we will be using in class, we will not need this statement. In the environment that we will be using in class, we will not need this statement. reader.pause();

Methods in class KeyboardReader SignatureDescription char readChar() Returns the first character in the input line, even if it is a space. double readDouble() Returns the first double in the input line. Leading and trailing spaces will be ignored. integer readInt() Returns the first integer in the input line. Leading and trailing spaces are ignored string readLine() Returns the input line, including leading and trailing spaces pause() Returns once the user presses Enter.

Adding a Prompt to KeyboardReader You may combine the prompt with the input statement. This will save you a line of code. The following two options do the same thing. You may combine the prompt with the input statement. This will save you a line of code. The following two options do the same thing. Option 1: System.out.print(“Please enter a number: “); variable = reader.readInt(); Option 2: variable = reader.readInt(“Please enter a number: “);

Variables An area in memory that holds data and can be modified throughout the program. An area in memory that holds data and can be modified throughout the program. The names of the variables must be descriptive. The names of the variables must be descriptive. A variable can be any valid Java identifier. A variable can be any valid Java identifier. They usually begin with a lowercase letter to distinguish them from class names. They usually begin with a lowercase letter to distinguish them from class names. A variable must be declared before it can be used. A variable must be declared before it can be used.

Variables A variable declaration includes the following: A variable declaration includes the following: A data type that identifies the type of data that the variables will store A data type that identifies the type of data that the variables will store An identifier that is the variable’s name An identifier that is the variable’s name An optional assigned value, when you want a variable to contain an initial value An optional assigned value, when you want a variable to contain an initial value An ending semicolon An ending semicolonEx: int myAge=17; float height;

Data Types Java has 8 primitive(simple) data types Java has 8 primitive(simple) data types boolean boolean byte byte char char double double float float int int long long short short

int Data Type Used for variables that contain whole numbers. Used for variables that contain whole numbers. Type Min Value Max Value Size in Bytes byte short-32,76832,7672 int-2,147,483,6482,147,483,6474 long-9,223,372,036,854,775,8089,223,372,036,854,775,8078

boolean Data Type Boolean logic is based on true-or-false comparisons. Boolean logic is based on true-or-false comparisons. A boolean variable can hold only one of two values – True or False A boolean variable can hold only one of two values – True or FalseEx: boolean isItPayday = false; boolean areYouBroke = true;

float Data Type Used for variables that contain decimals. Used for variables that contain decimals. Type Min Value Max Value Size in Bytes float -1.4* * double -1.7* *

char Data Type The char data type is used to hold a single character. The char data type is used to hold a single character. The stored character must be placed in single quotes. The stored character must be placed in single quotes.Ex: char myInitial = ‘M’; char percentSign = ‘%’;