DOC PREVIEW
CORNELL CS 211 - Study Guide

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Review of classes and subclassesPackagesSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 171Review of classes and subclassesM fast through this material, since by now all have seen it in CS100 or the Java bootcampFirst packagesThen classesThen subclassesGoal: to give you complete understanding about how objects, methods, method calls are implement.Raise discussion of classes and subclasses above the level of the computer and talk instead about classes as drawers in a filing cabinet, objects as manila folders that go in file drawers, and references or pointers as labels or names placed on manila folders.This makes concepts easier to grasp, to talk about; loses nothing.2PackagesRead Weiss, section 3.6, “packages”, for referencePackage: collection of .java source files (and other packages and interfaces) that are grouped together in the same directory (folder).Package java.lang contains classes that you can automatically use: •wrapper classes: Integer, Boolean, etc.•Math: contains functions you can use, like abs and sqrt•String and StringBuffer•System•Throwable, Error, Exception (discuss later)3Package java.io contains classes for doing input/ouput. We’ll discuss this a bit in recitations.To use these, you should “import” them.Put the commandimport java.io.*;at the top of a file that contains a class that will use a class in this package. import java.io.*;public class Ex {public void m(…) { ...}}4Other useful packagesYou have to import these packages. We’ll use many of these later in the course.•java.applet: used for applets•java.awt: used in constructing GUIs•javax.swing: the more modern classes for constructing GUIs•java.util: classes for dates, calendars, locales, random numbers. Class Vector. Classes for sets, lists, maps5You can make your own packagesDefault package, say classes C1, C2, C3Package mine, say classes K1, K2File structure: main directory: C1.java C2.java C3.java mypack (a directory)K1.javaK2.javapackage mypack;public class K2 {}package mypack;public class K1 {}file K1.java file K2.java6Visibility levelspublic int w;private int x;protected int y;/* package */ int z; private: visible only in the class./* package */: visible only in the package in which it appears.protected: visible in the package in which it appears and in subclasses.public: visible anywhere.Note: You cannot use the keyword package as a prefix on a declaration. That is why we have placed comments around it. Visibility “package” is the default, when no access modifier is given.Note: You can place these modifiers on fields, methods, and classes7Review of classesWhy review? To make sure that you and I are using the same terminology and the same concepts of class and related issues. Use this example:public class C { public static final int ten= 10;private int y;// Constructor: instance with y = yppublic C (int yp) { y= yp; }// specification of method ppublic static void p(int x) {// body of method goes here}// specification of function fpublic int f(int y) {// body of function f goes here} }8Notes on the class on previous slide1. A class is a drawer of a file cabinet. It contains (at least) the static entities defined in the class.In this case, ten and p.2. A class is a template for objectsof the class. Every object of C isa manila folder of the form given below. The manila folder contains all nonstatic entities declared in class C. The manila foldergoes in C’s file drawer.Class Ca0Cyf Cname of objectname of classnonstatic field,or instancevariablenonstatic method, or instance methodconstructor9Draw objects as manila foldersYou MUST draw each object as shown belowAs a manila folder with:• class name in box in upper right corner• name of object on the tab of manila folder • each nonstatic field as a variable in the folder• the name of each nonstatic method in the foldera0Cyf C10The frame for a method call All method calls occur within methods, which are defined in classes. We now explain the use of the “scope box” in the frame: Scope box used during execution of method body to find variables and methods that are referencedScope box contains, for a:• nonstatic method: the name of the instance in which the method appears• static method: the name of the class in which it is defined• constructor: name of newly created objectparameters and local variables appear here method name scope boxreturn address11parameters and local variables appear here method name scope boxreturn addressExecution of procedure call1. Evaluate args and push them onto call stack.2. Push locations for the rest of the frame for the call onto the stack.3. Put in frame: name of method, local vars, return address, and scope box --filled in correctly (see slide 10).4. Execute method body --look in frame at top of stack for variable or method. If not there, use scope box to determine where to look next; if in an object, search from bottom to top.5. Pop frame from call stack; continue execut-ing at the return address in popped frame.12Sample execution of proc call --do in classpublic class M {public static void main (String[] pars) {b= new C(5);c= new C(4);d= c.hits();}}// An instance maintains the number of walks// and hits of a baseball playerpublic class C {private int y; // number of walksprivate int x= 0; // number of hits// Constructor: instance with yp walks, 2 hitspublic C (int yp) { y= yp; x= 2; }// = number of hitspublic int hits() { return x; } // = number of hits + number of errorspublic int hitErr() { return x;}}13Memorize for quiz on Tuesday, 18 Sept.It’s important for understanding how method calls and objects work that you memorize:(1) format of a frame(2) format of an object --manila folder(3) evaluation of a new expressionnew C(…)(a) create a new instance of class C(b) execute method call C(…), putting inthe scope box of the frame the name ofthe newly created object(4) Steps in executing a method call (slide 11).14Drawing an instance of a subclass// Extends class C on slide 12 public class Sub extends C {private int z; // number of errors// Constructor: instance with yp walks, 2 hits,// and zp errorspublic Sub(int yp, int zp){ super(yp); z= zp; }// = number of hits + number of errorspublic int hitErr() { return hits() + z;}}a5C y x C hits hitErr SubzSub


View Full Document

CORNELL CS 211 - Study Guide

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?