1 / 22

BIT115: Introduction to Programming

BIT115: Introduction to Programming. Lecture 7. Instructor: Craig Duckett. Assignment 2. DUE TONIGHT! Uploaded to StudentTracker by midnight I’ve already graded and returned about half the class . Assignment 1 Revision. DUE Wednesday, July 23 rd. Assignment 2 Revision.

brad
Download Presentation

BIT115: Introduction to Programming

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. BIT115: Introduction to Programming Lecture 7 Instructor: Craig Duckett

  2. Assignment 2 DUE TONIGHT! Uploaded to StudentTracker by midnightI’ve already graded and returned about half the class  Assignment 1 Revision DUE Wednesday, July 23rd Assignment 2 Revision DUE Monday, August 4th Assignment 3 “The Maze” DUE Wednesday, August 6th

  3. And now... ... The Quiz

  4. 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 intnum, 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: intnum = 0; intnum; num 0 intnum = 0; num 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);

  5. 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. intcounter = 0; 0 counter while(counter < 3){ move(); System.out.println("Counter is "+ counter); counter = counter + 1; // Or counter++; } 2 1 3 counter counter counter

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

  7. 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 and accept the same number of parameters, and return the same data type. See: ICE_10_Demo_1.java

  8. Class  Sub-Class  Sub-Sub-ClassSub-Sub-Sub-Class … classMrRoboto extends Robot{MrRoboto( City c, intst, intave, Direction dir, intnum) { super(c, st, ave, dir, num); }<<Services here>>} classNeuvoRoboto extends MrRoboto{NeuvoRoboto( City c, intst, intave, Direction dir, intnum) { super(c, st, ave, dir, num); }<<Services here>>} See: ICE_10_Demo_2.java

  9. ICE: Overriding Inherited MethodsWe’ll wait until the end of the LECTURE to do all the ICEs together

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

  11. Random Numbers • 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

  12. Random Numbers Java has a rich toolkit for generating random numbers, in a class named Random. Randomcan 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 Randomis 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 importjava.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 instanceof the Random class is used to generate a stream of pseudorandom numbers using a 48-bit seed, which is modified using a linear congruential formula: xi+1= (0x5DEECE66DL* xi + 11) mod 248 - 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 Randomare 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.

  13. 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 • Allfilesmust be together in the same area (folder, directory, desktop, drive, etc.) • jGraspautomaticallysaves 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.

  14. 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 filename • Allfilesmust be grouped togetherin the same area (folder, directory, desktop, drive, etc.) • jGraspautomaticallysaves 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.

  15. MrRoboto: 2 Styles, 1 File import becker.robots.*;classMrRoboto extends Robot { public MrRoboto(City c, int s, int a, Direction d) { super(c, s, a, d); } 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 lfp= new City();MrRobotolisa = new MrRoboto(lfp, 3, 2, Direction.SOUTH); lisa.move3();lisa.turnRight(); lisa.move3();lisa.turnAround(); lisa.move3();lisa.turnLeft(); lisa.move3();lisa.turnAround(); } } import becker.robots.*;public class MrRoboto extends Robot {// Construct a new MrRoboto public MrRoboto(City c, ints, inta, Direction d) { super(c, s, a, d); } 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 lfp= new City();MrRobotolisa = new MrRoboto(lfp, 3, 2, Direction.SOUTH); lisa.move3();lisa.turnRight(); lisa.move3();lisa.turnAround(); lisa.move3();lisa.turnLeft(); lisa.move3();lisa.turnAround(); } }

  16. SINGLE FILE | STYLE 1 importbecker.robots.*;publicclassMrRoboto extends Robot {publicMrRoboto(City theCity, intavenue, intstreet, Direction aDirection) { super(theCity, avenue, street, aDirection); }publicvoidturnAround() { this.turnLeft();this.turnLeft(); } public void move3() { this.move();this.move();this.move(); } public void turnRight() { this.turnAround();this.turnLeft(); }publicstaticvoid main(String[] args) { City bothell = new City();MrRobotolisa = newMrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3();lisa.turnRight(); lisa.move3();lisa.turnAround(); } } In this style, since there is only one class name, then the classnameMrRoboto, the constructornameMrRoboto, and the filenameMrRoboto must all be the same.

  17. import becker.robots.*;class MrRobotoextends Robot {// Construct a new MrRobotopublic MrRoboto(City theCity, intavenue, intstreet, 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 MrRobotoMainextends Object {publicstaticvoid main(String[] args) { City bothell = new City();MrRobotolisa = newMrRoboto(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

  18. import becker.robots.*;class MrRobotoextends Robot {// Construct a new MrRobotopublicMrRoboto(City theCity, intavenue, intstreet, 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.*; public class MrRobotoMainextends Object {publicstaticvoid main(String[] args) { City bothell = new City();MrRobotolisa = newMrRoboto(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

  19. MrRoboto.java THE METHOD FILE import becker.robots.*;classMrRobotoextends Robot{// Construct a new MrRobotopublicMrRoboto(City theCity, intavenue, intstreet, 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(); }} MrRobotoMain.java THE MAINFILE import becker.robots.*; publicclassMrRobotoMainextends Object {publicstaticvoid main(String[] args) { City bothell = new City();MrRobotolisa = newMrRoboto(bothell, 3, 2, Direction.SOUTH); lisa.move3();lisa.turnRight(); lisa.move3();lisa.turnAround(); } }

  20. Multiple Files Go in Same Folder/Directory MrRoboto.java THE METHOD FILE MrRobotoMain.java THE MAINFILE

  21. Mid-Term Review • Pencil and Paper Only…I’ll supply the paper • 150 Points | Worth 15% of Overall Grade • 150 Total Points • 6 - True/False Questions (6 x 5 points = 30 points) • 8 - Multiple Choice Questions (8 x 5 points = 40 points) • 2 - Traces ( 2 x 20 points = 40 points) • 1 - Writing Code Section ( 1 x 20 points = 20 points) • 1 - Finding Errors Section (1 x 20 points = 20 points) • Mid-Term Study Guide is available on BIT115 website

  22. ICE: Overriding Inherited Methods ICE: Multiple File ProgramsDo both sets of ICEs now… NOTE: ALWAYS download your multiple files from the website first and then work on them and run them from the same directory, or else things aren’t going to work correctly. NOTE: If you want to read how I constructed a City using a separate .txt 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…”

More Related