Classes. Classes as Code Libraries. Classes as Data Structures

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

Methods and Data (Savitch, Chapter 5)

Methods. Methods. Mysteries Revealed

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

5. Defining Classes and Methods

CS 251 Intermediate Programming Methods and More

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

Lecture Static Methods and Variables. Static Methods

method method public class Temperature { public static void main(string[] args) { // your code here } new

Principles of Object Oriented Programming. Lecture 4

Programming II (CS300)

Defining Classes and Methods

Programming II (CS300)

CS 251 Intermediate Programming Methods and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Lecture Static Methods and Variables. Static Methods

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

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

5. Defining Classes and Methods

Defining Classes and Methods

Defining Classes and Methods

Defining Classes and Methods

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

by Pearson Education, Inc. All Rights Reserved. 2

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

Object Oriented Programming

CS112 Lecture: Defining Instantiable Classes

Programming II (CS300)

Objects as a programming concept

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Object Oriented Programming in C#

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Object Oriented Programming

CS 2530 INTERMEDIATE COMPUTING

CS 302 Week 9. Jim Williams

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Fundamentos de programação

Ch 7 Designing Java Classes & Class structure

CS110: PROGRAMMING LANGUAGE I

Software and Programming 1

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS1083 Week 2: Arrays, ArrayList

Midterms Save the Dates!

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

This exam is open book. Each question is worth 3 points.

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Object-Oriented Programming Concepts

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

Encapsulation in Java

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Introduction to Java. Handout-1d. cs402 - Spring

Programming Languages: Encapsulation

MIDTERM REVIEW. midterminformation.htm

CIS 110: Introduction to Computer Programming

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Learning objec<ves. Classes, Objects, and Methods. Problem Decomposi<on. Problem Decomposi<on (cont d)

Java and OOP. Part 2 Classes and objects

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Recitation 3 Class and Objects

CS1004: Intro to CS in Java, Spring 2005

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

Chapter 4 Defining Classes I

Chapter 5 Methods / Functions

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Midterms Save the Dates!

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Exam Percentage: / 55 = %

Assignment 1 due Monday at 11:59pm

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

JAVA Programming Concepts

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Chapter 7 Classes & Objects, Part B

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

Preview from Notesale.co.uk Page 3 of 36

Programming II (CS300)

Classes and Methods: Classes

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CS111: PROGRAMMING LANGUAGE II

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Java Magistère BFA

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Software and Programming 1

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

Introduction to Programming Using Java (98-388)

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Chapter 6 Methods. Dr. Hikmat Jaber

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CS1004: Intro to CS in Java, Spring 2005

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Chapter 4: Writing Classes

Week 11: Class Design

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

AP Computer Science Unit 1. Programs

Lecture 5: Methods CS2301

Transcription:

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 encapsulate functionality to form powerful abstractions of real world objects. What can classes be used for? Classes have many different uses, for example: Data Structures Code Libraries Java Programs Complex Objects CS 160, Spring Semester 2016 2 Classes as Data Structures Just like a struct in C and C++ (no code), for example: public class Student { public String firstname; public String lastname; public Date birthdate; public Address homeaddress; public double gradepointaverage; CS 160, Spring Semester 2016 3 Classes as Code Libraries Just like a library in a procedural language (no data) like C or C++, for example: public class Math { public static final double PI = 3.14159; public static double sin(double a) { public static double exp(double a) { public static double log(double a) { public static double sqrt(double a) { CS 160, Spring Semester 2016 4 1

Classes as Small Programs Just like a program in a procedural language like C or C++, for example: public class MySmallProgram{ public static void main(string args[]) { System.out.println( Hello, World! ); CS 160, Spring Semester 2016 5 Classes as Large Programs Just like a program in a procedural language like C or C++, for example: public class MyLargeProgram{ // lots of data public static void main(string args[]) { // lots of code // lots of methods CS 160, Spring Semester 2016 6 Classes as Complex Objects Using Different Class Types No comparable example in a procedural language like C or Pascal! public class MyClass { // class variables (static) // instance variables (non-static) // no main method // class methods (static) // instance methods (non-static) CS 160, Spring Semester 2016 7 // Data Structure Student students[] = new Student[100]; students[0].firstname = Christopher ; // Code Library System.out.println(Math.sin(1.0)); // Java Programs $ java MySmallProgram // Complex Objects MyClass myclass = new MyClass(); myclass.initialize(); CS 160, Spring Semester 2016 8 2

public Public versus Private Can access the class, method, or data by name outside defining class private Can access the class, method, or data by name only inside defining class Classes generally specified as public Instance variables usually are private Methods can be public or private CS 160, Spring Semester 2016 9 Static (versus non-static) Local data resides in a method, class and instance data resides in the class. Data defined in the class can be of two types: Data may belong to the class (only one copy that has the same value for all objects of that class) Data may belong to the object (separate storage and different values for each instance) Class data is identified by static Instance data is not be static CS 160, Spring Semester 2016 10 Class versus Instance: Data public class MyClass { static int int0; double real0; int0 resides in the class (1 copy), use class name to access: MyClass.int0 = 123; real0 resides in an instance (many copies): MyClass m = new MyClass; m.real0 = 1.234; CS 160, Spring Semester 2016 11 Class Methods Notation: class.method() Does not require instantiation, should be called using the class name. Class name is not necessary if within the class itself. Marked as static, and cannot access instance data, since no instance is used to call them. Useful for library methods that do not need any associated data: for example Math.sin(); CS 160, Spring Semester 2016 12 3

Instance Methods Notation: objectname.method() Must be called on an object instantiated from a class. Most objects are instantiated with the new keyword: String word = new String( Whatever ); For example, calling word.length() requires an object of type String called word. The length() method uses the data for the specific instance it is called on. Constructors Q: How to initialize instance data when creating an object? A: Use a constructor. Constructors are used to create objects and send them data. Always named after the class, arbitrary parameters, but no return value! Example: Scanner s = new Scanner(System.in); CS 160, Spring Semester 2016 13 CS 160, Spring Semester 2016 14 Methods inside a class Order of writing methods is arbitrary Generally constructors are written first. What if two methods need to share data? Can pass data around as parameters and return values. Alternative: specifying class or instance data allows access from multiple methods. Encapsulation Consider example of driving a car We see and use break pedal, accelerator pedal, steering wheel we know what they do. We do not see mechanical details of how they do their jobs. We can divide class definition into different parts to support encapsulation: Class interface Class implementation CS 160, Spring Semester 2016 15 CS 160, Spring Semester 2016 16 4

Interface/Implementation A class interface Tells t he us er or programmer 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, i.e. how the class does what it does Interfaces Interface definition is pure functionality, a set of methods and a description of what they do (Javadoc). Instance variables in the class usually are declared as private to prevent direct access. Provide public accessor (getter) methods to read data. Provide public mutator (setter) methods to write data. Add comments before each public method heading that fully specifies how to use method. Hide everything else: data structures, helper methods, algorithms - all implementation details. CS 160, Spring Semester 2016 17 CS 160, Spring Semester 2016 18 Interface Example Encapsulation/Interfaces public interface Trigonometry { public double sin(double angle); public double cos(double angle); public double tan(double angle); public class Math implements Trigonometry { public double sin(double angle) { code public double cos(double angle) { code public double tan(double angle) { code Trigonometry trig = new Math(); A well encapsulated class definition: Programmer who uses the class CS 160, Spring Semester 2016 19 CS 160, Spring Semester 2016 20 5

Why Interfaces? Interfaces are pure functionality, a designer can specify what the software does without worrying about how. Provides a methodology for partitioning large systems without having to consider all of the details. Interfaces can (and should) be designed based on user requirements and the intended functionality. Interfaces hide implementation details, user of class does not need to see internal code or data structures. Allows shipment of class files to protect intellectual property while still communicating functionality. CS 160, Spring Semester 2016 21 6