|
|
|
|
|||||||||||||||||||||
|
|
Control Structures - Conditional
Statements |
|
|||||||||||||||||||||
|
|
Introduction Computer
programs are executed sequentially (i.e., statement by statement) unless
programmed to do otherwise. To get a
program to execute in a non-sequential manner, programmers insert control
structures which move the point of execution to another part of the program. Control
structures include conditional statements, loops, and the execution and
return statement of methods. We will
go into greater detail with respect to loops and methods in future lab
activities. Conditional
statements are of three types, the if statement,
the if…else statement and the switch statement. The syntax for each is as follows: |
|
|||||||||||||||||||||
|
|
|
|
|||||||||||||||||||||
|
|
|
|
|||||||||||||||||||||
|
|
In
the case of a conditional statement, the statements immediately following the if are
executed immediately if the condition or Boolean expression is evaluated to
be true. A Boolean expression is two
values or variables separated by a relational operator. The Boolean expression is enclosed in parentheses.
The relational operators, their meaning and examples of use are in the
following table:
|
|
|||||||||||||||||||||
|
|
Boolean
expressions can also be combined with logical operators so that multiple
statements are tested. The logical
operators are listed in the following table:
Conditional
statements can also be nested. This
means that the statements being executed after an if statement can also contain additional if statements |
|
|||||||||||||||||||||
|
|
|
|
|||||||||||||||||||||
|
|
Notice
how in nesting statements that are grouped together in a block,
that the curly braces associated with the block have the same level of
indentation. The statements within the
block should also have the same level of indentation. The reason for this is that it makes the
code easier for someone to examine or review. |
|
|||||||||||||||||||||
|
|
Procedure GUI’s or
Graphical User Interfaces 1.
Up until now we have been using the Keyboard class for inputting
data. The methods of the Keyboard
class allow the computer to parse the input and assign it to the proper primitive
data type. However, Java has a set of
dialog boxes that have been constructed (i.e
programmed) within the “swing” class the permit a much better looking way to
input data. To use the dialog boxes
you have to import the Java swing class methods at the beginning of your
program. Since the input data type is
a string, you will have to convert the string input to a data type of int.
2.
Write a short program that uses the above interfaces to input and
output an integer. Use the string
concatenation ability of Java to construct the output string, e.g.:
|
|
|||||||||||||||||||||
|
|
Programming Assignment Using
the Internet find a formula for calculating the date of the Christian holiday
Easter in any given year. Note that
Easter can be in March or April, so
your program will have to distinguish between whether Easter should be in
March or April. Your program should
also calculate the day for Ash Wednesday (46 days before Easter) and Ascension Day (39
days after Easter) You should make a new project. Use the GUI interfaces discussed above for
your input and output. Turn in a program listing your Easter Date source
code. Source code should use proper
formatting. Also turn in a copy of
your output. Output should be test
with the years 1960, 1985 and 2003.
This can be obtained by running the program and then using AltPrintScreen to copy the window of the output onto the
clipboard. Then paste the contents of the clipboard into WordPad and print
out the document. |
|
|||||||||||||||||||||
|
|
|
|