Computing 1 Tutorial 3
Computing 1 Tutorial 3
Tutorial 3
TestComputeNumber.java
You need to compile these two files and run
}
ComputeNumber class
/**
* @(#)ComputeNumber.java ComputeNumber
*
class contain the
*
* @ng mee mee method named
* @version 1.00 2007/3/24 addNumber which
*/
accept two integer
values
public class ComputeNumber{
return n1 + n2;
}
}
/**
* @(#)TestComputeNumber.java
* The class definition of TestComputeNumber for setting
up the
* environment and test the ComputeNumber object
* @ng mee mee
* @version 1.00 2007/3/24 A driver program for
*/
ComputeNumber
public class TestComputeNumber{ named
TestComputeNumber
// the main execute method
public static void main(String[] args){
// create the ComputeNumber object
ComputeNumber compute = new
ComputeNumber();
}
}
Getting Input from Input Dialog
Boxes
String string = JOptionPane.showInputDialog(
null, “Prompt Message”, “Dialog Title”,
JOptionPane.QUESTION_MESSAGE));
Displaying text in console
What is System.out.println?
System.out is known as the standard
output object, println is a method in the
object
It is a collection of statements that
performs a sequence of operations to
display a message on the console.
Example:
System.out.println("The total =" + total);
Displaying Text in a Message
Dialog Box
double doubleValue
=Double.parseDouble(doubleString);
}
}
Boolean Operators
Boolean operators also known as logical operators.
A variable that holds a Boolean value is known as a
Boolean variable. i.e. boolean lightsOn = true;
Operator Name Description
! not logical negation
&& and logical conjunction
|| or logical disjunction
^ exclusive or logical exclusion
Truth Table for Operator !
p1 p2 p1 || p2 Example
false false false (2>3) || (5>5) is false
false true true
true false true (3>2) || (5>5) is true
true true true
System.out.println(aBoolean || anotherBoolean);
System.out.println(aBoolean && anotherBoolean);
System.out.println(!aBoolean);
int anInt = 5;
int anotherInt = 6;
if ( condition )
statement;
condition
evaluated
true false
statement
true false
statement1 statement2
if (radius >= 0) {
area = radius*radius*PI;
System.out.println("The area for the circle of radius " + ra
+
" is " + area);
}
else {
System.out.println("Negative input");
}
}
Nested if statements
The statement executed as a result of an
if statement or else clause could be
another if statement
These are called nested if statements
An else clause is matched to the last
unmatched if (no matter what the
indentation implies)
Braces can be used to specify the if
statement to which an else clause
Example: Nested if
statement
int i = 1; int j = 2; int k = 3;
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");
}
The if (j>k) statement is nested inside the if (i>k)
statement
Multiple Alternative if
Statements
if (score >= 90) if (score >= 90)
grade = ‘A’; grade = ‘A’;
else else if (score >= 80)
if (score >= 80)
grade = ‘B’;
grade = ‘B’;
else else if (score >= 70)
if (score >= 70) grade = ‘C’;
grade = ‘C’; else if (score >= 60)
else
grade = ‘D’;
if (score >= 60)
grade = ‘D’; else
else grade = ‘F’;
grade = ‘F’;
This is better
switch Statements
The switch statement provides another
means to decide which statement to
execute next
The switch statement evaluates an
expression, then attempts to match the
result to one of several possible cases
Each case contains a value and a list of
statements
The flow of control transfers to
statement associated with the first
switch Statements
The general syntax of a switch
statement is:
switch switch ( expression )
and {
case case value1 :
are statement-list1
reserve case value2 :
d statement-list2 If expression
words case value3 : matches value2,
statement-list3 control jumps
case ... to here
}
switch Statements
Often a break statement is used as the last
statement in each case's statement list
A break statement causes control to transfer
to the end of the switch statement
If a break statement is not used, the flow
of control will continue into the next case
Sometimes this can be appropriate, but
usually we want to execute only the
statements associated with one case
switch Statements
A switch statement can have an optional
default case
The default case has no associated value and
simply uses the reserved word default
If the default case is present, control will
transfer to it if no other case value matches
Though the default case can be positioned
anywhere in the switch, usually it is placed at
the end
If there is no default case, and no other value
matches, control falls through to the
Example: switch
statements
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
}
}
Conditional Operator
Java has a conditional operator that
evaluates a boolean condition that
determines which of two other expressions
is evaluated
The result of the chosen expression is the
result of the entire conditional operator
Its syntax is:
condition ? expression1 :
expression2
Conditional Operator (cont)
The conditional operator is similar to an if-
else statement, except that it forms an
expression that returns a value
For example:
larger = ((num1 > num2) ? num1 :
num2);
If num1 is greater that num2, then num1 is
assigned to larger; otherwise, num2 is
assigned to larger
The conditional operator is ternary
because it requires three operands
Conditional Operator
if (x > 0)
y = 1;
else
y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
Example: Conditional
Operator
public class TestConditional{
public static void main(String[] args){
int num=20;
if (num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Loop Statements
The while Loops
The do-while Loops
The for Loops
break and continue
The while Loop
The while statement has the following
syntax:
while ( condition )
while is a
statement;
reserved word
condition
evaluated
true false
statement
•A do loop is similar to a
while loop, except that the
statement
condition is evaluated
after the body of the loop true
is executed condition
•Therefore the body of a evaluated
data = Integer.parseInt(dataString);
sum+=data;
}while(data !=0);
condition
evaluated
true false
statement
increment
int i; false
i++ i<100?
for (i = 0; i<100;
i++) { true
System.out.println( System.out.println(
"Welcome to “Welcom to Java!”);
Java");
}
Next
Statement
Which Loop to Use?
A for loop may be used if the number of
repetitions is known, as, for example, when
you need to print a message 100 times.
A while loop may be used if the number of
repetitions is not known, as in the case of
reading the numbers until the input is 0.
A do-while loop can be used to replace a
while loop if the loop body has to be executed
before testing the continuation condition.
The break Keyword
• Break
immediately ends
the innermost
loop that contains
it.
• Generally used
with an if
statement
Example: break keyword
public class TestBreak{
public static void main(String[] args)
{
int sum=0;
int number=0;
while(number < 20){
number++;
sum+=number;
if(sum>=100)break;
}
System.out.println("The number is " +
number);
System.out.println("The sum is " + sum);
}
The continue Keyword