BIT115: Introduction to Programming

Slides:



Advertisements
Similar presentations
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
Advertisements

COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Lecture 9a Instructor: Craig Duckett CALL ME. Assignment 2 Revision DUE THIS Wednesday, August 5 th Assignment 3 DUE Monday, August 10 th Assignment 3.
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.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Object-Oriented Programming in C++
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Variables, Primitives, and Objects A Visual Learner’s Guide.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Constructors & Garbage Collection Ch. 9 – Head First Java.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
Lecture 13 Instructor: Craig Duckett CALL ME. Assignment 3 DUE TONIGHT! Uploaded to StudentTracker by midnight If you are submitting as part of a team.
Lecture 11 Instructor: Craig Duckett. Announcements Assignment 1 Revision DUE TONIGHT February 10 th In StudentTracker by midnight If you have not yet.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
OOP Basics Classes & Methods (c) IDMS/SQL News
Lecture 11 Instructor: Craig Duckett Instance Variables.
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”
1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction.
BIT 115: Introduction To Programming L ECTURE 3 Instructor: Craig Duckett
BIT115: Introduction to Programming
Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight.
Classes and Objects Introduced
Values vs. References Lecture 13.
Lecture 14 Throwing Custom Exceptions
Examples of Classes & Objects
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
Lecture 10: More on Methods and Scope
CS 106A, Lecture 2 Programming with Karel
Student Book An Introduction
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
BIT 115: Introduction To Programming
Chapter 4: Writing Classes
Namespaces, Scopes, Access privileges
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
BIT115: Introduction to Programming
CSC 253 Lecture 8.
Arrays & Functions Lesson xx
BIT115: Introduction to Programming
Introduction to Programming
Lesson 2: Building Blocks of Programming
BIT115: Introduction to Programming
Object Oriented Programming (OOP) LAB # 8
CSC 253 Lecture 8.
Functions BIS1523 – Lecture 17.
Classes & Objects: Examples
Java Programming Arrays
BIT115: Introduction to Programming
Chapter 4 Topics: class declarations method declarations
Namespaces, Scopes, Access privileges
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Lists.
A Methodical Approach to Methods
LCC 6310 Computation as an Expressive Medium
CISC101 Reminders Assignment 3 due today.
What to expect this week
References Revisted (Ch 5)
Visibilities and Static-ness
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

BIT115: Introduction to Programming Lecture 12 Instructor: Craig Duckett Instance Variables

Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) Section 1: Wednesday, October 11th Section 3: Thursday, October 12th Assignment 2 (LECTURE 9) Section 1: Wednesday, October 25th Section 3: Thursday, October 26th Assignment 1 Revision (LECTURE 11) Section 1: Monday, November 6th Section 3: Thursday, November 2nd Assignment 2 Revision (LECTURE 13) Section 1: Monday, November 13th Section 3: Thursday, November 9h Assignment 3 (LECTURE 15) Section 1: Monday, November 20th Section 3: Thursday, November 16th Assignment 3 Revision (LECTURE 18) Section 1: Wednesday, November 29th Section 3: Thursday, November 30th Assignment 4 (LECTURE 21) NO REVISION AVAILABLE! Section 1: Monday, December 11th Section 3: Tuesday, December 12th The Fickle Finger of Fate

And Now… … The Warm-Up Quiz!

Instance Variables

Instance Variables Temporary/Local Variables versus Instance Variables All the local variables we've looked at so far will disappear at the end of the service (method call). This means that they're temporary. We want memory that won't go away between service calls. We want the robot to remember something, to be able to keep a running tally that can be called up outside the scope of the method, not just get some temporary working space. An instance variable is what we need. A temporary variable can only be used inside the method where it is created. An instance variable may be used anywhere in the class containing it, by any of its methods. Remember: inside the class, but outside the methods and constructor. We might also manage this by declaring it private, which is foreshadowing the beginnings of encapsulation (although we aren’t quite there yet), although we don’t have to for what we’ll be doing with it.

These are Temporary Variables Method public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } numberOfIntersections counter Main System.out.println("How many intersections forward would you like the robot to go?"); if( keyboard.hasNextInt() ) { int numMoves = keyboard.nextInt(); System.out.println ("You entered a " + numMoves + "."); rob.moveMultiple(numMoves); }

Memory: The Stack and The Heap Methods Objects Main Array Data Primitive Variables Instance Variables The Stack Temporary Memory The Heap Persistent Memory Not to worry! We will go over this in more depth in an upcoming Lecture

