Chapter 2 - Bismarck Public Schools

Download Report

Transcript Chapter 2 - Bismarck Public Schools

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.
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)
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
import TerminalIO.KeyboardReader;



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.
Without this statement you will not be able to
input information.
Example Program Explanation
KeyboardReader reader = new KeyboardReader();



This statement instantiates or creates a
KeyboardReader object.
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.
•
i.e. SomeClass someobject = new SomeClass()
Example Program Explanation
double fahrenheit;
double celsius;


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)
Example Program Explanation
System.out.print(“Enter degrees Fahrenheit: “);



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.
You must always prompt the user for the input.
Example Program Explanation
fahrenheit = reader.readDouble();



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.
Once that is done, it returns that value to
“fahrenheit”.
Example Program Explanation
celsius = (fahrenheit – 32.0) * 5.0/9.0;




This statement contains the actual calculation for
converting fahrenheit to celsius.
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.
On the right hand side, you can have variables and
math operators.
Example Program Explanation
System.out.print(“The equivalent in Celsius is “);
System.out.println(celsius);



These statements place the output in the
console window.
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.
Example Program Explanation
reader.pause();



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.
In the environment that we will be using in
class, we will not need this statement.
Methods in class
KeyboardReader
Signature
Description
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.
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.
The names of the variables must be descriptive.
A variable can be any valid Java identifier.
They usually begin with a lowercase letter to
distinguish them from class names.
A variable must be declared before it can be
used.
Variables

A variable declaration includes the following:
•
•
•
•
A data type that identifies the type of data that the
variables will store
An identifier that is the variable’s name
An optional assigned value, when you want a variable to
contain an initial value
An ending semicolon
Ex:
int myAge=17;
float height;
Data Types

Java has 8 primitive(simple) data types
•
•
•
•
•
•
•
•
boolean
byte
char
double
float
int
long
short
int Data Type

Used for variables that contain whole numbers.
Size in
Bytes
1
Type Min Value
byte
-128
Max Value
127
short
-32,768
32,767
2
int
-2,147,483,648
2,147,483,647
4
long
-9,223,372,036,854,775,808
9,223,372,036,854,775,807
8
boolean Data Type
Boolean logic is based on true-or-false
comparisons.
 A boolean variable can hold only one of two
values – True or False
Ex:
boolean isItPayday = false;
boolean areYouBroke = true;

float Data Type

Used for variables that contain decimals.
Type
float
Min Value
-1.4*10-45
Max Value
3.4*1038
double
-1.7*10-308
1.7*10308
Size in
Bytes
4
8
char Data Type
The char data type is used to hold a single
character.
 The stored character must be placed in single
quotes.
Ex:
char myInitial = ‘M’;
char percentSign = ‘%’;
