Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Programming Languages and Paradigms The C Programming Language.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Lilian Blot BUILDING CLASSES Java Programming Spring 2014 TPOP 1.
ECE122 Feb Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
CMSC 202 Classes and Objects: Reusing Classes with Composition.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Classes and Objects: The Basics. Version 9/09 2 Programming & Abstraction All programming languages provide some form of abstraction. –Also called.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CMSC 202 Java Classes and Object 2nd Lecture. Aug 6, Stack and Heap When your program is running, some memory is used to store local variables.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
CMSC 202 CMSC 202, Advanced Section Classes and Objects In Java.
CIS162AD Constructors Part 2 08_constructors.ppt.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Class Fundamentals BCIS 3680 Enterprise Programming.
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Creating Your Own Classes
Classes and Objects: Encapsulation
AKA the birth, life, and death of variables.
Classes and Objects 2nd Lecture
Defining Classes II.
Classes and Objects: Encapsulation
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Objects Encapsulation
Inheritance.
Java Classes and Objects 3rd Lecture
Defining Classes I Part A.
Java Classes and Objects
CMSC 202 Classes and Objects.
Class.
Classes, Objects and Methods
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
CMSC 202 Encapsulation Version 9/10.
CMSC 202 Constructors Version 9/10.
Object-Oriented Programming and class Design
Presentation transcript:

Defining Classes I Part A

Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in use today. Classes are the most important language feature of OOP. Classes are the most important language feature of OOP. Instance (value) of the class = a particular object (a particular value of a class type) Instance (value) of the class = a particular object (a particular value of a class type) A class is a type and you can declare variables of a class type. A class is a type and you can declare variables of a class type.

Class definitions Attributes, fields, or properties = data Attributes, fields, or properties = data Methods = function Methods = function Members = data + functions (or attributes + methods) Members = data + functions (or attributes + methods)

Example class Consider dates such as December 31, 2007 and July 4, Consider dates such as December 31, 2007 and July 4, We would like to develop a class to represent dates. We would like to develop a class to represent dates. public class Date { } What’s our next step? What’s our next step?

Example class What attributes (data) do we need? What attributes (data) do we need? To represent the month? To represent the month? To represent the day? To represent the day? To represent the year? To represent the year?

Example class public class Date { public StringmMonth; public intmDay; public intmYear; } What does “public” mean for the month? What does “public” mean for the month?

Conventions (mandatory) 1. One class per file. File name is always classname.java. 2. What does “public” mean for the month? 3. Let’s adopt the convention that all class names will begin with an uppercase letter. 4. Let’s adopt the convention that all attributes will begin with ‘m’ (to distinguish them from local variables).

Example class public class Date { public StringmMonth; public intmDay; public intmYear; } How do we declare an object of type Date? How do we declare an object of type Date? How do we create an instance of this class? How do we create an instance of this class?

Example class public class Date { public StringmMonth; public intmDay; public intmYear; } How do we declare an object of type Date? How do we declare an object of type Date? Rule of Date dt; How do we create an instance of this class? How do we create an instance of this class? dt = new Date(); Date d2 = new Date(); //both ops

Class definition In general, a class definition is of the form: In general, a class definition is of the form: public class Class_Name { Instance_Variable_Declaration_1Instance_Variable_Declaration_2…Instance_Variable_Declaration_LastMethod_Definition_1Method_Definition_2…Method_Definition_Last}

Example class A common operation is to ask the object to create a string that represents it values. We’d like to say: A common operation is to ask the object to create a string that represents it values. We’d like to say: Date dt = new Date(); System.out.println( “date is “ + dt.toString() ); What must we add to Date to make this happen? What must we add to Date to make this happen?

Example class public class Date { public StringmMonth; public intmDay; public intmYear; public String toString ( ) { return mMonth + “ “ + mDay + “, “ + mYear; }}

Methods in general (aka function, procedures) (aka function, procedures) Method definition vs. method invocation Method definition vs. method invocation May have an optional return statement to optionally return a value to the caller. May have an optional return statement to optionally return a value to the caller.

Methods in general Definition: Definition: public type_returned method_name ( optional_parameter_list ) { //body of method (containing local var definitions and other // statements. //if type_returned is not void, there must be at least one return // statement that returns a value to the caller. }

Example methods public int getDay ( ) { return mDay; } /** yet another overloaded version of setDate with * a single parameter of type int * a single parameter of type int */ */ public void setDate ( int year ) { setDate(1, 1, year); setDate(1, 1, year);}

Return statement The return statement exits the current function and optionally returns a value to the caller. The return statement exits the current function and optionally returns a value to the caller. public int getDay ( ) { return mDay; }… int d = dt.getDay();

Returning boolean Consider the following: Consider the following: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; }

Retuning boolean Consider the following: Consider the following: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; } public boolean checkRange2 ( int i ) { return (1<=i && i<=6); }

Types of variables Local Local Declared w/in methods Declared w/in methods Function parameters can be thought of as local variables Function parameters can be thought of as local variables Class Class Declared w/in a class Declared w/in a class Not declared w/in any method Not declared w/in any method We will adopt the convention of starting all of these class variables with m. We will adopt the convention of starting all of these class variables with m. Ex. String mMonth; Ex. String mMonth;

The this parameter Every (non static) method has an implicit, pre- defined parameter called this. Every (non static) method has an implicit, pre- defined parameter called this. this can be used to refer to class variables or methods. this can be used to refer to class variables or methods. this is a keyword. this is a keyword. this refers to the current object instance. this refers to the current object instance.

When do we use this? When a function parameter (or local variable) name masks a class variable name. When a function parameter (or local variable) name masks a class variable name. But our convention avoids this situation in most cases. So you shouldn’t need to use this very often. But our convention avoids this situation in most cases. So you shouldn’t need to use this very often. But it doesn’t hurt: But it doesn’t hurt: public int getDay ( ) { return this.mDay; }

When we must use this int where = 12; public void foo ( int where ) { where = 92; } Did the class variable called where change to 92?

When we must use this int where = 12; public void foo ( int where ) { this.where = 92; where = 7; }

Determine if two dates are equal Recall what happened when we used == to test if two strings are equal. Recall what happened when we used == to test if two strings are equal. What method did we use? What method did we use? Let’s write our own for dates. Let’s write our own for dates.

Determine if two dates are equal public boolean equals ( Date other ) { return ( mDay == other.mDay && mMonth.equals( other.mMonth ) && mYear==mYear ); }

Function to determine if one date precedes another date Note that December 31, 1970 precedes January 1, Note that December 31, 1970 precedes January 1, Note that December 31, 1971 does not precede January 1, Note that December 31, 1971 does not precede January 1, Note that January 1, 1970 does not precede January 1, Note that January 1, 1970 does not precede January 1, Write a function to determine if one date precedes another date. Write a function to determine if one date precedes another date.

public boolean precedes ( Date other ) { return ( ( mYear<other.mYear) || (mYear==other.mYear && getMonth()<other.getMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) && mDay<other.mDay) ); } I personally would never write it this way!

public boolean precedes ( Date other ) { return ( ( mYear<other.mYear) || (mYear==other.mYear && getMonth()<other.getMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth()) && mDay<other.mDay) ); } public boolean precedes2 ( Date other ) { if (mYear < other.mYear)return true; //year must be the same if (getMonth() < other.getMonth())return true; //year and month must be the same return (mDay<other.mDay); }

Testing Use a separate program called a driver to test the classes that you develop. Use a separate program called a driver to test the classes that you develop. Drivers typically have a main. Your classes should not. Drivers typically have a main. Your classes should not.