Java Reference
In-Depth Information
11. Distinguish between “=” and “==”. The first operator is used to perform assignment,
while the second operator is used to check for equality.
12. The result of dividing two integers is an integer, where the result is rounded down.
For example, writing x =8 / 9 is equivalent to writing x =0.
13. Always include a break statement at the end of each case block inside a switch
statement.
14. Use the equals and compareTo methods to compare strings.
15. Use the statement return to exit any method, including the main method. Use this
statement instead of using the else part of an if statement where possible. In general,
avoid creating nested if statements and using the else construct where possible. This
makes the code easier to read.
16. Surround string literals in double quotes and character literals in single quotes.
2.11 Exercises
1. Write a program that prints your name and address. Feel free to use a fake address.
2. Write a program that reads two integers from the keyboard and prints their average.
Do not forget that dividing two integers always returns an integer.
3. Write a program that checks if the user is underage. If they are, then the program
should tell them not to go to the bar. If they are over 21, then the program should
print that they are allowed to go to the bar, but they should drink responsibly.
4. Write a program that prints two random integers (smaller than 100 and bigger than
0). The program should ask the user to multiply the numbers and read the result
from the user. If the user has done the multiplication correctly, then the program
should print “Congratulations!” Otherwise, the program should print “Better luck
next time.”
5. Write a program that reads an integer. If the integer is between 1 and 4, then the
program should print one , two , three ,or four , respectively. If the integer is not
between 1 and 4, then the program should print Number not between one and four .
Use a switch statement.
6. Write a program that chats with you. If you type Hi , then the program should print
Hi and terminate. If you enter Bye , then the program should print Bye and terminate.
If you enter anything else, the program should respond And how do you feel about
this? and terminate. Use a switch statement.
7. Describe how the computer executes the following Java statement.
int i=5;
8. Rewrite the following code using the conditional operator.
 
Search WWH ::




Custom Search