Memory: The Stack and The Heap Int someNum = 5; #A234C7 #A234C7 5 Address The Stack Temporary Memory The Heap Persistent Memory Not to worry! We will go over this in more depth in an upcoming Lecture

Temporary Variables Suppose we want to recall a number from one method to use in a different method? With local (or temporary) variables we can’t do that since the variable space is no longer available once we leave the method This does not work as planned because int x and int y are not available to Method3 So how do we get around this limitation of local variables?

We Do It With Instance Variables We declare the variables inside the class but outside the methods, that way all the methods can access and use them We also declare them as private so only objects instantiated from the class that declared them can use and/or alter them, protecting them from outside interference (hence, private).

We Do It With Instance Variables We declare the variables inside the class but outside the methods, that way all the methods can access and use them See: InstVar_Demo_1.java InstVar_Demo_2.java

Adding Another Argument to Constructor Useful when you create a new robot Object with other functionality besides just its placement in the City Let’s say you want to create a new type of Robot that also starts with a limited number of actions it’s allowed to do when the program is run, say for example, 20 total actions. You can add another argument to its constructor parameters to hold this number (although you’ll still have to write the code to actually do something with this number)! So, how would you do this? You would create a new class that extends the Robot class You could create an instance variable to hold data You could create a new argument in the constructor’s parameter to enter this additional data You could assign this entered data inside the constructor to the instance variable to be used elsewhere in the program for whatever purposes you need. In main, you would have your new robot object do something with this new number (either subtract from it or add to it) Example on Next Page 

ExampleNewArgument.java class RechargableRobot extends Robot { private int numOfActions; // number of actions executed instance-variable private int maxNumOfActions; // max number of actions instance-variable RechargableRobot( City c, int st, int ave, Direction dir, int num, int numActions) super(c, st, ave, dir, num); //  Notice NO additional argument here for the superclass this.maxNumOfActions = numActions; // set the max number of actions } // Additional Methods go here public class ExampleNewArgument extends Object public static void main(String[] args) City toronto = new City(); RechargableRobot Jo = new RechargableRobot(toronto, 3, 0, Direction.EAST, 0, 20); new Thing(toronto, 3, 2); // Additional Code goes here

For Your Coding Pleasure … And Now, For Your Coding Pleasure …

Some Helpful Hints for Assignment 3 HINT #1: This assignment is using RobotSE which contains additional methods that can be used in the code for this assignment. For example: turnRight(), turnaround(), isfacingEast(), isFacingSouth(), isFacingWest(), isFacingNorth(), etc. See the Becker Library for reference. HINT #2: You will need to declare and initialize five (5) instance variables! These will be used to keep a running tally of the following: - Total Number of Moves Made - Total Number of Moves East - Total Number of Moves South - Total Number of Moves West - Total Number of Moves North HINT #3: You should create a method (e.g., movesCounted()) that will first keep a running tally of all the moves mentioned above and then move(). EXAMPLE: if the robot is facing east, tally move. HINT #4: You should create a method (e.g., printEverything()) that will print out the tally of all the moves made at the end of the NavigateMaze() method. HINT #5: Instead of using move() in the NavigateMaze() method, use movesCounted() which will not only move, but tally the move to the instance variables. HINT #6: All you need to add to the NavigateMaze() method to successfully navigate the Maze is fourteen (14) lines of code—and this includes the squiggles! If you need to use more lines with your logic, that’s okay too (it doesn’t only have to be 14). Also, the robot will only put down a thing if there is a thing in the backpack to put down, otherwise will still move without putting down a thing. HINT #7: Make sure and add backpack items to the robot object, don, down in main! Changing the 0 to at least 100 would be nice  (I will be testing your code with 100 items in backpack, and 33, and 10, and 0). Maze.java

class MazeBot extends RobotSE // <-- NOTE This is RobotSE class, not Robot class { public MazeBot(City theCity, int str, int ave, Direction dir, int numThings) { super(theCity, str, ave, dir, numThings); } // Five (5) Instance variables are declared and initialized here public void movesCounted() { //First tally the move and direction faced, then actually move } public void printEverything() { // Print everything (moves and directions faced) tallied to the instance variables above } private boolean isAtEndSpot() { return (this.getAvenue() == 9 && this.getStreet() == 10); } public void NavigateMaze() { while( !this.isAtEndSpot() ) { // All you really need is 14 lines of code to navigate Maze, including squiggles, // making sure only to put down a thing if there is a thing in the backpack, otherwise // just move without putting down a thing! Of course, if you need to use more than // 14 lines of code that’s okay too (for instance, 18 or 20 or 24 lines, but not 200!) } // Print everything here } } // Finally, down in main, change the number of things in backpack to at least 100

12