chapter 7 Flashcards
when using the many different scanner methods, and getting a prompt to enter more than one amount of numbers how must you type in the console correctly so that the computer recognizes that youve entered 2 seperate amounts?
make sure you use a space in between the two amounts when typing into the console.
in the prompt is it important the order of which you ask the user for 2 seperate amounts?
yes if in the prompt amount a was first and and amount b came after you want to make sure you have everything in your code to align perfectly otherwise the ouput will be incorrect
what happens when you divide an int value by another int value?
When you divide an int value by another int value, you get an int value.
when you divide an int by another int, do you get a decimal number or remainder?
The computer doesn’t round. Instead, the computer chops off any remainder. If you put System.out.println(11 / 4) in your program, the computer prints 2, not 2.75.
how can you divide in java so that you get a decimal numberas the answer?
If you need a decimal answer, make either (or both) of the numbers you’re dividing double values. For example, if you put System.out.println(11.0 / 4) in your program, the computer divides a double value, 11.0, by an int value, 4. Because at least one of the two values is double, the computer prints 2.75.
what do you do if your intent is to get the remainder of the quotient of a number in Java?
There’s a useful arithmetic operator called the remainder operator. The symbol for the remainder operator is the percent sign (%). When you put System.out.println(11 % 4) in your program, the computer prints 3. It does so because 4 goes into 11 who-cares-how-many times, with a remainder of 3.
another name for the remainder operator ?
Another name for the remainder operator is the modulus operator.
why would you use whole numbers like an int to be divided rather than a double if you are trying to get the remainder?
because only int variables can give a remainder value
what type of numbers should you avoide and use when dealing with money dollars and cents? and why?
Throughout this book, I illustrate Java’s double type with programs about money. Many authors do the same thing. But for greater accuracy, avoid using double values for money. Instead, you should use int values or use the long values that I describe in the last section of this chapter.
what is something to keep in mind when mixing int values and double values?
With a computer, you have to be very careful when you mix int values and double values.
what is it you need in order to cram a double value into an int?
To cram a double value into an int variable, you need something called casting.
how do you do casting?
When you cast a value, you essentially say, “I’m aware that I’m trying to squish a double value into an int variable. It’s a tight fit, but I want to do it anyway.” To do casting, you put the name of a type in parentheses, as follows: //This works!
total = (int) (amount * 100); This casting notation turns the double value 138.00 into the int value 138, and everybody’s happy.
what is it you need to create variables of different types? give an example.
to create variables of different types, you need separate declarations. For example, to create an int variable named total and a double variable named amount, you need one declaration int total; and another declaration double amount;.
what would be the purpose for executing a call to System.out.println() with nothing in the parentheses?
When the computer executes a call to System.out.println() with nothing in the parentheses, the cursor jumps to a new line on the screen. (It is often used to put a blank line in a program’s output.)
what 4 operators make life easier for your computer’s processor, for your brain, and for your fingers?
Java has some neat little operators that make life easier (for the computer’s processor, for your brain, and for your fingers). Altogether, there are four such operators — two increment operators and two decrement operators.