Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Size: px
Start display at page:

Download "Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5"

Transcription

1 Defining Classes and Methods Chapter 5 Objectives Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters in a method Use modifiers public, private Define accessor, mutator class methods Describe information hiding, encapsulation Write method pre- and postconditions Objectives Describe purpose of javadoc Draw simple UML diagrams Describe references, variables, parameters of a class type Define boolean-valued methods such as equals In applets use class Graphics, labels, init method 1

2 Class and Method Definitions: Outline Class Files and Separate Compilation Instance Variables Methods The Keyword this Local Variables Blocks Parameters of a Primitive Type Class and Method Definitions Java program consists of objects Objects of class types Objects that interact with one another Program objects can represent Objects in real world Abstractions Class and Method Definitions Figure 5.1 A class as a blueprint 2

3 Class and Method Definitions Figure 5.1 ctd. Objects that are instantiations of the class Automobile Class and Method Definitions Figure 5.2 A class outline as a UML class diagram Class Files and Separate Compilation Each Java class definition usually in a file by itself File begins with name of the class Ends with.java Class can be compiled separately Helpful to keep all class files used by a program in the same directory 3

4 Dog class and Instance Variables View sample program, listing 5.1 class Dog Note class has Three pieces of data (instance variables) Two behaviors Each instance of this type has its own copies of the data items Use of public No restrictions on how variables used Later will replace with private Using a Class and Its Methods View sample program, listing 5.2 class DogDemo Sample screen output Methods When you use a method you "invoke" or "call" it Two kinds of Java methods Return a single item Perform some other action a void method The method main is a void method Invoked by the system Not by the application program 4

5 Methods Calling a method that returns a quantity Use anywhere a value can be used Calling a void method Write the invocation followed by a semicolon Resulting statement performs the action defined by the method Defining void Methods Consider method writeoutput from Listing 5.1 Method definitions appear inside class definition Can be used only with objects of that class Defining void Methods Most method definitions we will see as public Method does not return a value Specified as a void method Heading includes parameters Body enclosed in braces { } Think of method as defining an action to be taken 5

6 Methods That Return a Value Consider method getageinhumanyears( ) Heading declares type of value to be returned Last statement executed is return Second Example Species Class Class designed to hold records of endangered species View the class listing 5.3 class SpeciesFirstTry Three instance variables, three methods Will expand this class in the rest of the chapter View demo class listing 5.4 class SpeciesFirstTryDemo The Keyword this Referring to instance variables outside the class must use Name of an object of the class Followed by a dot Name of instance variable Inside the class, Use name of variable alone The object (unnamed) is understood to be there 6

7 The Keyword this Inside the class the unnamed object can be referred to with the name this Example this.name = keyboard.nextline(); The keyword this stands for the receiving object We will seem some situations later that require the this Local Variables Variables declared inside a method are called local variables May be used only inside the method All variables declared in method main are local to main Local variables having the same name and declared in different methods are different variables Local Variables View sample file, listing 5.5A class BankAccount View sample file, listing 5.5B class LocalVariablesDemoProgram Note two different variables newamount Note different values output Sample screen output 7

8 Blocks Recall compound statements Enclosed in braces { } When you declare a variable within a compound statement The compound statement is called a block The scope of the variable is from its declaration to the end of the block Variable declared outside the block usable both outside and inside the block Parameters of Primitive Type Recall method declaration in listing 5.3 Note it only works for 10 years We can make it more versatile by giving the method a parameter to specify how many years Note sample program, listing 5.6 class SpeciesSecondTry Parameters of Primitive Type Note the declaration public int predictpopulation(int years) The formal parameter is years Calling the method int futurepopulation = speciesofthemonth.predictpopulation(10); The actual parameter is the integer 10 View sample program, listing 5.7 class SpeciesSecondClassDemo 8

9 Parameters of Primitive Type Parameter names are local to the method When method invoked Each parameter initialized to value in corresponding actual parameter Primitive actual parameter cannot be altered by invocation of the method Automatic type conversion performed byte -> short -> int -> long -> float -> double Information Hiding, Encapsulation: Outline Information Hiding Pre- and Postcondition Comments The public and private Modifiers Methods Calling Methods Encapsulation Automatic Documentation with javadoc UML Class Diagrams Information Hiding Programmer using a class method need not know details of implementation Only needs to know what the method does Information hiding: Designing a method so it can be used without knowing details Also referred to as abstraction Method design should separate what from how 9

