Lecture 11 Instructor: Craig Duckett Instance Variables.

Slides:



Advertisements
Similar presentations
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Advertisements

Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
Access to Names Namespaces, Scopes, Access privileges.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
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.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
ArrayList, Multidimensional Arrays
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
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Object-Oriented Programming in C++
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
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.
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
Programming for Geographical Information Analysis: Core Skills Lecture 4: Program Flow II Methods.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member 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”
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.
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Lecture 10: More on Methods and Scope
BIT 115: Introduction To Programming
Chapter 4: Writing Classes
BIT115: Introduction to Programming
CSC 253 Lecture 8.
Arrays & Functions Lesson xx
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Object Oriented Programming (OOP) LAB # 8
CSC 253 Lecture 8.
Chapter 4 Topics: class declarations method declarations
BIT115: Introduction to Programming
A Methodical Approach to Methods
Presentation transcript:

Lecture 11 Instructor: Craig Duckett Instance Variables

2 Assignment 1 Revision (LECTURE 10) GRADED! RETURNED! Assignment 2 Revision (LECTURE 12) THIS Wednesday, May 4 th Assignment 3 (LECTURE 13) NEXT Monday, May 9 th Assignment 3 Revision (LECTURE 16) Wednesday, May 18 th Assignment 4 (LECTURE 19) Wednesday, June 1 st Assignment 4 Revision (LECTURE 20) Monday, June 6 th Assignment Dates (By Due Date) The Fickle Finger of Fate

Warm-Up Quiz And Now… … The Warm-Up Quiz !

Instance Variables

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.

public void moveMultiple(int numberOfIntersections) { int counter = 0; while( counter < numberOfIntersections) { if(this.frontIsClear()) { this.move(); counter = counter + 1; } } 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); } These are Temporary Variables Method Main numberOfIntersections counter

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

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

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?

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

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

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 } ExampleNewArgument.java

And Now, For Your Coding Pleasure …

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.Becker Library 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). Some Helpful Hints for Assignment 3 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