2/8: Algorithms, if & if/else structures
Download
Report
Transcript 2/8: Algorithms, if & if/else structures
7/12: Algorithms, if & if/else structures
•
•
•
•
•
•
Looking through AdditionApplet.java
Algorithms
if selection structure
if/else selection structure
about Thursday’s quiz
program of the day
AdditionApplet.java
import
statements
import java.awt.Graphics;
import javax.swing.*;
class
header
public class AdditionApplet extends JApplet {
declaring
double sum;
This is an instance variable. It can
a double
be used by all the methods
variable
associated with this class.
public void init()
method
header
{
declaring
String firstNumber, secondNumber; declaring
String
double
double number1, number2;
variables
variables
statements:
displaying
input dialog
boxes
firstNumber = JOptionPane.showInputDialog
( “Enter 1st value”);
secondNumber = JOptionPane.showInputDialog
( “Enter next value”);
AdditionApplet.java, pt.2
statements:
initializing
number1 &
number2
statement:
initializing
sum
statements:
drawing on
the Applet
window
}
number1 = Double.parseDouble ( firstNumber );
number2 = Double.parseDouble ( secondNumber );
sum = number1 + number2;
}
public void paint ( Graphics g )
{
g.drawRect ( 15, 10, 270, 20 );
g.drawString ( “The sum is “ + sum , 25, 25 );
}
Algorithms
• actions to be executed in a particular order.
• control structures: three kinds in Java
– sequential: “do this, then do that”
– selection: “if this, then do that”
– repetition: “do this while that”
Selection Structures
• if
if ( condition )
statement ;
• if / else
if ( condition )
statement ;
else
other statement ;
• switch
discussed later…
if: Multiple Resulting Actions
if ( condition ) {
statement ;
another statement ;
yet another statement ;
}
brackets surround the
statements to be done if
the condition is true.
if / else
if ( condition )
statement ;
else
other statement ;
• Notice that the else statement is associated with
the immediately preceding if rather than some
other one unless told otherwise.
if / else
Nesting if/else selection structures allows us to choose
from multiple possibilities:
if ( age < 18 )
JOptionPane.showMessageDialog ( null , “no vices”);
else
if ( age < 21 )
JOptionPane.showMessageDialog (null, “cigs only” );
else
JOptionPane.showMessageDialog ( null , “choose
your vice” );
if / else: writing it differently
if ( age < 18 )
JOptionPane.showMessageDialog ( null , “no vices”);
else if ( age < 21 )
JOptionPane.showMessageDialog (null, “cigs only” );
else
JOptionPane.showMessageDialog (null, “choose
your vice” );
• You can bump the nested if up to the else line to
keep the indentations to a reasonable level and to
increase readability.
About Thursday’s Quiz
• Correct that code: finding errors and fixing them in
snippets of code.
– EX:
if ( x < 10 ) ;
sum = y + 10 ;
• Create that code: writing lines of code to perform certain
actions.
– EX: Write a line of code to display a message dialog box titled
“NO!” with a “stop sign” icon displaying the phrase “Don’t
press that button!”
• What’s my output?: figuring out what a program will do
and look like.
– EX: similar to last quiz’s #10.
Program of the Day: Work in Teams
• Create a program that will ask the user for a
number between 0 and 100 ( decimal-type
numbers should be acceptable ), and display the
corresponding letter grade in a message dialog
box:
89.5 – 100 – A
79.5 – 89.4 – B
69.5 – 79.4 – C
59.5 – 69.4 – D
less than 59.5 – F