10 Pre- and Postcondition Comments Precondition comment States conditions that must be true before method is invoked Example Pre- and Postcondition Comments Postcondition comment Tells what will be true after method executed Example The public and private Modifiers Type specified as public Any other class can directly access that object by name Classes generally specified as public Instance variables usually not public Instead specify as private View sample code, listing 5.8 class SpeciesThirdTry 10

11 Programming Example Demonstration of need for private variables View sample code, listing 5.9 Statement such as box.width = 6; is illegal since width is private Keeps remaining elements of the class consistent in this example Programming Example Another implementation of a Rectangle class View sample code, listing 5.10 class Rectangle2 Note setdimensions method This is the only way the width and height may be altered outside the class Accessor and Mutator Methods When instance variables are private must provide methods to access values stored there Typically named getsomevalue Referred to as an accessor method Must also provide methods to change the values of the private instance variable Typically named setsomevalue Referred to as a mutator method 11

12 Accessor and Mutator Methods Consider an example class with accessor and mutator methods View sample code, listing 5.11 class SpeciesFourthTry Note the mutator method setspecies Note accessor methods getname, getpopulation, getgrowthrate Accessor and Mutator Methods Using a mutator method View sample program, listing 5.12 classspeciesfourthtrydemo Sample screen output Programming Example A Purchase class View sample code, listing 5.13 class Purchase Note use of private instance variables Note also how mutator methods check for invalid values View demo program, listing 5.14 class purchasedemo 12

13 Programming Example Sample screen output Methods Calling Methods A method body may call any other method If the invoked method is within the same class Need not use prefix of receiving object View sample code, listing 5.15 class Oracle View demo program, listing 5.16 class OracleDemo Methods Calling Methods Sample screen output 13

14 Encapsulation Consider example of driving a car We see and use break pedal, accelerator pedal, steering wheel know what they do We do not see mechanical details of how they do their jobs Encapsulation divides class definition into Class interface Class implementation A class interface Encapsulation Tells what the class does Gives headings for public methods and comments about them A class implementation Contains private variables Includes definitions of public and private methods Encapsulation Figure 5.3 A well encapsulated class definition Programmer who uses the class 14

15 Encapsulation Preface class definition with comment on how to use class Declare all instance variables in the class as private. Provide public accessor methods to retrieve data Provide public methods manipulating data Such methods could include public mutator methods. Place a comment before each public method heading that fully specifies how to use method. Make any helping methods private. Write comments within class definition to describe implementation details. Automatic Documentation javadoc Generates documentation for class interface Comments in source code must be enclosed in /** */ Utility javadoc will include These comments Headings of public methods Output of javadoc is HTML format UML Class Diagrams Recall Figure 5.2 A class outline as a UML class diagram 15

16 UML Class Diagrams Note Figure 5.4 for the Purchase class Minus signs imply private access Plus signs imply public access UML Class Diagrams Contains more than interface, less than full implementation Usually written before class is defined Used by the programmer defining the class Contrast with the interface used by programmer who uses the class Objects and References: Outline Variables of a Class Type Defining an equals Method for a Class Boolean-Valued Methods Parameters of a Class Type 16

17 Variables of a Class Type All variables are implemented as a memory location Data of primitive type stored in the memory location assigned to the variable Variable of class type contains memory address of object named by the variable Variables of a Class Type Object itself not stored in the variable Stored elsewhere in memory Variable contains address of where it is stored Address called the reference to the variable A reference type variable holds references (memory addresses) This makes memory management of class types more efficient Variables of a Class Type Figure 5.5a Behavior of class variables 17

18 Variables of a Class Type Figure 5.5b Behavior of class variables Variables of a Class Type Figure 5.5c Behavior of class variables Variables of a Class Type Figure 5.5d Behavior of class variables 18

19 Variables of a Class Type Figure 5.6a Dangers of using == with objects Variables of a Class Type Figure 5.6b Dangers of using == with objects Defining an equals Method As demonstrated by previous figures We cannot use == to compare two objects We must write a method for a given class which will make the comparison as needed View sample code, listing 5.17 class Species The equals for this class method used same way as equals method for String 19

20 Demonstrating an equals Method View sample program, listing 5.18 class SpeciesEqualsDemo Note difference in the two comparison methods == versus.equals( ) Sample screen output Complete Programming Example View sample code, listing 5.19 class Species Figure 5.7 Class Diagram for the class Species in listing 5.19 Boolean-Valued Methods Methods can return a value of type boolean Use a boolean value in the return statement Note method from listing

