COMP 14: Writing Classes June 6, 2000 Nick Vallidis.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Fields, Constructors, Methods
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
COMP 14: decomposition, overloading, relationships June 7, 2000 Nick Vallidis.
Objects and Classes Objects and Classes objects defining classes method declaration object references and aliases instance variables encapsulation and.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
1 Lecture 4 Objects and Classes Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Writing Classes (Chapter 4)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
1 Objects and Classes Introduction zNow that some low-level programming concepts have been established, we can examine objects in more detail zLecture.
Lecture 4 b Writing Classes b class declarations b method declarations b constructors b instance variables b encapsulation b method overloading b program.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 4: Writing Classes. 2 b We've been using predefined classes. Now we will learn to write our own classes to define new objects b Chapter 4 focuses.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Object-Oriented Design Bina Ramamurthy SUNY at Buffalo.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Anatomy of a Class & Method
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Encapsulation & Visibility Modifiers
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Writing Classes.
Chapter 4 Writing Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

COMP 14: Writing Classes June 6, 2000 Nick Vallidis

Announcements zAssignment P3 due today! zMidterm exam is on Friday zAssignment P4 goes out today (due next Tuesday, June 13)

Review zWhat's the MAJOR difference between a class and an object? zWhat two types of things go into a class? zWhat does the scope of data mean?

Today zEncapsulation zMethod Declaration

Encapsulation zYou can take two views of an object: yinternal - the structure of its data, the algorithms used by its methods yexternal - the interaction of the object with other objects in the program zFrom the external view, an object is an encapsulated entity, providing services zThese services define the interface to the object

Encapsulation zAn object should be self-governing zAny changes to the object's state (its variables) should be accomplished by that object's methods

Encapsulation zWe should make it difficult, if not impossible, for one object to "reach in" and alter another object's state zThe user, or client, of an object can request its services, but it should not have to be aware of how those services are accomplished (abstraction - hide details)

Encapsulation zAn encapsulated object can be thought of as a black box or an abstraction (p. 54) zIts inner workings are hidden to the client, which only invokes the interface methods Client Methods Data

Visibility modifiers zIn Java, we accomplish encapsulation through the use of visibility modifiers zA modifier is a Java reserved word that specifies particular characteristics of a method or data value  We've used the modifier final to define a constant

Visibility modifiers  Java has three visibility modifiers: public, private, and protected  public - accessed from anywhere  private - only from within the class zdefault (no modifier) - only from within the package  protected - we won't talk about this

Visibility modifiers  No object's data should be declared with public visibility  Methods that provide the object's services are declared with public visibility zPublic methods are also called service methods

Visibility modifiers zA method created simply to assist a service method is called a support method zSince a support method is not intended to be called by a client, it should not be declared with public visibility

Method declarations zA method declaration begins with a method header methodname returntype parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument int add (int num1, int num2)

{ int sum = num1 + num2; return sum; } Method declarations zThe method header is followed by the method body The return expression must be consistent with the return type sum is local data It is created each time the method is called, and is destroyed when it finishes executing

the return statement zThe return type of a method indicates the type of value that the method sends back to the calling location  A method that does not return a value has a void return type zThe return statement specifies the value that will be returned zIts expression must conform to the return type

Parameters zEach time a method is called, the actual arguments in the invocation are copied into the formal arguments answer = obj.add (25, count); int add (int num1, int num2) { int sum = num1 + num2; return sum; }

Homework zread zassignment P4 is due next Tuesday!