CS106A, Stanford Handout #18. Writing a Class
|
|
- Kathleen Pitts
- 5 years ago
- Views:
Transcription
1 CS106A, Stanford Handout #18 Fall, Nick Parlante Writing a Class Up to now we have used classes like Binky and DWindow defined by others, but now it is time to see how to write our own classes. Object Oriented Programming (OOP) is great at breaking a large problem into smaller, more manageable pieces. This strength has made OOP the dominant style for creating new programs. To write an OOP program, we must formulate the program as being made up of many "objects" each of which is controlled by a "class". The social contract of objects Each object "encapsulates" its own part of the data. For example, a person object might contain "name" and " address" data. The "Person" class contains the code and definitions used by each person object. Each object is responsible for operations on its data. For example, the Person class might implement a send to() method that sends an to its individual. Each object calls methods on other objects, so they can operate on their bits of data. In particular, an object might contain within it a pointer ("reference") to another object. This allows the first object to call methods on the second object when necessary. The data of the whole program is divided up into objects. Each object owns its little part of the data and does operations on it. Objects call methods on each other to prompt them to do operations. The main() starts the whole thing up creating the initial objects and calling the first methods to start things off. Stoplight We will start with a simple Stoplight example. A Stoplight object controls three DOvals on screen, and operates them so they look like your typical red-yellow-green stoplight. Every object contains some data that it manages. In this case, the Stoplight object will own and control three DOval objects. The Stoplight class implements methods bered(), beyellow() and begreen() that prompt the receiver stoplight object to change its appearance.
2 2 A Stoplight as it appears on screen. Client Side As an introduction to the Stoplight class we can look at some typical client code that uses a Stoplight; then we can look at how the Stoplight class itself works. The client code below creates a Stoplight object and then loops around calling the begreen(), beyellow(), and bered() methods on it. This code has the hallmarks of OOP client code: Creates an object with "new" and the constructor for the desired class, e.g. new Stoplight() Stores a pointer to the new object in a variable e.g. Stoplight light = new Stoplight(); Calls methods on the object using the dot (.) syntax e.g. light.begreen(); light.beyellow(); // StoplightClient.java * Simple client code that drives a Stoplight object. * Just for fun, we give lots of red and just a little green. * Runs an infinite loop that cycles the light. import stanford.cslib.*;
3 3 public class StoplightClient { public static void main(string[] args) { Stoplight light = new Stoplight(); // while-true -- loops and never exits while (true) { // 3 seconds of red light.bered(); DWindow.waitSeconds(3.0); // 0.5 seconds of green light.begreen(); DWindow.waitSeconds(0.5); // 1 second of yellow light.beyellow(); DWindow.waitSeconds(1.0); Writing the Stoplight Class The features of the Stoplight object above are defined by the code in the Stoplight class. How is a class written? Before looking at the syntax, we can think about the things a class needs to define for its objects. A class defines three things for its objects Storage what information should be stored inside of each object. These are called "instance variables". Constructor code how should new objects of this class be set up when they are first created? The "constructor" is code in the class that sets up new objects of the class when they are first created. Method code what operations can the objects peform? "Methods" in the class contain the code that the objects can perform. Each Stoplight object contains, or "owns", three DOval objects that will be installed in a DWindow. The DOvals are themselves objects, but that's fine. We are not writing the DOval class; it is already written. We are just creating and keeping pointers to three DOvals, so we can order them around. Our Stoplight class is a client of the existing DOval class our class uses DOval objects. The variables inside of an object are called "instance variables" or "ivars". Our Stoplight object will contain three instance variables, one for each of three DOvals. The instance variables are named myred, myyellow, and mygreen (will follow the convention that instance variables are named starting with "my"). In the client main() shown above, the code creates a single Stoplight object and stores a pointer to it in a local variable called "light". "Local" variables are declared inside of a method and used for temporary storage. This is the only sort of variable we have used up
4 4 until now. Local variables use an area of memory called the "stack", which we will draw off to the left. Objects are given memory by the "new" operator in an area of memory called the "heap", which we will draw on the right. For the above client code, the picture of memory will look something like the following. We see one Stoplight object, and the Stoplight class that defines how that object behaves. Stack Heap light myred myyellow mygreen Stoplight object // Code that defines how // Stoplight objects work. public class Stoplight { Stoplight class As we build up a program with objects with pointers to each other and whatnot, it's very common to make a diagram like this to help us think about the program. Class Definition Syntax Now we are ready for the details of the Java syntax to define a class: defining the storage, constructors, and methods of the class.
5 5 Class Syntax Usually the class begins with a comment that describes the basic role of the class. Then there are "import" statements that identify non-default external classes that the class uses. For CS106A, there are just a couple imports we will commonly use // import standard Java graphics classes import java.awt.*; // import Stanford classes import stanford.cslib.*; 1. Instance Variables ivars The central feature of an object is what data does it contain? A class declares "instance variables" for its objects, also known as "ivars" or "fields" or "data members". Each instance variable is space that will be available inside every object of the class. So if an MP3 class declares instance variables mytitle and mygenre, then every MP3 object will have instance variables inside it called mytitle and mygenre, ready to store the information for that object. For the Stoplight example, we want each Stoplight to store three DOval objects. The line "public class Stoplight {" begins the class declaration, and the first thing inside it are the declarations of the three instance variables myred, myyellow, and mygreen public class Stoplight { private DOval myred; private DOval myyellow; private DOval mygreen; // our three DOval ivars. Each instance variable is declared with the word "private", followed by the type of the variable, followed by its name. We will usually put the instance variable declarations at the top of a class, since they are so central to how the class works. However, in reality, the elements in a class (variables, code, etc.) can be in any order. If the role of an ivar is not clear from its name, we would add a comment describing what that ivar stores. "my" Convention Every Stoplight object contains its own "myred", "myyellow" and "mygreen" instance variables. There are several conventions in use for how to name the ivars Java does not have any formal requirement about how they are named. We will use the convention of beginning their names with "my" to help emphasize that they are inside of an object. This will help distinguish instance variables from the other sorts of variable, such as local variables. There are several different naming conventions in use in the OOP community; "my" is just the one we are using.
6 6 private vs. public Encapsulation Every element in a class can be declared "private" or "public". Suppose we have some "client" code of the class i.e. code somewhere else in the program that makes use of the class. The public/private options control which aspects of the class are available to the client code. Something that is "public" may be referenced by client code. Something that is "private" may not be used directly by client code. The compiler enforces that client code obeys public and private restrictions. Typically, instance variables are declared private. This is part of "encapsulation" OOP design, where each object is protective of its data and internal workings, not allowing client code to use those parts directly. 2. Constructor The "constructor" is one-time setup code that runs to initialize new objects of the class at the time that "new" is called. The constructor is the place to allocate and set up the object's data. To set up the new object, the constructor puts data in each of the object's ivars. So a typical constructor has an assignment ("=") for each ivar to give it an initial value. Syntactically, the constructor has the same name as the class and is typically declared public. Constructors and methods (below) can take arguments, but we will save that for a later. For our Stoplight example, the constructor is named "Stoplight()". It creates a DWindow object, creates and installs three DOvals, and stores the pointers to the DOval objects in the three ivars. We could keep a pointer to the DWindow as well, however we never need to call a method on it, so we do not bother. We need to keep pointers to objects, only if we need to contact or use them later. * Constructor -- code that runs when the client creates * a new Stoplight object. * Sets up the ivars. * "public" = accessible to clients. public Stoplight() { DWindow window = new DWindow("Stoplight", 70, 190); // Create three DOvals // each is 50 pixels around, separated by 10 pixels of space myred = new DOval(10, 10, 50, 50); window.add(myred); myyellow = new DOval(10, 70, 50, 50); window.add(myyellow); mygreen = new DOval(10, 130, 50, 50); window.add(mygreen);
7 7 3. Methods "Methods" in a class contain the code that objects of the class can run. The "bered()" method in the Stoplight class contains the code that runs when the client calls "bered()" on a Stoplight object. The prototype of a method begins with "public" or "private" followed by the return type of the method. For now, we will use "void" as the return type which means that the method does not return a value. The name of the method is followed by its list of arguments in parenthesis. For now, the list of arguments will be empty (). * Methods -- these contain the code that runs when * the client sends a message to a Stoplight object. * Lights the green, clears the others. public void begreen() { mygreen.setcolor(color.green); myred.setcolor(color.black); myyellow.setcolor(color.black); Receiver Relative Coding Code in a method always executes in the context of a specific object. If some client calls the method "light.bered();", the bered() method code runs on the specific Stoplight object referenced by the "light" variable. The method is said to run "on" or "against" that object which is knows as the "receiver". Method and constructor code can refer to ivars simply by name, e.g. "myred", but where does that ivar come from? If there are two Stoplight objects in memory, which myred does the code use? It uses the myred of the receiver whichever Stoplight object the method was called on. Methods and constructors always run "on" a receiver object, and all the ivar references are on that receiver. The code can refer to variables like myred or mygreen, confident that the OOP class/object machinery will pull the data from the right receiver object. This turns out to be a convenient way to write code. We write code in a method and it is always in the context of the receiver object of the method. All of its ivars myred, myyellow, mygreen fall to hand conveniently just by name. Complete Stoplight.java Code Here is the complete source code for the Stoplight class as it appears in the file Stoplight.java. Much of this code appears in the explanations above, but here it is in one piece as you will see it when writing your own classes
8 8 // Stoplight.java CS106A -- a first example of defining a class. * Each Stoplight object owns and controls three DOvals that * are installed in a DWindow. * Implements begreen(), beyellow(), bered() methods. * // import standard Java graphics classes import java.awt.*; // import Stanford classes import stanford.cslib.*; // (This marks the beginning of the class definition) public class Stoplight { * Instance variables ("ivars") * Every Stoplight object will have myred, myyellow, and mygreen * ivars in it that store pointers to the three DOvals. * "private" = not accessible to clients. private DOval myred; private DOval myyellow; private DOval mygreen; * Constructor -- code that runs when the client creates * a new Stoplight object. * Sets up the ivars. * "public" = accessible to clients. public Stoplight() { // Create a DWindow for the DOvals // We do not need to call methods on the window later, // so we do not need to keep a pointer to it in an ivar. // Store the pointer in a local variable, just for use // while the constructor runs. DWindow window = new DWindow("Stoplight", 70, 190); // Create three DOvals // each is 50 pixels around, separated by 10 pixels of space myred = new DOval(10, 10, 50, 50); window.add(myred); myyellow = new DOval(10, 70, 50, 50); window.add(myyellow); mygreen = new DOval(10, 130, 50, 50); window.add(mygreen);
9 9 * begreen(), bered(), beyellow() methods * Lights the green, clears the others. public void begreen() { mygreen.setcolor(color.green); myred.setcolor(color.black); myyellow.setcolor(color.black); * Lights the yellow, clears the others. public void beyellow() { myyellow.setcolor(color.yellow); myred.setcolor(color.black); mygreen.setcolor(color.black); * Lights the red, clears the others. public void bered() { myred.setcolor(color.red); mygreen.setcolor(color.black); myyellow.setcolor(color.black); // End of the Stoplight class definition Stoplight Intersection Client Example In our first Stoplight client, we just had one Stoplight object and the one Stoplight class, so the difference between an object and a class was not emphasized. Here is a second client that creates two Stoplight objects, one Stoplight object for a north-south road and a second Stoplight object for an intersecting east-west road. This example has two Stoplight objects, each with its own DOvals. However, there is still just one Stoplight class, containing the code that both the Stoplight objects use. The local variables in the client main(), "north" and "east" are allocated in the stack. All the objects are in the heap. // StoplightClient2.java * Runs two Stoplights -- one north/south, one east/west import stanford.cslib.*;
10 10 public class StoplightClient2 { public static void main(string[] args) { Stoplight north = new Stoplight(); // for the north/south direction Stoplight east = new Stoplight(); // for the east/west direction while (true) { // north goes north.begreen(); east.bered(); DWindow.waitSeconds(2.0); north.beyellow(); DWindow.waitSeconds(1.0); // east goes north.bered(); east.begreen(); DWindow.waitSeconds(2.0); east.beyellow(); DWindow.waitSeconds(1.0); Stack Heap north east myred myyellow mygreen Stoplight object myred myyellow mygreen Stoplight object // Code that defines how // Stoplight objects work. public class Stoplight { Stoplight class
11 11
OOP Design Conclusions and Variations
CS108, Stanford Handout #20 Fall, 2008-09 Osvaldo Jiménez OOP Design Conclusions and Variations Thanks to Nick Parlante for much of this handout Part 1 -- Mainstream OOP Design First, we have the standard,
More informationCS106A, Stanford Handout #25. Methods 2
CS106A, Stanford Handout #25 Fall, 2004-05 Nick Parlante Methods 2 System.out.println() Console System.out is a standard system object. It implements a println() method that takes a String argument and
More informationChapter 10 Introduction to Classes
C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this
More informationCSC231 C Tutorial Fall 2018 Introduction to C
mith College CSC231 C Tutorial Fall 2018 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 4 Installments! Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard
More informationCS 231 Data Structures and Algorithms, Fall 2016
CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard
More informationCSE 303: Concepts and Tools for Software Development
CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects
More informationVariables and Java vs C++
Variables and Java vs C++ 1 What can be improved? (variables) public void godirection(string directionname) { boolean wenttoroom = false; for (Direction direction : currentroom.getdirections()) { if (direction.getdirectionname().equalsignorecase(directionname))
More informationCS108, Stanford Handout #3. HW1 CodeCamp
CS108, Stanford Handout #3 Fall, 2008-09 Osvaldo Jiménez HW1 CodeCamp Thanks to Nick Parlante for much of this handout For this first homework, you will run through a series of small coding problems to
More informationAP Computer Science Chapter 10 Implementing and Using Classes Study Guide
AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within
More informationName: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015
CMSC 202 Section 06 Fall 2015 Computer Science II Midterm Exam I Name: Username: Score Max Section: (check one) 07 - Sushant Athley, Tuesday 11:30am 08 - Aishwarya Bhide, Thursday 11:30am 09 - Phanindra
More informationJava Memory Management
Java Memory Management Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each code (classes and interfaces) objects running methods 1-2 Memory Areas
More informationChapter 6 Introduction to Defining Classes
Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of
More informationCS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays
CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements
More informationIntroduction to Programming (Java) 4/12
Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction
More informationCA31-1K DIS. Pointers. TA: You Lu
CA31-1K DIS Pointers TA: You Lu Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a
More informationIntroduction to Java. Handout-1d. cs402 - Spring
Introduction to Java Handout-1d cs402 - Spring 2003 1 Methods (i) Method is the OOP name for function Must be declared always within a class optaccessqualifier returntype methodname ( optargumentlist )
More informationLecture 18 Tao Wang 1
Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output
More informationCS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class
CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount
More informationCS108, Stanford Handout #8. Java Generics
CS108, Stanford Handout #8 Fall, 2007-08 Nick Parlante Java Generics Java generics (added in version 5) are a mixed bag. Some uses of generics are simple to understand and make the code cleaner. They are
More informationCMSC131. Library Classes
CMSC131 Designing Classes Library Classes Due to Java being 100% object-oriented, all code must live inside a class but there is some functionality/information that might be best kept in a more central
More informationCS 32. Lecture 2: objects good?
CS 32 Lecture 2: objects good? Double Vision This course has two main tracks Unix/shell stuff Object-Oriented Programming Basic C++ familiarity Off by one! Another Troika This class has three main texts
More informationArray Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)
Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants
More informationAbout this exam review
Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review
More informationObject Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.
CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java
More informationCS106A, Stanford Handout #30. Coding Style
CS106A, Stanford Handout #30 Fall, 2004-05 Nick Parlante Coding Style When writing paper, you can have well-crafted, correctly spelled sentences and create "A" work. Or you can hack out the text in a hurry.
More information+ Abstract Data Types
Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract
More informationXII CS(EM) Minimum Question List N.KANNAN M.Sc., B.Ed COMPUTER SCIENCE IMPORTANT QUESTION (TWO MARKS) CHAPTER 1 TO 5 ( STAR OFFICE WRITER)
COMPUTER SCIENCE IMPORTANT QUESTION (TWO MARKS) CHAPTER 1 TO 5 ( STAR OFFICE WRITER) 1. Selecting text with keyboard 2. Differ copying and moving 3. Text Editing 4. Creating a bulleted list 5. Creating
More informationCS112 Lecture: Defining Instantiable Classes
CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:
More informationPointers II. Class 31
Pointers II Class 31 Compile Time all of the variables we have seen so far have been declared at compile time they are written into the program code you can see by looking at the program how many variables
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationJAVA: A Primer. By: Amrita Rajagopal
JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently
More informationEMBEDDED SYSTEMS PROGRAMMING OO Basics
EMBEDDED SYSTEMS PROGRAMMING 2014-15 OO Basics CLASS, METHOD, OBJECT... Class: abstract description of a concept Object: concrete realization of a concept. An object is an instance of a class Members Method:
More informationLecture 02, Fall 2018 Friday September 7
Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7 Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python
More informationChapter 4. Defining Classes I
Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes
More informationCS Programming Exercise:
CS Programming Exercise: An Introduction to Java and the ObjectDraw Library Objective: To demonstrate the use of objectdraw graphics primitives and Java programming tools This lab will introduce you to
More informationCS 11 C track: lecture 6
CS 11 C track: lecture 6 Last week: pointer arithmetic This week: The gdb program struct typedef linked lists gdb for debugging (1) gdb: the Gnu DeBugger http://courses.cms.caltech.edu/cs11/material /c/mike/misc/gdb.html
More informationChapter 1 Getting Started
Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different
More informationAssertions, pre/postconditions
Programming as a contract Assertions, pre/postconditions Assertions: Section 4.2 in Savitch (p. 239) Specifying what each method does q Specify it in a comment before method's header Precondition q What
More informationLesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014
Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class
More informationCSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010
CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)
More information(Refer Slide Time: 1:27)
Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
More informationclass objects instances Fields Constructors Methods static
Class Structure Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance variables)that hold the data for each object Constructors that
More informationCS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor
CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static
More informationPass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).
Our programs are littered with function calls like f (x, 5). This is a way of passing information from the call site (where the code f(x,5) appears) to the function itself. The parameter passing mode tells
More informationQUIZ. What is wrong with this code that uses default arguments?
QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code
More informationDeclaring and ini,alizing 2D arrays
Declaring and ini,alizing 2D arrays 4 2D Arrays (Savitch, Chapter 7.5) TOPICS Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe Game // se2ng up a 2D array final int M=3, N=4;
More informationCSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many
More informationNotes from the Boards Set BN19 Page
1 The Class, String There are five programs in the class code folder Set17. The first one, String1 is discussed below. The folder StringInput shows simple string input from the keyboard. Processing is
More information1.00 Lecture 8. Using An Existing Class, cont.
.00 Lecture 8 Classes, continued Reading for next time: Big Java: sections 7.9 Using An Existing Class, cont. From last time: is a Java class used by the BusTransfer class BusTransfer uses objects: First
More informationA PROGRAM IS A SEQUENCE of instructions that a computer can execute to
A PROGRAM IS A SEQUENCE of instructions that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form
More informationObject Reference and Memory Allocation. Questions:
Object Reference and Memory Allocation Questions: 1 1. What is the difference between the following declarations? const T* p; T* const p = new T(..constructor args..); 2 2. Is the following C++ syntax
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationDocumentation Nick Parlante, 1996.Free for non-commerical use.
Documentation Nick Parlante, 1996.Free for non-commerical use. A program expresses an algorithm to the computer. A program is clear or "readable" if it also does a good job of communicating the algorithm
More informationAgenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda
More informationObjects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay
Computer Science and Engineering Indian Institue of Technology Bombay November 27, 2004 What is Object Oriented Programming? Identifying objects and assigning responsibilities to these objects. Objects
More informationLargest Online Community of VU Students
WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions
More informationQuestions: ECE551 PRACTICE Final
ECE551 PRACTICE Final This is a full length practice midterm exam. If you want to take it at exam pace, give yourself 175 minutes to take the entire test. Just like the real exam, each question has a point
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 12 October 10, 2016 CPSC 427, Lecture 12 1/27 Uses of Pointers Custody of Objects Move Semantics CPSC 427, Lecture 12 2/27 Uses of Pointers
More informationObject Oriented Modeling
Object Oriented Modeling Object oriented modeling is a method that models the characteristics of real or abstract objects from application domain using classes and objects. Objects Software objects are
More informationPrinciple of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore
Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments
More informationCS24 Week 3 Lecture 1
CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation
More informationMemory and Pointers written by Cathy Saxton
Memory and Pointers written by Cathy Saxton Basic Memory Layout When a program is running, there are three main chunks of memory that it is using: A program code area where the program itself is loaded.
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic
More informationCS 11 java track: lecture 3
CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces
More informationCode No: R Set No. 1
Code No: R05010106 Set No. 1 1. (a) Draw a Flowchart for the following The average score for 3 tests has to be greater than 80 for a candidate to qualify for the interview. Representing the conditional
More informationIntroduction to Programming in C Department of Computer Science and Engineering
Introduction to Programming in C Department of Computer Science and Engineering Once we know structures and pointers to structures, we can introduce some very important data structure called link list.
More informationObjects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes
Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on
More informationCERTIFICATE IN WEB PROGRAMMING
COURSE DURATION: 6 MONTHS CONTENTS : CERTIFICATE IN WEB PROGRAMMING 1. PROGRAMMING IN C and C++ Language 2. HTML/CSS and JavaScript 3. PHP and MySQL 4. Project on Development of Web Application 1. PROGRAMMING
More informationCS121/IS223. Object Reference Variables. Dr Olly Gotel
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223
More informationIntermediate Code Generation
Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target
More informationPrimitive vs Reference
Primitive vs Reference Primitive types store values Reference types store addresses This is the fundamental difference between the 2 Why is that important? Because a reference type stores an address, you
More informationOperating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst
Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Department of Computer Science Why C? Low-level Direct access to memory WYSIWYG (more or less) Effectively
More information(Refer Slide Time: 02.06)
Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 27 Depth First Search (DFS) Today we are going to be talking
More informationCS193j, Stanford Handout #6. OOP Design
CS193j, Stanford Handout #6 Winter, 2002-03 Nick Parlante OOP Design OOP Design #1 -- Encapsulation The most basic idea in OOP is that each object encapsulates some data and code. The object takes requests
More informationUnit 7. Functions. Need of User Defined Functions
Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have
More informationCLASSES AND OBJECTS IN JAVA
Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationLearning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods
Learning objec-ves 2D Arrays (Savitch, Chapter 7.5) TOPICS Using 2D arrays Decomposi-on of a solu-on into objects and methods Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe
More information6.001 Notes: Section 15.1
6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define
More informationMath Modeling in Java: An S-I Compartment Model
1 Math Modeling in Java: An S-I Compartment Model Basic Concepts What is a compartment model? A compartment model is one in which a population is modeled by treating its members as if they are separated
More informationCS61B Lecture #7. Announcements:
Announcements: CS61B Lecture #7 New discussion section: Tuesday 2 3PM in 310 Soda. New lab section: Thursday 2 4PM in 273 Soda. Programming Contest coming up: 5 October (new date). Watch for details. Last
More informationProgramming Languages (Outsource: 2-2)
Programming Languages (Outsource: 2-2) A computer program is a set of instructions that you write to tell a computer what to do. There are many languages used to write computer programs, each language
More informationCE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings
CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location
More informationObject Oriented Programming
Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented
More informationCS61B Lecture #5: Arrays and Objects
CS61B Lecture #5: Arrays and Objects For faster response, please send urgent problems (like the lab files don t compile ) as mail to cs61b, rather than using class messages. Homeworks are generally due
More informationCS193k, Stanford Handout #8. Threads 3
CS193k, Stanford Handout #8 Spring, 2000-01 Nick Parlante Threads 3 t.join() Wait for finish We block until the receiver thread exits its run(). Use this to wait for another thread to finish. The current
More informationCSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.
CSCI0330 Intro Computer Systems Doeppner Lab 02 - Tools Lab Due: Sunday, September 23, 2018 at 6:00 PM 1 Introduction 0 2 Assignment 0 3 gdb 1 3.1 Setting a Breakpoint 2 3.2 Setting a Watchpoint on Local
More informationJava Bytecode (binary file)
Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.
More informationCSE 303: Concepts and Tools for Software Development
CSE 303: Concepts and Tools for Software Development Hal Perkins Winter 2009 Lecture 7 Introduction to C: The C-Level of Abstraction CSE 303 Winter 2009, Lecture 7 1 Welcome to C Compared to Java, in rough
More informationLinked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }
Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will
More informationCONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want
More informationC Introduction. Comparison w/ Java, Memory Model, and Pointers
CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language
More informationCSE 142 Su 04 Computer Programming 1 - Java. Objects
Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request
More informationLinked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I
College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I What are they? Abstraction of a list: i.e. a sequence of nodes in which each node is linked to the node
More informationLecture 3. Lecture
True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design
More information6.001 Notes: Section 8.1
6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything
More informationMVC Table / Delegation
CS108, Stanford Handout #27 Winter, 2006-07 Nick Parlante MVC Table / Delegation Delegate MVC Table Censor Example Uses the delegate strategy to build a variant table model Uses table model listener logic
More informationVariables, Memory and Pointers
Variables, Memory and Pointers A variable is a named piece of memory The name stands in for the memory address int num; Variables, Memory and Pointers When a value is assigned to a variable, it is stored
More informationClasses and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?
Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable
More information