21 Unit Testing A methodology to test correctness of individual units of code Typically methods, classes Collection of unit tests is the test suite The process of running tests repeatedly after changes are make sure everything still works is regression testing View sample code, listing 5.20 class SpeciesTest Parameters of a Class Type When assignment operator used with objects of class type Only memory address is copied Similar to use of parameter of class type Memory address of actual parameter passed to formal parameter Formal parameter may access public elements of the class Actual parameter thus can be changed by class methods Classes have Summary Instance variables to store data Method definitions to perform actions Instance variables should be private Class needs accessor, mutator methods Methods may be Value returning methods Void methods that do not return a value 21

22 Summary Keyword this used within method definition represents invoking object Local variables defined within method definition Formal arguments must match actual parameters with respect to number, order, and data type Formal parameters act like local variables Summary Parameter of primitive type initialized with value of actual parameter Value of actual parameter not altered by method Parameter of class type initialized with address of actual parameter object Value of actual parameter may be altered by method calls A method definition can include call to another method in same or different class Summary Precondition comment states conditions that must be true before method invoked Postcondition comment describes resulting effects of method execution Utility program javadoc creates documentation Class designers use UML notation to describe classes Operators = and == behave differently with objects of class types (vs. primitive types) 22

23 Summary Designer of class should include an equals method Graphics drawn by applet normally done from within paint Other applet instructions placed in init Parameter of paint is of type Graphics Method setbackground sets color of applet pane Labels added to content pane within the init method 23

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09 Objectives Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters

More information

Defining Classes and Methods

Defining Classes and Methods Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe concepts of class, class

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe and define concepts of class, class object Describe use

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! Describe and define concepts of class and object! Describe use

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Information Hiding, Encapsulation: Outline Information Hiding Pre- and Postcondition Comments The public and private Modifiers Methods Calling Methods Encapsulation

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5b Information Hiding Programmer using a class method need not know details of implementation Only needs to know what the method does Information hiding: Designing

More information

Defining Classes and Methods

Defining Classes and Methods Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Class and Method Definitions: Outline Class Files and Separate Compilation Instance Variables Methods The Keyword this Local Variables

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Objects and References: Outline Variables of a Class Type Defining an equals Method for a Class Boolean-Valued Methods Parameters of a Class Type Variables of a Class

More information

Methods and Data (Savitch, Chapter 5)

