Presentation is loading. Please wait.

Presentation is loading. Please wait.

Other types of variables

Similar presentations


Presentation on theme: "Other types of variables"— Presentation transcript:

1 Other types of variables
double (also known as “floating point number”) allows decimals much more useful than integers, but takes up more memory char A single character You must put the character in single quotes, if you are giving a char value in your code example: char myGrade = ‘ ’; String Words, phrases, sentences Capitalize String Use double quotes example: String firstName= “”; Demo

2 Arithmetic You can perform arithmetic with numbers and/or variables.
Java follows mathematical order of operations (PEMDAS). Example: int x = 0; x = ( / 2); x now equals 22, not 20.

3 Input If you want a user of your program to input information, you must create an object of the Scanner class. You also must have an import statement so that you can access the Scanner class. Demo

4 char ltr = ‘ ‘; ltr = ‘b’; System. out
char ltr = ‘ ‘; ltr = ‘b’; System.out.println(ltr + ltr); What would this display?

5 Type Casting Let’s say you have a double that you need to convert into an integer This can be accomplished by type casting: int x = 0; double y = 3.7; x = (int)y; // the (int) is where the type casting occurs What does x equal now? also valid: x = (int)(y); incorrect: x = int(y);

6 Integer vs. Double division
Guess the result of the following: int x = 5; int y = 2; int z = x/y; Dividing with integers: the decimal part of the answer is truncated (erased). To be accurate, use at least one double, or one decimal in your division.

7 int x = 5; int y = 2; double z = x/y; what does z equal now? 2.0 ***It doesn’t matter that the “answer,” z, is a double. The sequence of how the code is executed is important here. Java does the division FIRST, then assigns the result to the variable z. ***Since 5/2 does equal 2, Java assigns 2 to the variable z.

8 int x = 5; double y = 2; double z = x/y; what does z equal now? 2.5

9 int x = 5; int y = 2; double z = (double)(x/y); what does z equal now? 2.0 why? because x/y = 2, then 2 is type-casted (converted) into a double.

10 int x = 5; int y = 2; double z = (double)x/y; what does z equal now? 2.5 why? Notice the slight change in parentheses from the previous slide. First, x is type-casted from 5 to 5.0. Next, 5.0/2 = 2.5. Remember, as long as one of the two numbers being divided is a double, then the result is a double.

11 Variables do not have to be used in order to work with doubles and integers. For example:
System.out.println(9/4); (this would display 2) System.out.println(9.0/4); (this would display 2.25) System.out.println(9/4.0); (this would display 2.25) System.out.println(9.0/4.0); (this would display 2.25) Notice that as long as one of the two numbers being divided is a double, then the answer will be a double.

12 Modulus Division Using the modulus symbol, %, determines the remainder
10 % 6 = 4 13 % 5 = ? = 3 283 % 100 = ? = 83 26 % 2 = ? = 0 27 % 2 = ? = 1 (any even #) % 2 = ? (any odd #) % 2 = ? 3 % 8 = 3

13 Assignment #1 (InputNumbers)
Ask the user to enter 2 numbers and 1 letter. Display the letter 3 times on the same line, like this: “QQQ” Display the sum, difference, product, and quotient of the two numbers. Display the first number, squared. Display the second number, cubed. All of the results should look like the following, for example: “The sum of 6 and 8 is 14.”

14 Assignments (files should be named using this format: P117num4, P117num5, etc) P : #4 (Ignore “Create… application to”; look up formula online) #5 (remember: “floating point value” is another name for a double) #6 #7 (hints: use both modulus division and integer division; how many seconds are in an hour?) #11 (start by asking user for # of gallons used, starting odometer #, and ending odometer #) #12 (ask the user: how many of each coin?)


Download ppt "Other types of variables"

Similar presentations


Ads by Google