CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Access to Names Namespaces, Scopes, Access privileges.
CS 1110 Prelim I: Review Session. Exam Info Prelim 1: 7:30–9:00PM, Thursday, 6 October, Statler Auditorium Look at the previous Prelims Arrive early!
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.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
CS 1110 Prelim I: Review Session. Introduction My name: Bruno Abrahao – We have four TA’s in the room to help you individually Shuang Zhao Nam Nguyen.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
Methods.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
Problem of the Day  Why are manhole covers round?
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Arrays Chapter 7.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Functions + Overloading + Scope
Classes and Objects.
Java Memory Management
Object Oriented Programming
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Software Development Java Classes and Methods
Week 8 - Friday CS 121.
Method Parameters and Overloading
Repetition-Counter control Loop
#include "std_lib_facilities
CS 302 Week 11 Jim Williams, PhD.
CS139 – Fall 2010 How far we have come
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Organizing Memory in Java
Class Structure 28-Nov-18.
Interfaces and Constructors
Call Frames Final Review Spring 2018 CS 1110
Sit next to someone. Today, we do some work in pairs.
CHAPTER 6 GENERAL-PURPOSE METHODS
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS2011 Introduction to Programming I Methods (II)
Chapter 4 Topics: class declarations method declarations
Sit next to someone. Today, we do some work in pairs.
Chapter 6 Methods.
Namespaces, Scopes, Access privileges
Stacks.
Class Structure 25-Feb-19.
CIS 199 Final Review.
Classes, Objects and Methods
CS 1110 Prelim I: Review Session Spring 2011
Review for Midterm 3.
Corresponds with Chapter 5
Lec 21 More Fun with Arrays: For Loops
The for Loop © 2018 Kris Jordan.
Presentation transcript:

CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method calls. Learning to do this will help you to do the same thing when trying to find errors in your programs.

Understanding execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples from previous exams code execution (Q4 from 2008 fall final, modified) method call (Q3 from 2007 fall final)

Important! All previous finals included some questions about code execution You need to know how to draw variables, objects, method frames … The purpose of such questions on executing statements with new expressions and method calls is to test your understanding of how java programs are executed

code segment (in a method body) int a= 3; C x= new C(a); C y= new C(a); x= y; The first thing to do? draw all local variables

code segment (in a method body) public class C { private int f; public C(int k) { f = k; } } x y a 3 int a= 3; C x= new C(a); C y= new C(a); x= y;

Evaluation of new expression 3 steps in evaluating the new expression new C(args) create a new folder (object) of class C with a unique name (place it in the class file drawer) Execute the constructor call C(args) yield the name of the object as the value of the new expression

code segment (in a method body) public class C { private int f; C(int k) { f= k; } } x y a a1 3 a0 a1 int a= 3; C x= new C(a); C y= new C(a); x= y; C f C(int); a1 C f C(int); a0 3 3 aliasing

