conditionals the vic class
play

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


  1. Conditionals

  2. The Vic Class a1 b1 ---- ---- e1 ---- g1 ---- end a2 ---- ---- d2 end a3 b3 ---- ---- e3 ---- g3 h3 end a4 ---- ---- d4 e4 end GarthB LyleL stack

  3. 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.

  4. The Vic Class public static void main( String[]args ) { Vic jane = new Vic(); Vic sally = new Vic(); } jane a1 b1 ---- ---- e1 ---- g1 ---- end sally a2 ---- ---- d2 end a3 ---- ---- ---- 3

  5. The Vic Class Vic jane = new Vic(); Vic sally = new Vic(); jane.moveOn(); jane a1 b1 ---- ---- e1 ---- g1 ---- end sally a2 ---- ---- d2 end a3 ---- ---- ---- 3

  6. The Vic Class . . . jane.moveOn(); sally.moveOn(); sally.moveOn(); jane a1 b1 ---- ---- e1 ---- g1 ---- end sally a2 ---- ---- d2 end a3 ---- ---- ---- 3

  7. The Vic Class . . . sally.moveOn(); jane.takeCD(); jane a1 ---- ---- ---- e1 ---- g1 ---- end sally a2 ---- ---- d2 end b1 a3 ---- ---- ---- b1 3 GarthB LyleL to stack stack

  8. The Vic Class . . . jane.takeCD(); sally.putCD(): jane a1 ---- ---- ---- e1 ---- g1 ---- end sally a2 ---- b1 d2 end a3 ---- ---- ---- 3 GarthB Lyle from stack stack

  9. The Vic Class public static void main( String[]args ) { Vic.reset( args ); } > java VicProgram 110010 1001 11001011 Use the java a1 b1 ---- ---- e1 ---- g1 ---- end command to create a a2 ---- ---- d2 end predictable number of rows and slots. a3 ---- ---- ---- 3

  10. The Vic Class public static void main( String[]args ) { Vic.reset( args ); } > java VicProgram #3 110010 1001 11001011 Use the java a1 b1 ---- ---- e1 ---- g1 ---- end command to create a a2 ---- ---- d2 end predictable stack Calexico configuration. GarthB a3 ---- ---- ---- 3 LyleL stack

  11. 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 .

  12. 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 .

  13. 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 .

  14. 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 .

  15. 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

  16. 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.)

  17. 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 on 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.

  18. The SmartVic Class SmartVic is a public class SmartVic extends Vic sublcass of { Vic public void moveTake() { moveOn(); moveTake is takeCD(); an instance method (not } static ) public void backPut() { Commands backUp(); apply to the putCD(); default } executor }

  19. 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(); } }

  20. 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.

  21. 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.

  22. 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.

  23. 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; } }

  24. 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!" ); } }

  25. 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" ); } }

  26. The If Statement (continued) 2. i f 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 ); } } }

  27. 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.

  28. 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?

  29. 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() ) Question: what will takeCD(); happen if you use this method while your Vic is at the end position of a row?

  30. 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(); } } } } } } }

  31. Exercises 1. Create and test the Shifter subclass. 2. Textbook, Chapter 2, page 2 ‐ 12: Exercises 2.14 ‐ 2.18.

  32. 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 • one CD. The stack is independent of any single arm, so stackHasCD is a class method. Class methods public class Test are invoked via { the class name. public static void main (String[ ] args) { Vic tim = new Vic(); if ( Vic.stackHasCD() ) Vic.say( "stack contains at least one CD." ); } }

  33. Exercises 1. Textbook, Chapter 2, page page 2 ‐ 15: Exercises 2.19 ‐ 2.20.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend