Lesson 4 Input. JAVA Input JAVA input is not straightforward and is different depending on the JAVA...
-
Upload
ralf-palmer -
Category
Documents
-
view
215 -
download
0
Transcript of Lesson 4 Input. JAVA Input JAVA input is not straightforward and is different depending on the JAVA...
![Page 1: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/1.jpg)
Lesson 4
Input
![Page 2: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/2.jpg)
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 complete object-oriented package, 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)
![Page 3: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/3.jpg)
Example Program with Inputimport 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();
}}
![Page 4: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/4.jpg)
Example Program Explanation
• 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.
Import TerminalIO.KeyboardReader;
![Page 5: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/5.jpg)
Example Program Explanation
• 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()
KeyboardReader reader = new KeyboardReader();
![Page 6: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/6.jpg)
Example Program Explanation
• 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 fahrenheit;double celsius;
![Page 7: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/7.jpg)
Example Program Explanation
• 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.
System.out.print(“Enter degrees Fahrenheit: “);
![Page 8: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/8.jpg)
Example Program Explanation
• 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”.
fahrenheit = reader.readDouble();
![Page 9: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/9.jpg)
• char readChar()
• double readDouble()
• int readInt()
• String readLine()
![Page 10: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/10.jpg)
Example Program Explanation
• 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.
celsius = (fahrenheit – 32.0) * 5.0/9.0;
![Page 11: 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.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf8a1a28abf838c8a62a/html5/thumbnails/11.jpg)
Example Program Explanation
• 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.
System.out.print(“The equivalent in Celsius is “);System.out.println(celsius);