




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Materials for Exam One
Narrowing conversions need to be done through casting! i = f; // wrong assignment. Correct one is: i = (float)f and the value of i is the integer part of variable f. Try to understand: int total = 20, count = 7; float average; average = total / count; // 2. average = (float)total / count; // 2. average = total / (float)count; // 2. average = (float)(total / count); // 2. //casting in the last statement is redundant since //int->float is a widening conversion and could be //converted automatically
int i = 10; i /= 5 + 2; // value i =
1 because i = i/(5 + 2)
//generate a random number of 2, 3, 4, …, 11 randomeNumber = generator.nextInt(10) + 2; //generate a random number of -6, -4, -2, …, 24 randomeNumber = 2 * generator.nextInt(16) - 6; //generate a random number of -13, -11, -9, …, 57 randomeNumber = 2 * generator.nextInt(36) - 13;
"); int input = scan.nextInt(); while (input != -1) { sum += input; System.out.println("Please enter an integer (-1 to stop) > "); input = scan.nextInt();
System.out.println("Sum is: " + sum); } }