Using else if
|
No. 52
Replacing else if (...){...}
by
nested if ... else
statements
Q: |
A computer newbie did not yet read about the
TipAs the title suggests you may want to nest an
“inner” |
A: |
The solution requires replacing the
|
|
Enter a value:123 You entered 123 See |
No. 53
Post modifying an exam's marking
Q: |
A lecturer marks an exam having a maximum of 12 reachable points:
The lecturer is dissatisfied with the overall result. He wants to add 3 bonus points but still keeping the maximum of 12 points to be reachable:
Complete the following code by assigning this modified
number of points to the variable
|
|||||||||||||||||||||||||
A: |
We present three different solutions:
|
No. 54
At the bar
Q: |
This example uses existing program code to be explained later. You'll implement an interactive application which implements a dialogue with a user asking for input to be entered in a terminal like window as being shown in the following video: Figure 152. Using a
Scanner
class collecting user input.
A bar uses a software system for picking up orders. The bar will serve just orange juice and beer. For legal reasons the latter will only be served to persons of at least 16 years of age. We show three possible user dialogues:
Since you may not yet know how to enable Java™ applications asking for user input simply use the following recipe to get started:
Copy this boilerplate code into your IDE. The IDE will assist
you adding a required Please enter a value:112 You entered: 112 Then extend the above code implementing the desired behaviour. |
A: |
Nested
With respect to upcoming
|
No. 55
Roman numerals
Q: |
Write an application which turns a positive integer values up to and including 10 into Roman numeral representation: Enter a number:>9 IX Regarding user input you may start from ??? again. If the user enters a value smaller than one or greater than ten the following output is to be expected: Enter a number:>11 Decimal value 11 not yet implemented TipYou may use a series of |
A: |
|
Task: Convert day's numbers to day's names |
|
final Scanner scan = new Scanner(System.in));
System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : ");
final int number = scan.nextInt();
if (1 == number) {
System.out.println("Monday");
} else if (2 == number) {
System.out.println("Tuesday");
...
} else if (7 == number) {
System.out.println("Sunday");
} else {
System.out.println("Invalid number " + number);
}
No. 56
Leap years
Q: |
We want to write an application telling whether a given year is a leap year or not. The following dialogue may serve as an example: Enter a year:>1980 Year 1980 is a leap year Enter a year:>1900 Year 1900 is no leap year You may reuse the user input handling code from the previous examples. TipRead about the leap year algorithm. |
A: |
A first straightforward rule translation based solution reads:
This solution contains two identical
Some enthusiasts prefer compact expressions at the expense
of readability (“Geek syndrome”) sometimes referred
to as “syntactic sugar”. The following code based
on the
|