Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight.

Similar presentations


Presentation on theme: "Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight."— Presentation transcript:

1 Lecture 13 Instructor: Craig Duckett

2 Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight YOUR LAST CHANCE TO EARN POINTS! Assignment 3 “Maze” DUE Wednesday, November 16 th Uploaded to StudentTracker by midnight Lecture 14 (November 14 th ) NO LECTURE: Assignment 3 Work Day ATTENDANCE OPTIONAL

3 3 Assignment 2 Revision (LECTURE 13) TONIGHT Wednesday, November 9 th Assignment 3 (LECTURE 15) Wednesday, November 16 th Assignment 3 Revision (LECTURE 18) Monday, November 28 th Assignment 4 (LECTURE 21) Wednesday, December 7 th Assignment 4 Revision (LECTURE 22) Monday, December 12 th Assignment Dates (By Due Date) The Fickle Finger of Fate

4 By-Passing/Jumping BIT116  BIT142 BIT 115 (Intro to Programming)  BIT 116 (JavaScript)  BIT 142 (Intermediate Programming C#)  BIT 143 (Data Structures) SKIP BIT 116? Solid performance in this class: On pace to earn a 3.8 GPA or higher. Very comfortable and confident with this material Feel like you can handle more at a faster right. Contact Mike Panitz the Instructor of BIT142 via email ( mpanitz@cascadia.edu ) and CC me ( cduckett@Cascadia.edu ) to make a request to jump into BIT142 from BIT115 (explaining why you think you are a good candidate to do this).mpanitz@cascadia.educduckett@Cascadia.edu Once I receive your email, I will email Mike Panitz in private giving him my yea or nay as to whether I think you are a good candidate to make the jump. Once Mike hears from me he will then contact you with instructions how to take a PLACEMENT QUIZ to be sent out on TBA (1 to 4 Java or JavaScript exercises) which you will have to pass before you can register for BIT142 (you will have to print out the email you receive from Mike and register in- person at Kodiak. You cannot do this on-line, since it requires special permission).  Required to transfer to UW

5 Assignment 3 (“The Maze”) First, how many are working with a partner or partners? Show of hands Some Additional Comments… Please read the Directions carefully (Word doc), especially the comments in red. Look at the Rubric for a sense of grading In the Maze.java file, pay special attention to the comments; they are there to help you! If you don't like the comments, simply delete them. If you need a bit of help, are stuck, or have an error you are unable to debug, feel free to email me but make sure to attach your Maze.java file so I can check your code and offer the pertinent comments. Remember: Assignment 3 is due next Wednesday, November 16 th, uploaded to Student Tracker by midnight.

6 Today’s Topics Overriding Inherited Methods Using super. Debugging Using System.out Random Numbers Multiple Files

7 But First... The Warm-Up Quiz... The Warm-Up Quiz

8 Some Quick Refreshers!

9 Variables: Quick Refresher Declaring a variable creates a named storage location in memory large enough to hold a value of a particular type. For instance, if you declare a variable int num, then you are saying you need a storage location in memory named num that will be large enough to hold an integer (any number in the range of -2,147,483,648 and 2,147,483,647 that takes up a total of 32-bits of storage space). “Initializing the variable” simply means that you are putting an initial value in that named storage location: int num = 0; int num; num int num = 0; 0 Now, using code in your program you can access the value that is stored in that named storage location, or add to it, or subtract from it, or alter it in some other way, since it is a variable (meaning that it can be changed and it doesn’t have to remain the same, i.e., it isn’t constant). System.out.print("The storage container called num contains a " + num);

10 Counter: Quick Refresher Setting up a counter creates a named storage location in memory large enough to hold a value of a particular type. Counters are useful when working with loops. int counter = 0; counter while(counter < 3) { move(); System.out.println("Counter is " + counter); counter = counter + 1; // Or counter++; } 1 0 counter 2 3

11 Instance Variables: Refresher An instance variable is a variable that is declared and initialized inside a class, but outside of the methods. A variable declared inside a method is a local or temporary variable and can only be used by that method, but a global or instance variable declared outside of a method can be used by any of the methods or objects of that class. Local or Temporary Variables Only Local Variables AND Instance Variable

12 Overriding Inherited Methods

13 Inheritance: Quick Refresher import becker.robots.*; public class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } public void turnAround() { this.turnLeft(); } public void move3() { this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); }

14 Overriding Inherited Methods Java allows us to override methods inherited from the superclass using the super. (‘super dot’) keyword in the method. Both the original method and the overriding method must: have the same name declare the same data type accept same number of parameters return the same data type (we’ll be going over return in the next lecture) See: ICE_10_Demo_1.java

15 // DEMO 1: Overriding the turnLeft() method /////////////////////////////////////////////////////////////////// import becker.robots.*; class SpinningRobot extends Robot { SpinningRobot ( City c, int st, int ave, Direction dir, int num) { super(c, st, ave, dir, num); } public void turnLeft() { super.turnLeft(); // super here tells compiler to use turnLeft() super.turnLeft(); // from the super class Robot and not from its super.turnLeft(); // own class SpinningRobot. If we would have super.turnLeft(); // used a ‘this’ here instead of ‘super’ then super.turnLeft(); // we would find ourselves in an infinite loop } } public class ICE_10_Demo_1 extends Object { public static void main(String[] args) { City Dizzyland = new City(10, 10); SpinningRobot mary = new SpinningRobot(Dizzyland, 4, 4, Direction.EAST, 0); mary.turnLeft(); // mary turns left 5 times } }

16 Class  Sub-Class  Sub-Sub-Class  Sub-Sub-Sub-Class … See: ICE_10_Demo_2.java class MrRoboto extends Robot { MrRoboto( City c, int st, int ave, Direction dir, int num) { super(c, st, ave, dir, num); } > } class NeuvoRoboto extends MrRoboto { NeuvoRoboto( City c, int st, int ave, Direction dir, int num) { super(c, st, ave, dir, num); } > }

17 Robot class: move( ); RobotSE class: move(int howFar); Robot class: turnLeft( ); MrRoboto class: turnLeft( ); Overriding Same method name Same parameter Overloading Same method name Different parameter

18 ICE: Overriding Inherited Methods As always, we’ll wait until the end of the LECTURE to do all the ICEs together

19 Debugging Programmatically

20 Debugging: A Brief Look Using System.out for debugging In a method: System.out.println(this); With an object (for example, a Robot object named rigby): System.out.println(rigby); [street=1, avenue=4, direction=EAST, isBroken=false, numThingsInBackpack=3] With an object (for example, a Thing object named t1): System.out.printlin(t1); [street=1, avenue=4] DebugExample.java

21 Random Numbers

22 Depending on your program, sometimes it useful to be able to generate a random number (or numbers) that can be used to interact with your code to accomplish various tasks: Simple Gaming (spin the wheel, roulette, craps, etc) Advanced Gaming (from what coordinate locations the zombies will attack, where the artillery strike will hit, how much schrapnel will fly, the direction and amount of smoke in the wind, etc) Statistical Sampling (political affiliation, voter turnout, employment / unemployment numbers, etc) Computer Simulation (predicting earthquakes, paths of hurricanes, tide flows, etc) Cryptography (codes and encryption) Completely Randomized Design (nuissance variables, response variables) Text

