240 likes | 256 Views
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 .
E N D
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.
Computer Math Unit 1Lab05 and Lab06
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
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
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
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(); …
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
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
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
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
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
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
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()
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()
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
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
! • 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
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
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?
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?
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)
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(); }
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”);
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)