1 / 24

Unit 1 Test 1 Redo

Unit 1 Test 1 Redo. Friday at 8am here or Friday 4 th BLOCK in Math Lab If you have questions for me before the redo, you can come in Wed. morning. Computer Math. Unit 1 Lab05 and Lab06. Arithmetic in JAVA. When JAVA computes equations, it computes the right side of the equal sign first .

nilsag
Download Presentation

Unit 1 Test 1 Redo

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 1 Test 1 Redo • Friday at 8am here or Friday 4th BLOCK in Math Lab • If you have questions for me before the redo, you can come in Wed. morning.

  2. Computer Math Unit 1Lab05 and Lab06

  3. Arithmetic in JAVA • When JAVA computes equations, it computes the right side of the equal sign first. • If you have: int x = 1; • And you want to increase x by 1, you could say: x = x + 1; • First the current value of x would be increased by 1, then x would be replaced with the new value. • New value of x: 2

  4. Increment and Decrement Operators • A shortcut to increase any number by 1 is the increment operator ++ • If you have: int x = 1; • And you want to increase x by 1, you could say: x = x + 1; //x now equals 2 ORx++; //x now equals 2

  5. Increment and Decrement Operators • A shortcut to decrease any number by 1 is the decrement operator -- • If you have: int y = 10; • And you want to decrease y by 1, you could say: y = y - 1; //y now equals 9 ORy--; //y now equals 9

  6. Tired of Typing? • Often we find ourselves typing the same code over and over again • Example: There is a pile of 5 beepers. You want karel to pick them all. karel.pickBeeper(); karel.pickBeeper(); …

  7. For Loop! • When we find ourselves typing the same code over and over again AND • We know exactly how many times you want something to happen: definite iteration • Use a for loop

  8. Format and Example Will happen LAST, every time loop executes Statement that will happen ONCE, before loop begins for ( int x = 1 ; x<=5 ; x++ ) { karel.pickBeeper(); } True or False Condition keyword 1 TRUE 2 TRUE 3 TRUE Will happen each time loop executes 4 TRUE 5 TRUE 6 FALSE

  9. 3 times Converting Code Into A Loop • Look for repetitive code: karel.move(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.move(); karel.putBeeper(); • Count the repetitions, this will be how many times you want your loop to execute. • Set up the loop. Does your integer have to be named x? • Does it have to start at 1? NO NO

  10. Converting Code Into A Loop karel.move(); karel.putBeeper(); karel.move(); karel.putBeeper(); karel.move(); karel.putBeeper(); for (int c = 5; c<=7; c++) { karel.move(); karel.putBeeper(); } 5 TRUE 6 TRUE 7 TRUE 8 FALSE

  11. While Loop! • When we find ourselves typing the same code over and over again AND • We DO NOT KNOW how many times you want something to happen: indefinite iteration • Use a while loop

  12. Format and Example Robot karel = new Robot (2, 4, Display.EAST, 5); //Make karel put down all of her beepers int x = 1; while (x <=5) { karel.putBeeper(); x++; } True or False Condition 1 TRUE keyword 2 TRUE 3 TRUE 4 TRUE 5 TRUE Will happen each time loop executes 6 FALSE

  13. Void Methods • So far we have only used void methods • This means the method does something, but does not return a value (return an answer) • The headers of void methods have the keyword void: public void turnRight()

  14. Return Methods • Sometimes methods return a value (give you an answer) • Example: maybe a method adds two numbers and returns the answer • The headers of return methods have the keyword of the type of data they return public int addTwoNumbers()

  15. Return Methods - Boolean • Some methods return a boolean value (true or false) • These methods are useful in loops • Robot has many boolean methods: • public boolean frontIsClear() • public boolean nextToABeeper() frontIsClear returns true frontIsClear returns false

  16. What is she talking about??? Robot karel = new Robot (2, 4, Display.EAST, 5); //Make karel move while her front is clear while (karel.frontIsClear()) { karel.move(); } Returns True or False Condition keyword Will happen each time loop executes

  17. ! • The ! is called the not operator • Place it in front of a boolean condition to alter the test while (!karel.frontIsClear()) { karel.turnLeft(); } While karel’s front is NOT clear, she will turnLeft

  18. Let’s Write Some Labs!Starting Lab05 • Create a new java class named Racer • Copy code from TJ Packet Page 21 • Racer extends Athlete • The pseudocode is given to you for the method called jumpRight • The sprint method is totally complete • You must define the put and pick methods

  19. Lab05 - Constructor • Racer Constructor definition (Inside of Racer) public Racer(int y) { super(1, y, Display.EAST, Display.INFINITY); } • Using the Constructor(inside of Lab05) Racer r = new Racer(3);//what happens to 3?

  20. Lab05 - Constructor • Sprint method definition (Inside of Racer) public void sprint(int n) { for(int k = 1; k< = n; k++) { move(); } } • Using the sprint method(inside of Lab05) r.sprint(5);//what happens to 5? r.sprint(3);//what happens to 3?

  21. Lab05 • Complete Racer – Compile • Create Lab05, using Lab04 as a template • Create a Class method called runTheRace • This method will be similar to takeTheField • Inside of runTheRace, you will invoke the Racer methods • Inside of main, create 3 Racers. Have them each runTheRace • (The Racer class will use for loops)

  22. Lab06 • 6 Robots each responsible for a different task. • The Robots must be able to perform the tasks on 3 different Displays. • Example: Task 3 Go to the wall while(temp.frontIsClear()) { temp.move(); }

  23. Lab06 • Create a dialog box that will pop up and allow you to type the name of the Display into the program when it runs. • Add:import javax.swing.JOptionPane; Instead of: Display.openWorld(“maps/first.map”); ADD: String filename = JOptionPane.showInputDialog("What robot display?");Display.openWorld(“maps/” + filename + “.map”);

  24. Lab06 • Open the Lab06 shell from your Unit 1 Folder • Create 6 class methods, each responsible for job (one has been done for you – USE IT AS A GUIDE) • In the main method, invoke these methods • Check your code with all 3 Displays. • (Lab06 will use while loops)

More Related