Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
your name
CSCI/CMPE 3326
Object-Oriented Programming in Java
Dongchul Kim
Department of Computer Science
University of Texas Rio Grande Valley
Statement, Expression, Operators, Operator 
Precedence, Conditional Statement
your name
Statement
• Statements control the 
flow of program execution.
• Examples
– Declaration statements
– Assignment statements
– Conditional statements
– Loop statements
– Method Call statements
– And so on
• Smallest standalone element of programming language
your name
Statement
• Three Classes of Statements
– Expression statements
• Declare, Assignment
– Compound statements
– Control statements
• Condition, Loop,…
your name
Expression
• A single entity (usually a number), such as a constant
• or variable, OR some combination of such entities
• Examples
– 5
– x
– a + b
– x++
– x <= y
– x == y
– x && y
your name
Operators
• Arithmetic operator
• Assignment operator
• Relational operator
• Logical operator
• And so on
your name
Arithmetic Operator
• +: plus 
• - : minus 
• / : division 
• * : multiplication 
• % : modulo 
– (remainder of after an integer division) 
• ++ : increment 
• -- : decrement 
• Examples
– 1+3 
– 4-1
– 2*4 
– 4/2 
– 5/0 (you cannot divide by zero)
– 3%2
– 4%1
– 5%0 error!
– (X)
– a++ 
– ++a
your name
Assignment Operator
• = 
• += 
• -= 
• *= 
• /= 
• %=
• Examples
– a = 1;
– a += 1; (a = a + 1)
– a -= 1; (a = a – 1)
– a *= 1;
– a /= 1;
– a %= 1;
your name
Relational/Logical Operator
• Relational Operator
– == Equal to (Don’t confuse with the assignment operator “=“)
– != Not equal to
– >
– <
– >= (=> gives an error)
– <= (=< gives an error)
• Logical Operator (boolean a, b)
– a && b Logical AND (short-circuiting)
– a & b  Boolean Logical AND (not short-circuiting)
– a || b Logical OR (short-circuiting)
– a | b Boolean Logical OR (not short-circuiting)
– !a  Logical NOT
your name
Operator Precedence
• Mathematical expressions can be very complex.
• There is a set order in which arithmetic operations will 
be carried out.
Higher
Priority
Lower
Priority
your name
Operator Precedence
Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment
= += -= *= /= %= &= ^= |= <<= >>= 
>>>=
your name
Useful Method
•Math.sqrt()
– Example
your name
Conditional Statement
• The if statement decides whether a section of code 
executes or not.
• The if statement uses a boolean to decide whether 
the next statement or block of statements executes.
if (boolean expression)
{
compound statements
}
your name
Relational Operators
• In most cases, the boolean expression, used by 
the if statement, uses relational operators.
Relational Operator Meaning
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to
== is equal to
!= is not equal to
your name
Boolean Expressions
• A boolean expression is any variable or 
calculation that results in a true or false condition.
Expression Meaning
x > y Is x greater than y?
x < y Is x less than y?
x >= y Is x greater than or equal to y?
x <= y Is x less than or equal to y.
x == y Is x equal to y?
x != y Is x not equal to y?
your name
Examples
if (x > y)
System.out.println("X is greater than Y");
if(x == y)
System.out.println("X is equal to Y");
if(x != y)
{
System.out.println("X is not equal to Y");
x = y;
System.out.println("However, now it is.");
}
your name
Programming Style and if Statements
• An if statement can span more than one 
line; however, it is still one statement.
if (average > 95)
grade = ′A′;
is functionally equivalent to
if(average > 95) grade = ′A′;
your name
Programming Style and if
Statements
• Rules of thumb:
– The conditionally executed statement should be on the 
line after the if condition.
– The conditionally executed statement should be 
indented one level from the if condition.
– If an if statement does not have the block curly braces, 
it is ended by the first semicolon encountered after the 
if condition.
if (expression)
statement;
No semicolon here.
Semicolon ends statement here.
your name
Having Multiple Conditionally-
Executed Statements
• Conditionally executed statements can be grouped into a 
block by using curly braces {} to enclose them.
• If curly braces are used to group conditionally executed 
statements, the if statement is ended by the closing curly 
brace.
if (expression)
{
statement1;
statement2;
} Curly brace ends the statement.
your name
Having Multiple Conditionally-
Executed Statements
• Remember that when the curly braces are not 
used, then only the next statement after the if
condition will be executed conditionally.
if (expression)
statement1;
statement2;
statement3;
Only this statement is conditionally executed.
your name
Comparing Characters
• Characters can be tested using the relational operators.
• Characters are stored in the computer using the Unicode
character format.
• Unicode is stored as a sixteen (16) bit number.
• Characters are ordinal, meaning they have an order in the 
Unicode character set.
• Since characters are ordinal, they can be compared to each other.
char c = ′A′;
if(c < ′Z′)
System.out.println("A is less than Z");
your name
if-else Statements
• The if-else statement adds the ability to 
conditionally execute code when the if
condition is false.
if (boolean expression)
{
statement;
}
else
{
statement;
}
if (boolean expression)
statement;
else
statement;
your name
Nested if Statements
• If an if statement appears inside another if
statement (single or block) it is called a nested if
statement.
• The nested if is executed only if the outer if
statement results in a true condition.
your name
if-else Matching
if (employed == ′y′)
{
if (recentGrad == ′y′)
{
System.out.println("You qualify for the " +    
"special interest rate.");
}
else
{
System.out.println("You must be a recent " +
"college graduate to qualify.");
}
}
else
{
System.out.println("You must be employed to qualify.");
}
This else
matches
with this
if.
This else
matches
with this
if.
your name
if-else-if Statements
if (expression)
{
statement or block
}
else if (expression)
{
statement or block
}
// Put as many else ifs as needed here
else
{
statement or block
}
your name
Quiz
int x = 9;
int y = 8;
int z = 7;    
System.out.println("Start");
if (x > 9)
if (y > 8)
System.out.println("x > 9 and y > 8");
else if (z >= 7)
System.out.println("x <= 9 and z >= 7");
else 
System.out.println("x <= 9 and z < 7");
System.out.println("Finish");
your name
Useful Method
• Math.random()
– Generate random number
– double and less than 1. 
• So, if you want to generate integer value,
– (int)(Math.random()*MAXINT) // random 
integer will be less than MAXINT.
• Example
your name
Type Casting
• For example, look at the following statements:
int x;
double y = 2.5;
x = y;
This statement will cause a 
compiler error because it is 
trying to assign a double
value (2.5) in an int variable.
your name
Type Casting
• The Java primitive data types are ranked, as 
shown here:
your name
Type Casting
• Widening conversions are allowed.
– This is when a value of a lower-ranked data type is 
assigned to a variable of a higher-ranked data type.
• Example:
double x;
int y = 10;
x = y; Widening Conversion
your name
Type Casting
• Narrowing conversions are not allowed.
– This is when a value of a higher-ranked data type is 
assigned to a variable of a lower-ranked data type.
• Example
int x;
double y = 2.5;
x = y; Narrowing Conversion
your name
Type Casting
• Cast Operators
– Let you manually convert a value, even if it means that 
a narrowing conversion will take place.
• Example:
int x;
double y = 2.5;
x = (int)y;
Cast Operator
your name
Lab3
Please at the top of the program, as a comment
include at a minimum your name, the date, and the lab 
number.
your name
Lab3-1
• Check if a given number is odd or even.
Please input an integer number: 36(enter)
36 is even.
Press any key to continue...
your name
Lab3-2
• Calculate GPA with letter grades of three classes.
Please input your grades: A B A(enter)
Your GPA is (answer).
Press any key to continue...
your name
Lab3-3
• Find max and min number from three integer numbers.
Please input three numbers: 9 13 2(enter)
MAX: 13
MIN: 2
Press any key to continue...
your name
Lab3-4
• Generate a random integer number between 1 and 3 
using modulus operator.
– i.e. 1, 2, or 3
Random number generation
2
Press any key to continue...
your name
Submission
• Please give a demo to your instructor or TA