Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab

Similar presentations


Presentation on theme: "Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab"— 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 1 Lab05 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 OR x++; //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 OR y--; //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();

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 x x<=5
Will happen LAST, every time loop executes Statement that will happen ONCE, before loop begins True or False Condition keyword x x<=5 for ( int x = 1 ; x<=5 ; x++ ) { karel.pickBeeper(); } TRUE TRUE TRUE Will happen each time loop executes TRUE TRUE FALSE

9 Converting Code Into A Loop
Look for repetitive code: 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? 3 times NO NO

10 Converting Code Into A Loop
karel.move(); karel.putBeeper(); for (int c = 5; c<=7; c++) { } c c<=7 TRUE TRUE TRUE 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 x x<=5 True or False Condition 1 TRUE keyword
Robot karel = new Robot (2, 4, Display.EAST, 5); //Make karel put down all of her beepers int x = 1; x x<=5 True or False Condition TRUE keyword TRUE while (x <=5) { karel.putBeeper(); x++; } TRUE TRUE TRUE Will happen each time loop executes 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 Returns True or False Condition keyword while (karel.frontIsClear()) { karel.move(); } 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’s front is NOT clear, she will turnLeft while (!karel.frontIsClear()) { karel.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)


Download ppt "Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab"

Similar presentations


Ads by Google