Designing an APP for ordinary users requested by a bank.
Important considerations include:
Data Types
Java Syntax
Example scenario: Assigning a negative value for the radius in ComputeArea.java
.
Consequence: Program prints an invalid result.
Solution: Avoid computation of area if the radius is negative.
Definition: A variable that holds a boolean value is known as a boolean variable.
Usage: The boolean data type is used for declaring boolean variables.
Evaluation: A boolean expression evaluates to either true or false.
Example: boolean b = (1 > 2); // b is assigned the value false
In programming, it’s common to compare two values.
Java provides six comparison (relational) operators:
Example Comparisons:
i > j
radius > 0
Mathematical Name | Symbol | Example (radius = 5) | Result |
---|---|---|---|
less than |
|
| false |
less than or equal |
|
| false |
greater than |
|
| true |
greater than or equal |
|
| true |
equal to |
|
| false |
not equal to |
|
| true |
Definition: Use conditions that are boolean expressions.
Types of selection statements in Java:
One-way if statements.
Two-way if-else statements.
Nested if statements.
Multi-way if-else statements.
Switch statements.
Conditional operators.
Structure:
if (boolean-expression) {
statement(s);
}
Code Example:
if (radius >= 0) {
area = 3.14 * radius * radius;
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
Note: The boolean expression is enclosed in parentheses, block braces can be omitted for a single statement.
Structure:
if (boolean-expression) {
statement(s) for true-case;
} else {
statement(s) for false-case;
}
Code Example:
if (radius >= 0) {
area = 3.14159 * radius * radius;
System.out.println("The area of the circle of radius " + radius + " is " + area);
} else {
System.out.println("Negative input");
}
Task: Write a program to check if the integer number entered by the user is odd or even.
Syntax: Math.random()
produces a random double
Example: int x = (int) (5 * Math.random()) + 4
generates a random integer in the range [4, 9).
Write a program to:
Randomly generate two single-digit integers.
Display a question like "What is 3 + 4?".
After the user answers, indicate whether it is correct.
Definition: An if statement within another if statement.
Example:
if (i > k) {
if (j > k) {
System.out.println("i and j are greater than k");
}
} else {
System.out.println("i is less than or equal to k");
}
Task: Compute the letter grade based on numeric score (0-100).
A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: <60.
Structure: Check multiple conditions.
Example: Trace if-else statements to determine the grade based on a score.
Syntax:
switch (switch-expression) {
case value1:
statement(s)1;
break;
case value2:
statement(s)2;
break;
default:
System.out.println("Errors: invalid status");
}
Use if-then-else for ranges of values or conditions.
Use switch for a single integer or enumerated value.
Usage of ternary operator for concise conditions.
Example: Instead of using if-else, use: y = (x > 0) ? 1 : -1;
.
Operators and their meanings:
!
: NOT
&&
: AND
||
: OR
^
: Exclusive OR
Write a program to determine if a year is a leap year based on:
Divisible by 400
Or (divisible by 4 AND not divisible by 100).
Task: Prompt for IMDB rating and Metascore to determine if a movie is recommended.
Criteria: IMDB rating > 7.0 and Metascore > 60.
Task: Prompt for year and month to display the number of days in that month.
Leap year handling for February.