23 Random Numbers Java has a rich toolkit for generating random numbers, in a class named Random. Random can generate many kinds of random numbers, but we’ll won’t going into these in this introduction. For more information, see http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Random.html Random is defined in the "java.util" library package, so any Java source file that uses Random must begin with a line of the form: import java.util.Random; or import java.util.*; The easiest way to initialize a random number generator is to use the parameterless constructor, for example: Random generator = new Random(); Random as it stands is NOT truly random. An instance of the Random class is used to generate a stream of pseudo random numbers using a 48-bit seed, which is modified using a linear congruential formula: x i+1 = (0x5DEECE66DL * x i + 11) mod 2 48 - 1 If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. Java implementations for Random have been developed this way for the sake of absolute portability of Java code. However, subclasses of Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods. Example: you can develop a subclass algorithm for Random that interacts with its seed in some way (through multiplication, division, a bit of both) like using the current system time from your computer (e.g., total seconds from some predefined date or randomized date). Some applications will find the random method in class Math (Math.random) simpler to use, as it creates a pseudorandom double greater than or equal to 0.0 and less than 1.0.

24 Multiple Program Files

25 Multiple Files (Programs) Separating main from classes to create more manageable code a main file one or more class files an optional.txt text file for configuration, additional input, or output (like creating a log file) Things to remember when dealing with multiple.java files: class name must match file name All files must be grouped together in the same area (folder, directory, desktop, drive, etc.) jGrasp automatically saves any changes made in the class files when compiling main, but I would get in the habit of recompiling all class files first before compiling main since other compilers behave differently.

26 import becker.robots.*; public class MrRoboto extends Robot { public MrRoboto(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); } } SINGLE FILE | STYLE 1 In this style, since there is only one class name, then: the class name MrRoboto, the constructor name MrRoboto, and the file name MrRoboto must all be the same.

27 import becker.robots.*; class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } } public class MrRobotoMain extends Object { public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); } } SINGLE FILE | STYLE 2 In this style, since there are two class names, then the file name must match the class name that contains the main method, MrRobotoMain. Also the class that holds the constructor and the new methods only starts with class, and the constructor starts with public. The class that holds main must start with public class

28 Splitting MrRobotoMain.java into Two Files METHODS and MAIN: 1 File with 2 Classes METHODS: 1 File with 1 Class MAIN: 1 File with 1 Class Example

29 import becker.robots.*; Needs its own ‘import’ statement class MrRoboto extends Robot { // Construct a new MrRoboto public MrRoboto(City theCity, int avenue, int street, Direction aDirection) { super(theCity, avenue, street, aDirection); } public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); this.turnLeft(); } } import becker.robots.*; Needs its own ‘import’ statement public class MrRobotoMain extends Object { public static void main(String[] args) { City bothell = new City(); MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); } } MrRoboto.java In this case, since there are two files, then the class names must match the files names, and both files must be in the same folder/directory. Each file needs to include the line import becker.robots.*; as well. MrRobotoMan.java Always compile the file that contains main when working with multiple files, since you cannot compile a file that does not contain main SPLIT IN TWO

30 Multiple Files Go in Same Folder/Directory MrRoboto.java THE METHOD FILE MrRobotoMain.java THE MAIN FILE The files need to be able “find” each other to work correctly, so storing them in the same directory or folder greatly simplifies this discovery process. NOW A DEMONSTRATION NOW A DEMONSTRATION Using MrRobotoMain.java

31 Assignment 3 Final Hint public void NavigateMaze() { while(!this.isAtEndSpot() ) { // Something happens here // Something happens here // Something happens here // if robot can not pick up a thing { // if robot still has things in its backpack { // put down a thing } } // Something happens here } // print everything here } NESTED IF STATEMENTS There is an if statement INSIDE an if statement With Pseudo-Code See MAZE city next slide for demonstration

32

33 ICE: Overriding Inherited Methods ICE: Multiple File Programs Let’s Look at Both ICEs Before Beginning NOTE : If you want to learn how I constructed a City using a separate text file, please refer to the Becker Robot Library > becker.robots > City and scroll down to the Constructor Detail on the right-hand side (the section begins with “Construct a new city by reading information to construct it from a file…”) REMEMBER: ALWAYS download your multiple files from the website first and then work on them and run them from the same folder/directory, or else things will not work correctly.


Download ppt "Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight."

Similar presentations


Ads by Google