COP 3330 : EXAM 1 REVIEW Page 1 © Dr. Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2011 EXAM #1 – In Class Practice Department of Electrical.

Slides:



Advertisements
Similar presentations
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Advertisements

Lecture 2: Object Oriented Programming I
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Huron High School AP Computer Science Instructor: Kevin Behemer S. Teacher: Guillermo Moreno.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Classes, Objects, and Methods
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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.
Building java programs, chapter 3 Parameters, Methods and Objects.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
1 Functions/Methods. 2 Function / Method zDefinition Õcollection of statements that may be invoked by name (from another function or method) zFunctions/methods.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Creating and Using Class Methods. Definition Class Object.
Department of Computer Engineering Methods Computer Programming for International Engineers.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
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];
Classes - Intermediate
Methods.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
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?
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Department of Computer Science
Chapter 5 Functions DDC 2133 Programming II.
Using local variable without initialization is an error.
Methods and Parameters
ATS Application Programming: Java Programming
Lecture 11 C Parameters Richard Gesick.
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Group Status Project Status.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Chapter 6 Methods.
Take out a piece of paper and PEN.
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
Lecture 11 Parameters CSE /26/2018.
Methods (a.k.a functions)
CIS 110: Introduction to Computer Programming
ITE “A” GROUP 2 ENCAPSULATION.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

COP 3330 : EXAM 1 REVIEW Page 1 © Dr. Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2011 EXAM #1 – In Class Practice Department of Electrical Engineering and Computer Science Computer Science Division University of Central Florida Instructor : Dr. Mark Llewellyn HEC 236,

COP 3330 : EXAM 1 REVIEW Page 2 © Dr. Mark Llewellyn public class Example1 { public static void main(String args[]){ C1 o1, o2; C1.y = 10; C1.x = 10; C1.printY(); C1.setX(10); o1 = new C1(); o2 = new C1(); o1.x = 2; o2.x = 3; o1.y = 4; C1.y = 6; o1.setX(7); o2.setX(8); C1.printY(); o2.printY(); } class C1 { public int x; public static int y = 5; public C1() { x = 1;} public void setX(int val) {x = val;} public static void printY() {System.out.println("y: " + y);} } Static vs. Class Variables and Methods – In-Class Practice Problem First – identify which statements are legal and which are illegal. Second – fix the illegal statements. Third – trace the program execution and show the output.

COP 3330 : EXAM 1 REVIEW Page 3 © Dr. Mark Llewellyn public class Example1 { public static void main(String args[]){ C1 o1, o2; C1.y = 10; C1.x = 10; //cannot make a static reference to a non-static field C1.printY(); //C1.setX(10); //cannot make a static reference to a non-static method o1 = new C1(); o2 = new C1(); o1.x = 2; o2.x = 3; o1.y = 4; //static variable – should be accessed thru the class C1.y = 6; o1.setX(7); o2.setX(8); C1.printY(); o2.printY(); // static method should be accessed thru the class } class C1 { public int x; public static int y = 5; public C1() { x = 1;} public void setX(int val) {x = val;} public static void printY() {System.out.println("y: " + y);} }

COP 3330 : EXAM 1 REVIEW Page 4 © Dr. Mark Llewellyn public class Example1 { public static void main(String args[]){ C1 o1, o2; o1 = new C1(); o2 = new C1(); C1.y = 10; o1.x = 10; //make reference to instance variable thru an instance C1.printY(); o2.setX(10); //invoke instance method thru an instance o1.x = 2; o2.x = 3; C1.y = 4; //static variable referenced/accessed thru the class C1.y = 6; o1.setX(7); o2.setX(8); C1.printY(); C1.printY(); // static method invoked thru the class } class C1 { public int x; public static int y = 5; public C1() { x = 1;} public void setX(int val) {x = val;} public static void printY() {System.out.println("y: " + y);} } The Fix

COP 3330 : EXAM 1 REVIEW Page 5 © Dr. Mark Llewellyn Execution

COP 3330 : EXAM 1 REVIEW Page 6 © Dr. Mark Llewellyn Parameter Passing Example

COP 3330 : EXAM 1 REVIEW Page 7 © Dr. Mark Llewellyn Step 1 - Execution begins in Main in1n3n2 in main before invocation of chValues 1 ival 375 Parameter Passing Example

COP 3330 : EXAM 1 REVIEW Page 8 © Dr. Mark Llewellyn in1n3n2 in main 1 ival 375 xzw y 13 Step 2 – Invoke Method chvalues ( i,n1.ival,n2,n3) in chValues actual parameters formal parameters Red dotted lines indicate the passing of parameter values. Notice that formal parameters x and y are primitive types and thus copies of the actual parameters are passed (pass by value) while w and z are objects and thus references to the objects are passed (pass by reference).

COP 3330 : EXAM 1 REVIEW Page 9 © Dr. Mark Llewellyn in1n3n2 in main 1 ival 375 xzw y 13 Step 3 – Method chValues Executes in chValues actual parameters formal parameters = 0= 4 ival 8 = 9

COP 3330 : EXAM 1 REVIEW Page 10 © Dr. Mark Llewellyn in1n3n2 in main 1 ival 375 xzw y 13 Step 4 – Return to Main Method – Print Results in chValues actual parameters formal parameters = 0= 4 ival 8 = 9 Output: 1 – 3 – 5 – 9

COP 3330 : EXAM 1 REVIEW Page 11 © Dr. Mark Llewellyn