Lecture Notes, class 4 for csc211.
Prepared by: Anthony Larrain
These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.
The Scanner Class
- Provides an alternative to
JOptionPane
for getting input. - The
Scanner
class lives in packagejava.util
- Can use a
Scanner
object to read data from the standard input or console. - The
Scanner
class is NOT available to versions of Java previous to 5.0. - A
Scanner
object will convert an input value to a primitive type or String. - When creating a
Scanner
object we pass as an argument the objectSystem.in
- The following piece of code will allow the user to read an integer from the console.
- Should provide the user with instructions (prompt) on what to enter.
Some methods of the Scanner class
Scanner read = new Scanner(System.in); int age = read.nextInt();
Scanner read = new Scanner(System.in); System.out.print("Enter your age "); int age = read.nextInt();
Return Value | Method Name |
---|---|
int | nextInt() |
double | nextDouble() |
String | next() |
String | nextLine() |
See PoundsToKilograms.java and SimpleTax.java
Using JOptionPane
for Output
- Class
JOptionPane
has a static or class method namedshowMessageDialog
that pops up an output dialog box. - Method
- Method
- Keyword null is used to indicate the center of the screen.
- The above code produces the following dialog box.
showMessageDialog
returns void or nothing
showMessageDialog
requires at least two arguments
JOptionPane.showMessageDialog(null, "greetings");
Control Flow
Unless directed otherwise the statements of a method are executed sequentially, one after the other, starting with the first. This is called sequential execution . There are a few ways in Java to alter the program flow so that the next statement executed, may be other than the next one in sequence. This is called transfer of control
3 ways to alter the flow of control
- Invoking a method.
- Using a Selection Structure. (Decisions)
Deciding which statement(s) to execute next. The decision is based on a boolean expression , which is an expression that evaluates to either true or false .
- Using a Repetition Structure (Looping)
When a method is called (invoked) control jumps to the code that defines the method. When the method is finished control returns to the place in the calling method where the method was invoked.
Loops allow us to execute a statement or group of statements over and over again based on some boolean expression.
The if Statement
The if statement has the form.
if ( condition is true ) execute this-statement;
The condition enclosed in parentheses must be an expression that evaluates to true or false. Such an expression is called a boolean expression.
The if-else Statement
if( condition ) statementA; else statementB;
If the condition is true we execute statementA, if condition is false execute statementB.
Comparison Operators
- Equality operators
== equal to != not equal to
< less than <= less than or equal to > greater than >= greater than or equal to
The comparison operators:
- Have lower precedence than the arithmetic operators.
- Return a boolean value.(True or False)
See COperators.java
More Operators
- The Assignment Operator ( = ) , gives a variable a value.
- +=
int sum = 30; sum += 5; now sum is 35 Equivalent to sum = sum + 5;
- -=
int a = 5; a -= 2; a is 3 Equivalent to a = a - 2;
- *=
int x = 20; x *= 3; x is now 60 Equivalent to x = x * 3;
- /=
int z = 40; z /= 2; z is now 20 Equivalent to z = z / 2;
Shorthand Assignment Operators