Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 2 Input, Variables and Data Types

2 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)

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

4 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;

5 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();

6 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;

7 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: “);

8 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();

9 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;

10 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);

11 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();

12 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.

13 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: “);

14 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.

15 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;

16 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

17 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-1281271 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

18 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;

19 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*10 -45 3.4*10 38 4 double -1.7*10 -308 1.7*10 308 8

20 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 = ‘%’;


Download ppt "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."

Similar presentations


Ads by Google