Conditionals The Vic Class a1 b1 ---- ---- e1 ---- g1 - - PowerPoint PPT Presentation
Conditionals The Vic Class a1 b1 ---- ---- e1 ---- g1 - - PowerPoint PPT Presentation
Conditionals The Vic Class a1 b1 ---- ---- e1 ---- g1 ---- end a2 ---- ---- d2 end a3 b3 ---- ---- e3 ---- g3 h3 end a4 ---- ---- d4 e4 end GarthB LyleL stack The Vic Class 1. The Vic
The Vic Class
a1 b1 ----
- e1 ----
g1 ---- end a2 ----
- d2 end
a3 b3 ----
- e3 ----
g3 h3 end a4 ----
- d4 e4 end
GarthB LyleL stack
The Vic Class
1. The Vic class simulates a CD changer. 2. You can have 1 to 4 rows of CDs. 3. Each row can have 1 to eight slots for storing CDs. 4. CDs can be stored on a stack when they are not in a slot. 5. Each row has a current slot indicated by a stick figure. 6. The stick figure can move one place past the last slot in a row.
The Vic Class
a1 b1 ----
- e1 ----
g1 ---- end a2 ----
- d2 end
a3 ----
- 3
public static void main( String[]args ) { Vic jane = new Vic(); Vic sally = new Vic(); } jane sally
The Vic Class
a1 b1 ----
- e1 ----
g1 ---- end a2 ----
- d2 end
a3 ----
- 3
Vic jane = new Vic(); Vic sally = new Vic(); jane.moveOn(); jane sally
The Vic Class
a1 b1 ----
- e1 ----
g1 ---- end a2 ----
- d2 end
a3 ----
- 3
. . . jane.moveOn(); sally.moveOn(); sally.moveOn(); jane sally
The Vic Class
a1 ----
- e1 ----
g1 ---- end a2 ----
- d2 end
a3 ----
- 3
. . . sally.moveOn(); jane.takeCD(); jane sally
b1
to stack
b1 GarthB LyleL stack
The Vic Class
a1 ----
- e1 ----
g1 ---- end a2 ---- b1 d2 end a3 ----
- 3
. . . jane.takeCD(); sally.putCD(): jane sally from stack
GarthB Lyle stack
The Vic Class
a1 b1 ----
- e1 ----
g1 ---- end a2 ----
- d2 end
a3 ----
- 3
public static void main( String[]args ) { Vic.reset( args ); } > java VicProgram 110010 1001 11001011 Use the java command to create a predictable number of rows and slots.
The Vic Class
a1 b1 ----
- e1 ----
g1 ---- end a2 ----
- d2 end
a3 ----
- 3
public static void main( String[]args ) { Vic.reset( args ); } > java VicProgram #3 110010 1001 11001011 Use the java command to create a predictable stack configuration.
Calexico GarthB LyleL stack
The Move‐On Method
The moveOn method takes no arguments. It advances the simulator "arm."
moveOn() This method tells the Vic simulator to advance an arm to the next position. If the arm is at the last slot it is advanced to the end position. You must not try to advance the arm past the end position.
The Back‐Up Method
The backUp method takes no arguments. It reverses the simulator "arm."
backUp() This method tells the Vic simulator to move an arm to the previous position. You must not try to move the arm before the first position.
The Take‐CD Method
The takeCD method takes no arguments. It moves a CD from a slot to the stack.
takeCD() This method tells the Vic simulator to take the CD from the current slot and add it to the stack. If there is no CD in the current slot no action is taken.
The Put‐CD Method
The putCD method takes no arguments. It moves a CD from the stack to a slot.
putCD() This method tells the Vic simulator to remove the CD at the top of the stack and put it in the current slot. If the stack is empty no action is taken.
The Say Method
The say class method takes one argument; it tells the Vic simulator to print a message.
Vic.say( message ) This method causes the Vic simulator to print a message in the white bar at the top of the simulator window. The previous message, if any, is erased.
Message goes here
a1
- e1 ----
- end
The Reset Method
The reset class method takes one argument; it tells the Vic simulator how to configure the rows, slots and stack.
Vic.reset( strings[] ) This method tells the Vic simulator how many rows to make, how many slots to put in each row, and, optionally, how many items to put on the stack.
(See previous slides for examples.)
Exercises
1. Write a program that uses the reset method to create a Vic simulator with three rows; the first should have 5 slots, alternating empty and occupied; the second should have 3 slots, all occupied; the third should have 6 empty slots.
- 2. Write a program to create a Vic simulator with 2 rows of 6 slots each, and 7 items
- n the stack.
- 3. In the following exercises ignore the instructions that make assumptions about the
configuration of a row or the stack; instead, use reset( args ) to ensure that the rows and/or stack are properly created. Textbook, Chapter 2, page 2‐6: Exercises 2.1 ‐ 2.5.
The SmartVic Class
public class SmartVic extends Vic { public void moveTake() { moveOn(); takeCD(); } public void backPut() { backUp(); putCD(); } }
SmartVic is a sublcass of Vic moveTake is an instance method (not static) Commands apply to the default executor
Testing the SmartVic Class
public class Test { public static void main (String[] args) { Vic.reset( args ); SmartVic tim = new SmartVic(); tim.moveTake(); tim.moveTake(); tim.backPut(); tim.backPut(); } }
Review
- subclass
SmartVic is a subclass of Vic.
- extends
SmartVic extends Vic.
- inherit
SmartVic inherits from Vic.
- superclass
Vic is a superclass of SmartVic.
- class method
Vic.reset is a class method.
- instance method
moveTake is an instance method.
Exercises
1. Write and compile SmartVic.java.
- 2. Write a program to test the SmartVic class.
- 3. Textbook, Chapter 2, page page 2‐9: Exercises 2.6 ‐ 2.12.
Boolean Values
- A Boolean value can be either true or false.
- In java, a Boolean value is represented by one of the keywords true or
false.
- Examples:
It is true that a cow is a mammal. It is true that a SmartVic is a Vic. It is false that all Turtles are SmartTurtles.
Boolean Types
- A variable or expression that can only be true or false is type boolean:
boolean firstOne = true;
- A method can be boolean, which means that it returns true or false:
public class TurtlePlus extends Turtle { public boolean areYouARabbit() { return false; } public boolean areYouATurtle() { return true; } }
The If Statement
The if statement can be used to test a boolean value:
public class TestIf { public static void main( String[] args ) { TurtlePlus timmy = new TurtlePlus(); if ( timmy.areYouATurtle() ) timmy.say( "I knew it!" ); } }
The If Statement
The if statement has two forms: 1. if condition statement If condition is true execute statement, otherwise skip to the first line following statement.
public class TestIf { public static void main( String[] args ) { TurtlePlus timmy = new TurtlePlus(); if ( timmy.areYouARabbit() ) timmy.say( "I knew it!" ); timmy.say( "next line starts here" ); } }
The If Statement (continued)
2. if condition { statements }
If condition is true execute all the statements inside the braces, otherwise skip to the next line following the right brace:
public class TestIf { static public void main( String[] args ) { TurtlePlus timmy = new TurtlePlus(); if ( timmy.areYouATurtle() ) { timmy.say( "I knew it!" ); timmy.paint( 90, 128 ); timmy.paint( -90, 64 ); } } }
Nested If Statements
An if statement can be nested within another if statement:
if ( condA ) { if ( condB ) { if ( condC ) { do something do something } // if condC is false, skip to here } // if condB is false, skip to here } // if condA is false, skip to here
Note: Indenting nested if statements is not required but it makes your code far more readable.
The Sees‐Slot Method
The seesSlot method takes no arguments. It returns true if the given Vic is at a valid slot.
if ( seesSlot() ) moveOn();
Question: when will the seesSlot method return false?
The Sees‐CD Method
The seesCD method takes no arguments. It returns true if the given Vic has a CD in its current slot.
if ( seesCD() ) takeCD();
Question: what will happen if you use this method while your Vic is at the end position of a row?
The Shifter Subclass
public class Shifter extends Vic { // If possible, shift the CD in slot 3 to slot 5. public void shiftThreeToFive() { if ( seesSlot() ) // at position one { moveOn(); // move to position two if ( seesSlot() ) { moveOn(); // move to position three if ( seesSlot() ) { if ( seesCD() ) { takeCD(); moveOn(); // move to position four if ( seesSlot() ) { moveOn(); // move to position five if ( seesSlot() ) putCD(); } } } } } } }
Exercises
1. Create and test the Shifter subclass.
- 2. Textbook, Chapter 2, page 2‐12:
Exercises 2.14 ‐ 2.18.
Review: Class vs. Instance Methods
- In the Vic class a method such as moveOn refers to any one of up to four
different "arms." To specify which arm is to be affected, invoke the method on a specific instance of a Vic. That makes moveOn an instance method.
- The method stackHasCD returns true if the simulator's stack contains at least
- ne CD. The stack is independent of any single arm, so stackHasCD is a class
method.
public class Test { public static void main (String[ ] args) { Vic tim = new Vic(); if ( Vic.stackHasCD() ) Vic.say( "stack contains at least one CD." ); } }
Class methods are invoked via the class name.
Exercises
1. Textbook, Chapter 2, page page 2‐15: Exercises 2.19 ‐ 2.20.
Block Statements
- A block statement, also called a compound statement, consists of one or
more additional statements enclosed in braces:
if ( tim.seesSlot() ) { Vic.say( "moving on" ); tim.moveOn(); }
- A block statement is considered a single statement, so we can say that if
the condition in an if statement is true the next statement is executed; if the condition is false the next statement is skipped.
The Else Statement
- An else statement is always paired with an if statement:
if ( condition ) is-true statement else is-false statement
- If condition is true, the is‐true statement is executed; if condition is false
the is‐false statement is executed.
- Remember that you can always use a block statement to encapsulate
multiple commands.
The Else Statement Example 1
public static void main( String[] args ) { Vic.reset( args ); Vic tim = new Vic(); if ( tim.seesSlot() ) Vic.say( "tim sees a slot" ); else Vic.say( "tim doesn't sees a slot" ); }
The Else Statement Example 2
public static void main( String[] args ) { Vic.reset( args ); Vic tim = new Vic(); if ( tim.seesSlot() ) { Vic.say( "tim sees a slot" ); tim.moveOn(); } else { Vic.say( "tim doesn't sees a slot" ); Vic.say( “tim can’t move on” ); } }
If‐Else Ladders
When making a simple choice from several alternatives you can use an if‐else ladder, sometimes called a selection block or a case construction (your textbook calls it a multiway selection).
if ( tim is an egg ) tim.scramble(); else if ( tim is a potato ) tim.boil(); else if ( tim is a fish ) tim.poach(); else if ( tim is a steak ) tim.grill(); else System.out.println( “How should I cook tim?" );
Exercises
- 1. Write a program that:
- moves an arm to the third slot of a sequence if it can; if the third slot doesn't
exist, say "third slot doesn't exist"...
- if the slot exists and contains a CD put the CD on the stack...
- if the slot exists and is empty, and if there is a CD on the stack, fill the slot...
- if the slot exists and is empty, and the stack is empty, say "I have been thwarted.“
- 2. Textbook, Chapter 2, page 2‐31:
Exercise 2.42.
The Not Operator
- The not operator (!) is used to negate the value of a condition. If an "arm"
labeled "tim" is currently at a slot:
tim.seeSlot();
will be true and
!tim.seeSlot();
will be false.
Not Operator Example
public static void main (String[ ] args) { Vic tim = new Vic(); Vic fred = new Vic(); if ( tim.seesSlot() ) Vic.say( "tim sees a slot" ); else Vic.say( "tim doesn't see a slot" ); if ( !fred.seesSlot() ) Vic.say( "fred doesn't see a slot" ); else Vic.say( "fred sees a slot" ); }
Review: Boolean Methods
public class VicPlus extends Vic { public boolean canTakeCD() { if ( seesSlot() ) if ( seesCD() ) return true; else return false; else return false; } } A return statement causes a method to terminate and return to the caller
Review: Boolean Methods, Example
public class Test { public static void main( String[] args ) { VicPlus tim = new VicPlus(); if ( tim.canTakeCD() ) Vic.say( "can take CD" ); else Vic.say( "cannot take CD" ); } }
Boolean Methods
You can use variables to simplify return logic.
public class VicPlus extends Vic { public boolean canTakeCD() { boolean rval = false; if ( seesSlot() ) if ( seesCD() ) rval = true; return rval; } }
Exercises
- 1. Textbook, Chapter 2, page 2‐21:
Exercise 2.24 – 2.26, 2.28, 2.29.
Boolean Operators: And
The and operator (&&) evaluates the truth value of two conditions:
condA && condB
The above expression is true only if condA is true and condB is true.
if ( tim.seesSlot() && tim.seesCD() ) tim.takeCD();
Boolean Operators: Or
The or operator (||) evaluates the truth value of two conditions:
condA || condB
The above expression is true if condA is true or condB is true.
if ( tim.seesCD() || !Vic.stackHasCD() ) Vic.say( "can't put CD" );
Expressions
An expression is any valid string of operators and operands, for example:
3 + 5 5 / 2 3 + 5 / 2 !stackHasCD() || !seesSlot() || seesCD() 1 rval = true rval = seesSlot() && !seesCD() Java has some
- perators you won’t
be familiar with from math class, such as ., =, (), []
Expression Evaluation
- Like arithmetic, operators have precedence and expressions are evaluated
accordingly:
3 + 5 / 2
5 is divided by two, then the result is added to 3
condA && condB || condC
condA is and'ed with condB, then the result is or'ed with condC
- Order of evaluation can be changed by introducing parentheses:
(3 + 5) / 2
5 added to 3, then the result is divided by 2
condA && (condB || condC)
condB is or'ed with condC, then the result is and'ed with condA
Short‐Circuits
In an expression involving and and or, the value of the first part of the expression will prevent the evaluation of the second part. This is called a short circuit.
- condA && condB
if condA is false condB will not be evaluated.
- condA || condB
if condA is true condB will not be evaluated.
if ( tim.seesSlot() && tim.seesCD() ) tim.takeCD(); If tim can’t see a slot he won’t check for a CD.
Exercises
- 1. Textbook, Chapter 2, page 2‐25:
Exercise 2.34 – 2.38