1/21
So fart: Printing, variables and types, user input, casting
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
How do you print “Hello World.”?
System.out.println("Hello world.");
How do you declare a variable that is all whole numbers and their negatives (integers)?
int num;
How do you declare a variable that is a real number that also has decimals?
double num;
How do you declare a variable that is a character of the alphabet?
char letter;
How do you declare a variable that represents a logical system that can only have one of two values: true/false, yes/no, etc?
boolean value;
How do you declare a variable that is composed of words
String words;
Why are strings unique?
Unlike int, double, boolean, or char, they are a non-primitive data type
User input for string?
String str = readLine(prompt);
User input for integer?
int num = readInt(prompt);
User input for double?
double myDouble = readDouble(prompt);
User input for boolean?
boolean bool = readBoolean(prompt);
Suppose you initialize two integer variables. How would you create variable sum, which represents the sum of both integers?
int sum = firstInteger + secondInteger;
Suppose a represents the int 20 and b represents the int 6. What would a / b result in?
Just 3; when dividing two integers, the result gets TRUNCATED.
If you divide two integers and the expected value is 3.9, what value does it truly become given the code is in Java?
Just 3; integer division always results in an integer and it is truncated, meaning rounding doesn’t occur
What is the result of 150 % 100
50
Is “#” an arithmetic operator
No; arithmetic means addition, subtraction, division, multiplication, and modulus
What is casting in Java?
Casting is turning a value of one type into another type.
What does (int) 9.9 simplify to?
9
How would you cast the double 10.9 to an int?
(int) 10.9 = 10;
How would you overcome integer division truncation?
Casting either the numerator or denominator as a double: 2 / (double) 3
While you could cast either the numerator or denominator for integer division, what is a more visually-appealing solution?
Casting outside of parentheses: (double) (2/3)
How do you round double values to the nearest integer value?
Add 0.5 to the double, then cast it to an integer will round it to the nearest integer.