Methods and Data (Savitch, Chapter 5) Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private 2 public class Temperature { public static void main(string[] args) {

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 4 Chapter 4 1 Basic Terminology Objects can represent almost anything. A class defines a kind of object. It specifies the kinds of data an object of the class can have.

More information

EECS168 Exam 3 Review

EECS168 Exam 3 Review EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any

More information

Methods. Methods. Mysteries Revealed

Methods. Methods. Mysteries Revealed Methods Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private A method (a.k.a. func2on, procedure, rou2ne) is a piece of code

More information

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Recitation 02/02/07 Defining Classes and Methods. Chapter 4 Recitation 02/02/07 Defining Classes and Methods 1 Miscellany Project 2 due last night Exam 1 (Ch 1-4) Thursday, Feb. 8, 8:30-9:30pm PHYS 112 Sample Exam posted Project 3 due Feb. 15 10:00pm check newsgroup!

More information

Chapter 4 Defining Classes I

Chapter 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 information

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015 COMP 110-001 Information Hiding and Encapsulation Yi Hong June 03, 2015 Review of Pass-By-Value What is the output? public void swap(student s1, Student s2) Student s3 = s1; s1 = s2; s2 = s3; Student berkeley

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. Before you can drive a car, someone has to design it and build it. A car typically begins as engineering

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology Computer Science II OO Programming Classes Scott C Johnson Rochester Institute of Technology Outline Object-Oriented (OO) Programming Review Initial Implementation Constructors Other Standard Behaviors

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

Chapter 4. Defining Classes I

Chapter 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 information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters) Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Chapter 6 Introduction to Defining Classes

Chapter 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 information

Classes. Classes as Code Libraries. Classes as Data Structures

Classes. Classes as Code Libraries. Classes as Data Structures Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They

More information

Chapter 4. Defining Classes I

Chapter 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 dfii defining a number of

More information

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been

More information

CS112 Lecture: Defining Instantiable Classes

CS112 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 information

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Array 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 information

Objects and Classes Lecture 2

Objects and Classes Lecture 2 Objects and Classes Lecture 2 Waterford Institute of Technology January 12, 2016 John Fitzgerald Waterford Institute of Technology, Objects and ClassesLecture 2 1/32 Classes and Objects Example of class

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

How to engineer a class to separate its interface from its implementation and encourage reuse.

How to engineer a class to separate its interface from its implementation and encourage reuse. 1 3 Introduction to Classes and Objects 2 OBJECTIVES In this chapter you ll learn: What classes, objects, member functions and data members are. How to define a class and use it to create an object. How

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College August 29, 2018 Outline Outline 1 Chapter 2: Data Abstraction Outline Chapter 2: Data Abstraction 1 Chapter 2: Data Abstraction

More information

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

Introduction to Computers and Java

Introduction to Computers and Java Introduction to Computers and Java Chapter 1 Chapter 1 1 Objectives overview computer hardware and software introduce program design and object-oriented programming overview the Java programming language

More information

Defensive Programming

Defensive Programming Defensive Programming Software Engineering CITS1220 Based on the Java1200 Lecture notes by Gordon Royle Lecture Outline Why program defensively? Encapsulation Access Restrictions Documentation Unchecked

More information

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1 OBJECT ORIENTED SIMULATION LANGUAGE OOSimL Reference Manual - Part 1 Technical Report TR-CSIS-OOPsimL-1 José M. Garrido Department of Computer Science Updated November 2014 College of Computing and Software

More information

A JavaBean is a class file that stores Java code for a JSP

A JavaBean is a class file that stores Java code for a JSP CREATE A JAVABEAN A JavaBean is a class file that stores Java code for a JSP page. Although you can use a scriptlet to place Java code directly into a JSP page, it is considered better programming practice

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Lecture 7: Classes and Objects CS2301

Lecture 7: Classes and Objects CS2301 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations. Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn

More information

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 4: Writing Classes Objects An object has: Presentation slides for state - descriptive characteristics Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus,

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

System.out.print(); Scanner.nextLine(); String.compareTo();

System.out.print(); Scanner.nextLine(); String.compareTo(); System.out.print(); Scanner.nextLine(); String.compareTo(); Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 6 A First Look at Classes Chapter Topics 6.1 Objects and

More information

8. Polymorphism and Inheritance

8. Polymorphism and Inheritance 8. Polymorphism and Inheritance Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe polymorphism and inheritance in general Define interfaces

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 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 information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java expressions and operators concluded Java Statements: Conditionals: if/then, if/then/else Loops: while, for Next

More information

CPS122 Lecture: Defining a Class

CPS122 Lecture: Defining a Class Objectives: CPS122 Lecture: Defining a Class last revised January 14, 2016 1. To introduce structure of a Java class 2. To introduce the different kinds of Java variables (instance, class, parameter, local)

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

https://asd-pa.perfplusk12.com/admin/admin_curric_maps_display.aspx?m=5507&c=618&mo=18917&t=191&sy=2012&bl...

https://asd-pa.perfplusk12.com/admin/admin_curric_maps_display.aspx?m=5507&c=618&mo=18917&t=191&sy=2012&bl... Page 1 of 13 Units: - All - Teacher: ProgIIIJavaI, CORE Course: ProgIIIJavaI Year: 2012-13 Intro to Java How is data stored by a computer system? What does a compiler do? What are the advantages of using

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Computer Programming C++ Classes and Objects 6 th Lecture

Computer Programming C++ Classes and Objects 6 th Lecture Computer Programming C++ Classes and Objects 6 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved Outline

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

CSC207 Week 2. Larry Zhang

CSC207 Week 2. Larry Zhang CSC207 Week 2 Larry Zhang 1 Today s Outline Finish up non-oo examples, Array, HashMap OO programming Unit Test: JUnit (if time permits) Javadoc (if time permits) 2 Recap Java code is compiled into bytecode

More information

Dot and Scope Resolution Operator

Dot and Scope Resolution Operator Dot and Scope Resolution Operator Used to specify "of what thing" they are members Dot operator: Specifies member of particular object Scope resolution operator: Specifies what class the function definition

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array Arrays Chapter 7 Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array as an instance variable Use an array not filled

More information

3.1 Class Declaration

3.1 Class Declaration Chapter 3 Classes and Objects OBJECTIVES To be able to declare classes To understand object references To understand the mechanism of parameter passing To be able to use static member and instance member

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

CST141 Thinking in Objects Page 1

CST141 Thinking in Objects Page 1 CST141 Thinking in Objects Page 1 1 2 3 4 5 6 7 8 Object-Oriented Thinking CST141 Class Abstraction and Encapsulation Class abstraction is the separation of class implementation from class use It is not

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

VARIABLES AND TYPES CITS1001

VARIABLES AND TYPES CITS1001 VARIABLES AND TYPES CITS1001 Scope of this lecture Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Primitive types Every piece of data

More information

Introduction to Classes

Introduction to Classes Introduction to Classes Procedural and Object-Oriented Programming Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented

More information

Inheritance. Chapter 7. Chapter 7 1

Inheritance. Chapter 7. Chapter 7 1 Inheritance Chapter 7 Chapter 7 1 Introduction to Inheritance Inheritance allows us to define a general class and then define more specialized classes simply by adding new details to the more general class

More information

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited Table of Contents Date(s) Title/Topic Page #s 11/6 Chapter 3 Reflection/Corrections 56 Chapter 4: Writing Classes 4.1 Objects Revisited 57 58-59 look over your Ch 3 Tests and write down comments/ reflections/corrections

More information

BBM 102 Introduc0on to Programming II Spring 2014

BBM 102 Introduc0on to Programming II Spring 2014 BBM 102 Introduc0on to Programming II Spring 2014 Encapsula0on Instructors: Fuat Akal, Nazlı İkizler Cinbiş, Oğuz Aslantürk TAs: Ahmet Selman Bozkır, Gültekin Işık, Levent Karacan 1 Today Informa0on Hiding

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Object Oriented Programming

Object 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 information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

Design Patterns: State, Bridge, Visitor

Design Patterns: State, Bridge, Visitor Design Patterns: State, Bridge, Visitor State We ve been talking about bad uses of case statements in programs. What is one example? Another way in which case statements are sometimes used is to implement

More information

Expressions and Casting

Expressions and Casting Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable

More information

More About Objects and Methods

More About Objects and Methods More About Objects and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! learn more techniques for programming with classes and objects!

More information

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich. More About Objects and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! learn more techniques for programming with classes and objects!

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1 C How to Program, 6/e 1 Structures : Aggregate data types are built using elements of other types struct Time { int hour; int minute; Members of the same structure must have unique names. Two different

More information

Chapter 3 - Introduction to Java Applets

Chapter 3 - Introduction to Java Applets 1 Chapter 3 - Introduction to Java Applets 2 Introduction Applet Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language)

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Classes & Objects DDU Design Constructors Member Functions & Data Friends and member functions Const modifier Destructors Object -- an encapsulation of data

More information

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 6 Structures and Classes 1 Learning Objectives Structures Structure types Structures as function arguments Initializing structures Classes Defining, member functions Public and private members

More information

Express Yourself. Writing Your Own Classes

Express Yourself. Writing Your Own Classes Java Programming 1 Lecture 5 Defining Classes Creating your Own Classes Express Yourself Use OpenOffice Writer to create a new document Save the file as LastFirst_ic05 Replace LastFirst with your actual

More information

Week 7 - More Java! this stands for the calling object:

Week 7 - More Java! this stands for the calling object: Week 7 - More Java! Variable Scoping, Revisited this Parameter Encapsulation & Principles of Information Hiding: Use of public and private within class API, ADT javadoc Variables of class Type Wrapper

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005 Lecture 8 Classes and Objects Part 2 MIT AITI June 15th, 2005 1 What is an object? A building (Strathmore university) A desk A laptop A car Data packets through the internet 2 What is an object? Objects

More information

Outline. Outline. 1 Chapter 2: Data Abstraction

Outline. Outline. 1 Chapter 2: Data Abstraction Outline Outline 1 Chapter 2: Data Abstraction From Data Type to ADT Values A value is a unit of information used in a program. It can be associated with a constant or variable (a name) by an assignment

More information

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009 CMPT 117: Tutorial 1 Craig Thompson 12 January 2009 Administrivia Coding habits OOP Header Files Function Overloading Class info Tutorials Review of course material additional examples Q&A Labs Work on

More information

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

Key Differences Between Python and Java

Key Differences Between Python and Java Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively.

More information