CS1004: Intro to CS in Java, Spring 2005

Similar documents
Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Chapter 4: Writing Classes

Anatomy of a Class Encapsulation Anatomy of a Method

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

ECE 122. Engineering Problem Solving with Java

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

CS1004: Intro to CS in Java, Spring 2005

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Chapter 5: Writing Classes and Enums

ECE 122. Engineering Problem Solving with Java

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

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

Where do objects come from? Good question!

Assignment 1 due Monday at 11:59pm

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Designing Classes part 2

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

Chapter 6 Introduction to Defining Classes

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

ITI Introduction to Computing II

Programming II (CS300)

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

ITI Introduction to Computing II

Chapter 3 Objects and Classes

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

CS 302 Week 9. Jim Williams

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

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

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Chapter 4 Defining Classes I

Programming II (CS300)

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Programming II (CS300)

Software Design and Analysis for Engineers

CS 2530 INTERMEDIATE COMPUTING

Encapsulation. Mason Vail Boise State University Computer Science

Java Programming Lecture 7

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

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

Classes Classes 2 / 35

Inheritance and Polymorphism

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

Classes Classes 2 / 36

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

CS-202 Introduction to Object Oriented Programming

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

CS 251 Intermediate Programming Methods and More

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

Express Yourself. Writing Your Own Classes

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

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CS 251 Intermediate Programming Methods and Classes

Inheritance and Polymorphism

Chapter 15: Object Oriented Programming

CS112 Lecture: Defining Instantiable Classes

Object-Oriented Programming Concepts

An Introduction to C++

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

CS1083 Week 2: Arrays, ArrayList

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Recommended Group Brainstorm (NO computers during this time)

Data Structures (list, dictionary, tuples, sets, strings)

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

ECOM 2324 COMPUTER PROGRAMMING II

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

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

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

Recursion 1. Recursion is the process of defining something in terms of itself.

Defining Classes and Methods

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

CS112 Lecture: Working with Numbers

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Creating an object Instance variables

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

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

Programming II (CS300)

More About Objects and Methods

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

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Subclassing for ADTs Implementation

Principles of Object Oriented Programming. Lecture 4

Example: Fibonacci Numbers

CSC207 Week 3. Larry Zhang

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Programming in the Large II: Objects and Classes (Part 1)

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Example: Class MSDie Introduction to Graphs and NetworkX

EECS168 Exam 3 Review

Transcription:

CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a constructor has no return type specified in the method header, not even void A common error is to put a return type on a constructor, which makes it a regular method that happens to have the same name as the class The programmer does not have to define a constructor for a class Each class has a default constructor that accepts no parameters 1

Defining the Die class Goal: design the Die class with other data and methods to make it a versatile and reusable resource That said, it doesn t t mean a program has to use all the features of a class Let s s write out what a possible Die class might be An int that has the face value Methods to roll and set the die explicitly Methods to get info on the die s s current value DieRoller class Once we ve defined a Die,, we need to actually use it somehow We ll define a class called DieRoller,, in which we ll actually manipulate the die This is a common model Define one or more data classes Establish one or more program classes, with a main method Variables and scope As you may have guessed, there s s multiple places to put variables in your program (scope) At the class level (instance( variables) Inside a method (local( variables) Variables declared inside one method cannot be used in another method without being explicitly passed to it What happens when you declare a variable with the same name in two places? 2

The tostring Method All classes that represent objects should define a tostring method The tostring method returns a character string that represents the object in some way It is called automatically when an object is concatenated to a String or when it is passed to the println method UML Diagrams UML stands for the Unified Modeling Language UML diagrams show relationships among classes and objects A UML class diagram consists of one or more classes, each with sections for the class name, attributes (data), and operations (methods) Lines between classes represent associations A dotted arrow shows that one class uses the other (calls its methods) UML Class Diagrams A UML class diagram for our example We re not going to explore this too deeply, just enough for basic diagramming DieRoller main (args : String[]) : void value : int Die roll() : int setvalue (int value) : void getvalue() : int tostring() : String 3

Encapsulation We can take one of two views of an object: internal - the details of the variables and methods of the class that defines it external - the services that an object provides and how the object interacts with the rest of the system Box metaphor From the external view, an object is an encapsulated entity, providing a set of specific services These services define the interface to the object Object-oriented design One object (called the client) ) may use another object for the services it provides The client of an object may request its services (call its methods), but it should not have to be aware of how those services are accomplished Any changes to the object's state (its variables) should be made by that object's methods We should make it difficult, if not impossible, for a client to access an object s s variables directly Not a strict requirement, but generally considered good design Visibility Modifiers In Java, we accomplish encapsulation through the appropriate use of visibility modifiers A modifier specifies particular characteristics of a method or data (final( final) Java has three visibility modifiers: public, protected,, and private The protected modifier involves inheritance, which we will discuss later 4

Visibility Modifiers, cont d. Public visibility: can be referenced anywhere Private visibility: can be referenced only within that class No visibility modifier is default visibility,, and can be referenced by any class in the same package An overview of all Java modifiers is presented in Appendix E So what s s their preferred use? Visibility for Variables Public variables violate encapsulation because they allow the client to reach in and modify the values directly Therefore instance variables should generally not be declared with public visibility It is acceptable to give a constant public visibility although the client can access it, its value cannot be changed Visibility for Methods Methods that provide the object's services (service( methods) ) are declared with public visibility so that they can be invoked by clients Methods to assist service methods (support( methods) ) are not intended to be called by a client and should not be declared with public visibility 5

Visibility Modifiers: Summary public private Variables Violate encapsulation Enforce encapsulation Methods Provide services to clients Support other methods in the class Accessors and Mutators If you want to let a client access data in a class, provide accessor and mutator methods The names of accessor and mutator methods usually take the form getx and setx,, respectively, where X is the name of the value Sometimes called getters and setters The use of mutators gives the class designer the ability to restrict a client s s options to modify an object s s state For example, restrict setting the value of a Die to a valid range Enumerated Types If you re defining a class just to store one basic property, consider using an enumerated type instead An enumerated type establishes all possible values for a variable of that type; values are identifiers of your own choosing The following declaration creates an enumerated type called Season enum Season {winter, spring, summer, fall}; Any number of values can be listed Specify type of Die: enum DieType {weighted, fair}; No instantiation needed: DieType dt = DieType.weighted; 6

Ordinal Values Internally, each value of an enumerated type is stored as an integer, called its ordinal value The first value in an enumerated type has an ordinal value of zero, the second one, and so on However, you cannot assign a numeric value to an enumerated type, even if it corresponds to a valid ordinal value For type safety purposes The ordinal method returns the ordinal value of the object The name method returns the name of the identifier corresponding to the object's value Let s s do one more example Let s s create two geometric shapes, circle and square, and play with them briefly Finish GUIs Start chapter 5 of L/L Next time 7