How do you identify and express the bigger number between the two numbers, x and y?
Math: Hypothesis and Conclusion
In a mathematical formula, we have to introduce the absolute sign to form an expression:
The bigger number between x and y
It is very straightforward and fairly easy to read!
The <Hypothesis> part needs to be a Boolean value, which may contain one variable or multiple variables in a math expression.
Simple if, or if/else clause.
A little more complicated if/else if ladder.
if (...) {......} else if (...) {......} else if (...) {......} else {......}Nested if/else statement.
if (...) {......} else {if (...) {......} else {...... ......}}
The final pattern is used to implement a tree-like structure. It will depend on the type of problems we solve when we decide which pattern to use.
Example
Answer
Example
Create a method to map a student’s grades (0 to 100 integers) to a standard GPA score.
Answer
Math: Quadrants
On the Cartesian coordinate system, a quadrant is determined by whether the x and y coordinates are positive or negative numbers. There are four quadrants, separated by the x-axis and the y-axis. Specifically, all the points (x > 0, y > 0) belong to quadrant I (or 1st quadrant); all the points (x < 0, y > 0) belong to quadrant II (or 2nd quadrant); all the points (x < 0, y < 0) belong to quadrant III (or 3rd quadrant); and all the points (x > 0, y < 0) belong to quadrant IV (or 4th quadrant)
Example
Can you write a method to identify which quadrant on the coordinate system that any given point (x, y) belongs to? Both x and y are real numbers. If a point falls on the x-axis or the y-axis, then the method should return 0.
Answer
There are two variables, x and y, in this example. Define x and y as float type of numbers. Do case work analysis as shown below:
Case 1: When a point falls on either x-axis or y-axis y = 0 or x = 0
Case 2: When a point falls in the 1st quadrant x > 0 and y > 0
Case 3: When a point falls in the 2nd quadrant x < 0 and y > 0
Case 4: When a point falls in the 3rd quadrant x < 0 and y < 0
Case 5: When a point falls in the 4th quadrant x > 0 and y < 0
It uses float to hold x and y values, although it could also use double to do so. Both float and double are numeric data types that are used for storing floating-point numbers. The double type requires twice as much space as the float type, as every float type of data is represented in 32 bits while one double type of data uses 64 bits
Ternary Operator
This implies, when a < b, max = b; otherwise max = a.
Problems
- 1.Please rewrite the code as below to improve its logic and readability (num is an integer value).if (num < 10 && num > 0) {System.out.println("It's an one digit number");}else if (num < 100 && num > 9) {System.out.println("It's a two digit number");}else if (num < 1000 && num > 99) {System.out.println("It's a three digit number");}else if (num < 10000 && num > 999) {System.out.println("It's a four digit number");}else {System.out.println("The number is not between 1 & 9999");}
- 2.Take the following three if statements:if (a == 0 && b == 0) {...}if (a == 0 && b != 0) {...}if (a != 0 && b != 0) {...}
Please simplify the code logic and combine them together.