chapter 7 Flashcards

1
Q

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?

A

make sure you use a space in between the two amounts when typing into the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

in the prompt is it important the order of which you ask the user for 2 seperate amounts?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what happens when you divide an int value by another int value?

A

When you divide an int value by another int value, you get an int value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when you divide an int by another int, do you get a decimal number or remainder?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how can you divide in java so that you get a decimal numberas the answer?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what do you do if your intent is to get the remainder of the quotient of a number in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

another name for the remainder operator ?

A

Another name for the remainder operator is the modulus operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

why would you use whole numbers like an int to be divided rather than a double if you are trying to get the remainder?

A

because only int variables can give a remainder value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what type of numbers should you avoide and use when dealing with money dollars and cents? and why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is something to keep in mind when mixing int values and double values?

A

With a computer, you have to be very careful when you mix int values and double values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is it you need in order to cram a double value into an int?

A

To cram a double value into an int variable, you need something called casting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how do you do casting?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is it you need to create variables of different types? give an example.

A

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;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what would be the purpose for executing a call to System.out.println() with nothing in the parentheses?

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what 4 operators make life easier for your computer’s processor, for your brain, and for your fingers?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

briefly describe the increment and decrement operators.

A

The increment operators add one, and the decrement operators subtract one.

17
Q

what are one of the names of the increment operators , the characteristics and their function if the operator has to do with before a variable?

A

The double plus sign goes under two different names, depending on where you put it. When you put the ++ before a variable, the ++ is called the preincrement operator. In the word preincrement, the pre stands for before. In this setting, the word before has two different meanings: You’re putting ++ before the variable. The computer adds 1 to the variable’s value before the variable is used in any other part of the statement.

18
Q

what is the name of the increment operator that adds after the variable?

A

An alternative to preincrement is postincrement. With postincrement, the post stands for after. The word after has two different meanings: You put ++ after the variable. The computer adds 1 to the variable’s value after the variable is used in any other part of the statement.

19
Q

what happens when you print a postincrement after printing the old value that came before?

A

With System.out.println(A++), the computer adds 1 to “A” after printing the old value that “A” already had.

20
Q

what is usually more often used , increment or decrement operators?

A

Most programmers use postincrement. In a typical Java program, you often see things like gumballs++. You seldom see things like ++gumballs.

21
Q

besides the increment operators what other kinds of similar operators are there?

A

In addition to preincrement and postincrement, Java has two operators that use –. These operators are called predecrement and postdecrement:

22
Q

explain what is the predecrement opertaor?

A

With predecrement (–gumballs), the computer subtracts 1 from the variable’s value before the variable is used in the rest of the statement.

23
Q

explain what is the postdecrement opertaor?

A

With postdecrement (gumballs–), the computer subtracts 1 from the variable’s value after the variable is used in the rest of the statement.

24
Q

what can you do with an assignment operator? give examples on how to write an assignment operator for addition, and multiplication instead of adding more symbols such as ++

A

With an assignment operator, you can add, subtract, multiply, or divide by anything you want. You can do other cool operations, too. For example, you can add 1 to the kids variable by writing kids += 1; Is this better than kids++ or kids = kids + 1? No, it’s not better. It’s just an alternative. But you can add 5 to the kids variable by writing kids += 5; You can’t easily add 5 with preincrement or postincrement. And what if the kids get stuck in an evil scientist’s cloning machine? The statement kids *= 2; multiplies the number of kids by 2. With the assignment operators, you can add, subtract, multiply, or divide a variable by any number. The number doesn’t have to be a literal. You can use a number-valued expression on the right side of the equal sign:

25
Q

what are the 4 types of whole numbers in java?

A

byte, short, int, and long

26
Q

what is the range value of a byte number?

A

-128 to 127

27
Q

what is the range value of a short number?

A

–32768 to 32767

28
Q

what is the range value of a int number?

A

–2147483648 to 2147483647

29
Q

what is the range value of a long number?

A

–9223372036854775808 to 9223372036854775807

30
Q

what is the range value of a float number?

A

–3.4×1038 to 3.4×1038

31
Q

what is the range value of a double number?

A

–1.8×10308 to 1.8×10308

32
Q

why would it be a wiser choice to choose a double rather than a float in your code?

A

A variable of type double has a greater possible range of values and much greater accuracy.

33
Q

explain the program in the API whose method name is Character.toUpperCase

A

it changes a lowercase letter ot a capital.

34
Q

what happens if you use the Character.toUpperCase method if the letter is already a capital.

A

there will be no errors but what will be the result is it remains a capital when it is printed.

35
Q

what would happen if you assigned a number 3 to th Character.toUpperCase method when you print out the character?

A

instead of changing into another symbol or letter it remains a number 3 with no error messages

36
Q

if you dont understand something about computer programming what can you do and why would you do that for a solution ?

A

it often helps to write a test program. Make up an experiment and see how the computer responds.

The answers to questions aren’t handed down from heaven. The people who created the Java API made decisions. They made some obvious choices, and they also made some unexpected choices. No one knows everything about Java’s features, so don’t expect to cram all the answers into your head.