Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 Lesson 4 Input

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

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

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

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

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

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

9 charreadChar() readChar doublereadDouble()readDouble intreadInt() readInt StringreadLine() readLine

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

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


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

Similar presentations


Ads by Google