Activity 1.5.1.
What happens when you divide an int by an int or with a double operand or with the type cast (double) or (int) on one of the operands? Add another line that divides 5 by 2 using a (double) cast. What is the result?

(int) and (double) placed before any expression (a literal, a number, a variable, or more complex expression in parentheses) produces a value of the given type by converting the value of the original expression to the new type. The casting operators (int) and (double) can be used to convert from a double value to an int value (or vice versa). For example, (double) 1 / 3 will evaluate to a double value 0.33333333 instead of an int truncated value 0. And (int) 3.6 will evaluate to an int value 3 instead of a double. Casting an int value to a double adds on digits to the right of the decimal point. And casting double value to an int value causes the digits to the right of the decimal point to be truncated.int by an int or an int by a double or an int cast to a double divided by an int.ints, it produces an int result by truncating the actual mathematical result, removing anything after the decimal point. Thus 9 / 10 evaluates to 0, not 0.9. It also does not evaluate to 1; truncating is not the same as rounding!double, the double is “contagious” and will cause the value of that expression to also be a double. Thus the expression 9.0 / 10 is evaluated as if it had been written 9.0 / 10.0 and produces the double value 0.9. This code causes int values like 10 to be automatically cast (widened) to double values like 10.0.int to double, as shown in the code above, produces a double value which will then causes any expression it is part of to produce a double. This is especially useful when you have int variables that you want to do non-integer division with:int total; // a variable containing the sum of a bunch of ints
int count; // the number of ints that went into total
// Compute the average of the bunch of ints summed into total.
double average = (double) total / count;
int to double is called a widening conversion because a double can represent any int value but not vice versa; thus a double is considered a wider data type than an int.ints in Java are always 32-bit signed values which mean they can represent values from \(-2^{31}\) to \(2^{31} - 1\text{,}\) inclusive, while the range of consecutive integer values that can be represented by a double is from \(-2^{53}\) to \(2^{53}\text{,}\) inclusive. (A double can also represent much larger values but with limited precision.) You can refer to the minimum and maximum int values with the constants Integer.MIN_VALUE and Integer.MAX_VALUE.double in the range that can be represented by an int can be rounded to the nearest int by adding or subtracting 0.5 and then casting the result with (int):double number; // positive value from somewhere
double negNumber; // negative value from somewhere
int nearestInt = (int)(number + 0.5);
int nearestNegInt = (int)(negNumber – 0.5);
7.0 / 4.0 you get 1.75. If you cast that to an int, it will be truncated to 1. However if we want to round a double rather than truncating it, adding 0.5 will produce a number that is above the next integer value if the decimal part is greater than 0.5, as it is here. Then casting that value to an int will truncate down. So in this case 1.75 + 0.5 gives us 2.25 which is then truncated to 2. On the other hand adding 0.5 to the result of evaluating 5.0 / 4.2, namely 1.25, only gets us to 1.75 which truncates back to 1 which is the nearest integer to 1.25.double number to about 14-15 digits. You should be aware that the accuracy of any calculation on a computer is limited by the fact that computers can only hold a limited number of digits.Integer.MAX_VALUE holds the value of the largest possible int value in Java which is 2147483647, and Integer.MIN_VALUE holds the smallest int value -2147483648. Values in an int variable must be within the range of Integer.MIN_VALUE and Integer.MAX_VALUE inclusively. If you try to store any number larger or smaller than these numbers in an int variable, it will result in an integer overflow where an incorrect value could be stored. Try it below.double values, computers allot twice as much memory than for ints—8 bytes rather than 4. However it’s still possible that an expression would evaluate to a mathematical value that requires more precision than can be stored in even 8 bytes. In that case a round-off error occurs and the result is rounded to the nearest double representable in 8 bytes. For example, repeating decimals like 0.333333333333333333 will be cut off after about 15 digits. To avoid round-off errors, you can choose to round the result to a specified number of digits after the decimal point or to an int value.printf a formatted print method or format instead of println. It takes a format string like %.02f which tells printf to print a floating point number indicated by the % with 2 digits after the decimal point. See https://docs.oracle.com/javase/tutorial/java/data/numberformat.htmlhttps://docs.oracle.com/javase/tutorial/java/data/numberformat.html\n in the format string to do a newline. Try it below.https://www.w3schools.com/java/java_user_input.asphttps://play.juicemind.com/dashboard/teams/Mk2wWMTqPkekcxTDWqRn/item/5f00d71e-df8f-448f-a767-ed49e7af6f05#cb460a02-de03-4328-b310-ba057cb47a39https://replit.com/@BerylHoffman/Challenge1-6-Average-Template#Main.javahttps://en.wikipedia.org/wiki/List_of_Unicode_charactersU+1F600. But they’re just numbers. That last one is the same as 128512.char data type that can hold exactly 216 values. Then, seven months later, the Unicode folks, said, “Ooops, that’s not enough”, and extended their system to its current size of 1,112,064 possible codepoints. (As of September 2022, 149,186 have actually been used.)char kind of obsolete. But while not every Unicode codepoint can be represented in a Java char, you can use an int to represent a codepoint and the method Character.toString to translate an int into a String containing the character for that codepoint. (You might see older Java code that casts numbers to char; for many codepoints that will work but not on more recently added codepoints including, critically those for Emoji. 😞 So better to use Character.toString and ignore char.)https://unicodelookup.com/#cjk/1http://unicode.org/emoji/charts/full-emoji-list.htmlhttps://unicodelookup.com/https://unicodelookup.com/(int) and (double) can be used to convert from a double value to an int value (or vice versa).double value to an int causes the digits to the right of the decimal point to be truncated (cut off and thrown away).doubles, the double values are contagious, causing ints in the expression to be automatically converted (“widened”) to the equivalent double value so the result of the expression can be computed as a double.double can be rounded to the nearest integer by (int)(x + 0.5) or (int)(x – 0.5) for negative numbers.Integer.MAX_VALUE holds the value of the largest possible int value. The constant Integer.MIN_VALUE holds the value of the smallest possible int value.int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE, inclusive.