APCS Notes 2.6 - Pg # of #


2.6: Temperature Conversion Program

Intro: Today, we talk about inputs - the ability for the user to interact with the program.

You can follow along on pgs 38-40.

import TerminalIO.KeyboardReader;

Import statements make use of existing Java classes so you don’t have to rewrite the code for it!

KeyboardReader reader = new KeyboardReader()

Instantiating a “KeyboardReader” object. We decided to call the object “reader.”

Since an object is always an instance of a class, it must be created (instantiated) before being used.

In General:

SomeClass someObject = new SomeClass();


double fahrenheit; double celsius;

We are defining two variables here.
(This names a location in RAM where a number will be stored.) The value of the variable may change (hence the name variable!) but the name will not.

double” refers to the type of variable - in this case floating point numbers.

Programming Tip: Define variables in lowercase

fahrenheit = reader.readDouble();

Here, the reader object responds to the message readDouble by waiting for the user to input a value and press enter. Then the number is stored as a value for fahrenheit.

Note: The parameter parentheses are still required even though there were no parameters for the message.

celsius = (fahrenheit - 32.0) • 5.0 / 9.0;

Defining the conversion formula! This is called an assignment statement because it utilizes the assignment operator “=.”


Note:
We used decimals because we defined the numbers as floating point and not as integers. (More on this later.)

Operators:
* = multiplication
/ = division
- = subtraction
+ = addition

System.out.println(celsius);

Just telling the system to print the value of the variable celsius.

Note: So we see that System.out.println can be used to display a variable, text (string) or even an expression!

reader.pause();

Designed to prevent the terminal window from disappearing after the JVM executes the program. Sending the pause message to the reader object displays the message “Press Enter to Continue ...” in the terminal window, thus pausing the program.