variables declared in a loop When is local variable b inside the loop created? During the first step of executing a method call, when the frame for the call is drawn. Not after the loop starts. public void m(int size) { for (int i= 0; i< size; i= i+1) { int[] b = …; … }

Code Execution (Q4 from 2008 fall final, modified) Execute the call: Store.session(); public class Item { /* total cost of all items created */ private static int totalCost = 0; private int cost; // cost of this item private String name; // title /** Constructor: new Item with name t, cost c */ public Item(string t, int c) { name = t; cost = c; totalCost = totalCost + c; } /** = Cost of this item */ public int getCost() { return cost; } /** = this item’s name */ public String getName() { return name; } /** = “<name>:<cost>” */ public String toString() { return name + “:” + getCost(); } /** Add d to this item’s cost */ public void add(int d) { cost = cost + d; totalCost = totalCost + d; /** = the total cost of all Items */ public static int getTotalCost() { return totalCost; } public class Store { public static void session() { 1: Item one = new Item(“ipod”, 20); 2: Item two = new Item(“wii”, 32); 3: Item treat = two; 4: Item three = one; 5: three.add(4); 6: System.out.println(one); 7: System.out.println(“Cost of Item: “+ Item.getTotalCost()); 8:System.out.println(“Are they the same?” + (one.getName() == treat.getName()) ); 9:System.out.println(“Are they the same?” + one.getName().equals(treat.getName()) ); 10:System.out.println(“Are they the same?” + (one.getName() == three.getName()) ); }

Code Execution (Q4 from 2008 fall final, modified) Execute the call: Store.session(); public class Store { public static void session() { 1: Item one = new Item(“ipod”, 20); 2: Item two = new Item(“wii”, 32); 3: Item treat = two; 4: Item three = one; 5: three.add(4); 6: System.out.println(one); 7: System.out.println(“Cost of Item: “+ Item.getTotalCost()); 8:System.out.println(“Are they the same?” + (one.getName() == treat.getName()) ); 9:System.out.println(“Are they the same?” + one.getName().equals(treat.getName()) ); 10:System.out.println(“Are they the same?” + (one.getName() == three.getName()) ); } answers : 6 : “ipod:24” 7 : “Cost of Item: 56” 8 : “Are they the same? false” 9 : “Are they the same? false” 10 : “Are they the same? true”

The frame (the box) for a method call Remember: Every method is in a folder (object) or in a file-drawer. method name: instruction counter scope box local variables (don’t deal with these now) parameters Draw the parameters as variables. Put example here.

The frame (the box) for a method call Remember: Every method is in a folder (object) or in a file-drawer. method name: instruction counter scope box local variables (don’t deal with these now) parameters number of the statement of method body to execute NEXT. Helps you keep track of what statement to execute next. Start off with 1. Put example here.

The frame (the box) for a method call Remember: Every method is in a folder (object) or in a file-drawer. method name: instruction counter scope box local variables (don’t deal with these now) parameters scope box contains the name of entity that contains the method —a file drawer or object. Put example here. If this is a static method, this method in the file-drawer, so the scope box contains the class name, if it is not static, it is in the folder(object), scope box contain the name of the object.

Score To execute the call x.setScore(100); 1. Draw a frame for the call. 2. Assign arguments to the parameters (in the frame). setScore: 1 value a0 3. Execute the method body. (Look for variables in the frame; if not there, look in the place given by the scope box.) 100 4. Erase the frame for the call. a0 Score score setScore(int value) { score= value;} getScore() {…} 10 100 x a0 Score Push “7”, the arg, on the stack. Now, draw the frame for the call. T.setP refers to method setP in the object that t is holding the name of. lhc setP: 1 rhc: because setP is not statis, it’s the name of the object (folder) that contains this non-static metho, in this case a0 (you can tell by looking at t). [Move the t” box]

Scope of local variable: the sequence of statements following it within the containing “block”. /** = the max of x and y */ public static int max(int x, int y) { // Swap x and y to put the max in x if (x < y) { int temp; temp= x; x= y; y= temp; } return x; scope of temp You can’t use temp down here This is an error.

Scope of local variable: the sequence of statements following it. /** s contains a name in the form exemplified by “David Gries”. Return the corresponding String “Gries, David”. There may be 1 or more blanks between the names. */ public static String switchFormat(String s) { // Store the first name in variable f and remove f from s int k; // Index of the first blank in s k= s.indexOf(' '); String f; // The first name in s. f= s.substring(0, k); s= s.substring(k); // Remove the blanks from s s= s.trim(); return s + ", " + f; } declaration assignment scope of k scope of f

Call Stack Call Stack is the stack of frames for uncompleted method calls, a frame for a method call lasts as long as the method call is being executed. When the call is finished, the frame is erased. This fact explains why local variables do not retain their values from one call of a method to the next call of the same method: All the information about the first call is in a frame, and the frame is erased when the call is completed.

Exercise 1

Draw the frame for the call and execute the method call Step Into vs Step Over Step Into: Draw the frame for the call and execute the method call Step Over: Assume the function is doing exactly what it should do based on the specifications of the function.