Presentation is loading. Please wait.

Presentation is loading. Please wait.

Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

Similar presentations


Presentation on theme: "Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”"— Presentation transcript:

1 Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

2 Lecture 13 Attendance Announcements Lecture ICE13

3 Attendance

4 Announcements Mid-Course Survey Did you miss out on 5 points? It’s anonymous! Assignment 1 Revision due today Java Code Critic Feedback Read even if “Well done!” or “Done” Are you putting enough time into this course? Are you asking enough questions during ICE time? BIT 115: Introduction To Programming4

5 Group Work Common in industry (extreme/agile programming) Exchange contact info Email Cell Phone Share with your group what you are good at what are your challenges Plan a 2-hour meeting outside of class Don’t break up the work, instead collaborate & discuss together Take turns typing on 1 computer during the meeting A3 is due at 3:30pm, Thursday, (this week) Afterwards: We will discuss what worked well and what didn’t A4 will be group work too Complete the “Assignment 4 Partner Preferences” survey by midnight Thursday night Student groups will Peer Review other groups’ A3 work just after it is due So don’t be late with turning in A3! BIT 115: Introduction To Programming5

6 Assignment 3 (“The Maze”) Some Additional Comments… Please read the Directions carefully, especially the comments in red. Read the Guide & Rubrics for a sense of grading In the Maze.java file, pay special attention to the comments; they are there to help you! If you need a bit of help, are stuck, or have an error you are unable to debug, feel free to contact 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 Thursday, May 12 th by 3:30PM (so students can peer review it).

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

8 Overriding Inherited Methods

9 Inheritance: A 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(); }

10 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_13_Demo_1.java

11 Method Overriding vs. Method Overloading Don’t confuse method overriding (today’s lecture) same method name of a super class with same parameters with method overloading (last lecture) same method name of the current class or a super class with different parameters

12 Debugging

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

14 Class  Sub-Class  Sub-Sub-Class  Sub-Sub-Sub-Class  and so on… 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); } > } See: ICE_13_Demo_2.java

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

16 Random Numbers

17 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, 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 (nuisance variables, response variables)

18 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 https://docs.oracle.com/javase/8/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.

19 Multiple Program Files

20 Separating main from new classes and new class methods to create more manageable code a main file one or more class/method 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 folder/directory (desktop, drive, etc.) jGrasp automatically saves any changes made in the class files when compiling main Eclipse automatically saves any changes made in the class files when java files are saved (compiles everything in the background)

21 A Refresher: Using Single Files

22 import becker.robots.*; class MrRoboto 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(); MrRoboto lisa = new MrRoboto(lfp, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); lisa.move3(); lisa.turnLeft(); lisa.move3(); lisa.turnAround(); } } MrRoboto: 1 File, 2 Styles import becker.robots.*; public class MrRoboto extends Robot { // Construct a new MrRoboto 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 static void main(String[] args) { City lfp = new City(); MrRoboto lisa = new MrRoboto(lfp, 3, 2, Direction.SOUTH); lisa.move3(); lisa.turnRight(); lisa.move3(); lisa.turnAround(); lisa.move3(); lisa.turnLeft(); lisa.move3(); lisa.turnAround(); } } MrRoboto.java MrRobotoMain.java One Class Style Multiple Class Style

23 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.

24 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

25 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

26 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

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(); } } import becker.robots.*; 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 THE METHOD FILE MrRobotoMain.java THE MAIN FILE

28 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

29 ICE13 Most ICE13 groups are a copy of A3 groups, so this is also the time to exchange contact info (phone & email) & plan your A3 meeting Don’t break up the work, do it together collaboratively I’ll accept today’s ICE13 as late as the beginning of Tuesday’s class, so if your group doesn’t finish tonight they can finish during the meeting(s) later this week

30 ICE: Overriding Inherited Methods ICE: Multiple File Programs Work together in ICE13 Groups on 1 computer 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 "Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”"

Similar presentations


Ads by Google