Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 3: Variables in Java

Similar presentations


Presentation on theme: "Unit 3: Variables in Java"— Presentation transcript:

1 Unit 3: Variables in Java
Using numbers and Math in Java and Alice

2 What is a Variable? Data is constant when it can’t be changed while the program is running. It is variable if it might change. Variables are locations in memory where values are stored. Example: If we wanted a program to make a paycheck for a bunch of employees, it must be flexible enough to do it for each employee’s pay. Instead of using a specific number, we would use a variable such as grossPay. Variables must have a type, name, and a value.

3 Naming Variables Variable names may consist of letters, digits, underscores (_) or $. No spaces allowed! The first character cannot be a digit. Begin with a lowercase letter. If there are multiple words, the other words should each be capitalized. (This is a style standard. Java will accept variables with any capitalization.) Names should be meaningful. Case sensitive, so that number, Number, and NUMBER are all different variables to the computer.

4 Variable Types Type Size Range Use byte (integer) 8 bits -128 to 127
Infrequent short (integer) 16 bits to 32767 int (integer) 32 bits -2,147,483,648 to 2,147,483,647 Frequent long (integer) 64 bits to float (floating point) 32 bits single precision E+38 to E+38 (7 digits precision) Double (floating point) 64 bits double precision E+308 to E+308 (16 digits precision) char 16 bits unsigned One character using Unicode system Y/N, etc. Boolean True or false logic

5 Declaring and initializing variables
Variables must be declared with a type, usually at the beginning of a section of code. Ex: int number; sets up integer called “number” but doesn’t give it a value to start. You can declare a variable and give it a starting value in one step. Ex: int number = 15; declares “number” and sets it to 15 to start. You can declare multiple variables by separating them with commas. Ex: int x, y, z; sets up 3 integer variables called x, y, and z.

6 Declaring and initializing variables
You can declare and initialize multiple variables in one statement. Ex: int age = 30, hours = 45, sum = 0; But, it’s best to declare them on separate lines with comments so that you can add comments. Ex: int age = 30; //age is the employee’s age int hours = 45; //hours is the employee’s weekly hours worked int sum = 0; //sum is the total of all hours worked

7 Declaring and initializing variables
If you try to assign a number with a decimal point to an integer, you will get an error! Ex: int z = 18.5; //ERROR! A Boolean variable should be initialized as true or false Ex: Boolean x = true; //assigns the value true to the variable x. A char variable can be assigned a value expressed by a single character surrounded by single quotes Ex: char myLetter = ‘A’; //assigns the character A to variable myLetter.

8 String variables If you need to represent a series of characters instead of just one, you can use a string. Useful for displaying messages, inputting text, etc. May include letters, digits, and special characters. Strings are considered objects in Java (not really variables) Declared as a sequence of characters within double quotes. Ex: String message = “Go Trojans!!!”; declares and assigns the string.

9 String Concatenation The + operator, when used with strings and other objects, creates a single string that contains the concatenation (combination). Does NOT mean to do arithmetic! Ex: System.out.println(“The total is “ ); would print The total is because it concatenates the 25 and 10. If you want it to do arithmetic, put parentheses around the (25+10). To include a variable as part of a string, use the + operator. Ex: double total = 50.95; System.out.println (“The total is “ + total); These lines would tell the computer to print: The total is 50.95

10 Java Basics A statement is the simplest thing you can do in Java.
Each statement generally ends with a semicolon (;) A block of statements are surrounded by curly braces { }. Ex: int i; //declares a variable i to be an integer i = a + 1; //this is an arithmetic statement System.out.println(“Hello World”); //this is a print statement

11 Print Statements Printing in Java is done with two statements:
System.out.println goes to a new line before printing System.out.print remains on the same line You can make the computer tab over spaces and displays backslashes etc. \n Newline \t Tab \b Backspace \\ Displays a Backslash \” Displays a Double Quote Ex: System.out.println(“I am going to earn an \”A\” in this course.”); Will print: I am going to earn an “A” in this course.

12 Arithmetic Statements
Generally follow PEMDAS order of operations. New operation – modulus (%) – is the remainder from dividing. In terms of PEMDAS it is done with multiplication and division. Used for programs such as counting out change (when you figure out how many quarters you need to know how many more cents will have to be given in dimes, nickels, etc.) Ex: x = 5%3 would store 2 in the x variable.

13 Exercise #1 – Converting Fahrenheit to Celsius with Java
Get into JCreator. Open a New File with the name Celsius and save it in a Unit 3 folder. Add some comments about the program at the beginning. Use /* at the beginning and another / at the end. The program will convert from Fahrenheit to Celsius. Make sure to include your name and date. We will add statements inside of the main method to tell the computer what to do. We want to add two variables that we will declare as doubles so that they can have decimal points. double fahrenheit=52.0; double celsius;

14 Exercise #1 – Converting Fahrenheit to Celsius with Java
4. Next we want the computer to calculate the value of celsius based on the Fahrenheit temperature. The mathematical formula is 𝐶= 5 9 (𝐹−32). celsius = 5/9 * (fahrenheit–32); 5. Finally we need to computer to print the calculation. System.out.println(“Fahrenheit: “+ fahrenheit); System.out.println(“Celsius: “+ celsius);

15 Exercise #1 – Converting Fahrenheit to Celsius with Java
Compile the program (in the build menu) and correct any errors. Execute the program. Does it work? NO! The problem is with the 5/9 part of the arithmetic. Since 5 and 9 are both integers, the computer is dividing them and using just the integer part, which is 0. In order to fix the problem, change the 5 to 5.0 or the 9 to Compile and execute the new version. It should work now! Try changing the value in the Fahrenheit variable to a number of your choice. Run the program. Do you get the right output?

16 Exercise #2 – Choose Your Own Computation
Next Steps: Write your own program that will use a formula to compute something. You could think about the quadratic formula, distance formula, slope formula, etc. For now, you will need to set the inputs in the program. Later we will learn how to make the program ask for the inputs from the user.


Download ppt "Unit 3: Variables in Java"

Similar presentations


Ads by Google