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

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

Written by: Dr. JJ Shepherd
Road Map Introduction to object oriented programming. Classes
Guide To UNIX Using Linux Third Edition
Games and Simulations O-O Programming in Java The Walker School
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Lesson 6 – Libraries & APIs Libraries & APIs. Objective: We will explore how to take advantage of the huge number of pre-made classes provided with Java.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.
For Friday Finish reading chapter 2 Complete WebCT quiz.
Controlling Program Flow with Decision Structures.
BIT115: Introduction to Programming
Lecture 11 Instructor: Craig Duckett. Announcements Assignment 1 Revision DUE TONIGHT February 10 th In StudentTracker by midnight If you have not yet.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Monday, October 10 th I will double dog try to have.
BIT 115: Introduction To Programming L ECTURE 3 Instructor: Craig Duckett
BIT115: Introduction to Programming
Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
More About Objects and Methods
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
Chapter 7 User-Defined Methods.
Building Java Programs
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
Variables, Expressions, and IO
BIT 115: Introduction To Programming
Adding Assignments and Learning Units to Your TSS Course
BIT115: Introduction to Programming
CSC240 Computer Science III
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Lecture 11 C Parameters Richard Gesick.
CHAPTER 6 GENERAL-PURPOSE METHODS
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
BIT115: Introduction to Programming
Computing Fundamentals
Introduction to Primitives
BIT115: Introduction to Programming
BIT 115: Introduction To Programming
Introduction to Primitives
Instructor: Craig Duckett
BIT116: Scripting Lecture 6 Part 1
More on Creating Classes
CMSC 202 Exceptions.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Lecture 13 Instructor: Craig Duckett

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

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 ( ) and CC me ( ) to make a request to jump into BIT142 from BIT115 (explaining why you think you are a good candidate to do Once I receive your , I will 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 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

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

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

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

Some Quick Refreshers!

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);

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

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

Overriding Inherited Methods

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(); }

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

// 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 } }

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); } > }

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

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

Debugging Programmatically

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

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

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

Multiple Program Files

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.

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.

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

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

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

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

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

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.