Defining Classes I Part A.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
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.
Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in.
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) { }
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.
CMSC 202 CMSC 202, Advanced Section Classes and Objects In Java.
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.
Topic: Classes and Objects
Classes and Objects Introduced
Creating Your Own Classes
Classes and Objects: Encapsulation
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?
Class Structure 15-Jun-18.
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?
JavaScript Syntax and Semantics
Templates.
Classes and Objects 2nd Lecture
Lecture 11 B Methods and Data Passing
CMSC 202 Static Methods.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Defining Classes II.
Classes and Objects: Encapsulation
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Objects Encapsulation
Defining Classes and Methods
Chapter 6 Methods: A Deeper Look
Objects as Variables Featuring the Date Object
Class Structure 28-Nov-18.
Classes and Objects 5th Lecture
Inheritance.
Java Classes and Objects 3rd Lecture
Building Java Programs
Friends, Overloaded Operators, and Arrays in Classes
Classes.
Conditional Logic Presentation Name Course Name
Java Classes and Objects
CMSC 202 Classes and Objects.
Building Java Programs
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Classes and Objects Static Methods
Class.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Building Java Programs
Classes, Objects and Methods
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
CMSC 202 Encapsulation Version 9/10.
CMSC 202 Exceptions.
Classes and Objects Object Creation
CS 240 – Advanced Programming Concepts
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. Classes are the most important language feature of OOP. 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.

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

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

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

Example class public class Date { public String mMonth; public int mDay; public int mYear; } What does “public” mean for the month?

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

Example class public class Date { public String mMonth; public int mDay; public int mYear; } How do we declare an object of type Date? How do we create an instance of this class?

Example class public class Date { public String mMonth; public int mDay; public int mYear; } How do we declare an object of type Date? Date dt; How do we create an instance of this class? dt = new Date(); Date d2 = new Date(); //both ops at once

Class definition In general, a class definition is of the form: public class Class_Name { Instance_Variable_Declaration_1 Instance_Variable_Declaration_2 … Instance_Variable_Declaration_Last Method_Definition_1 Method_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: Date dt = new Date(); System.out.println( "date is " + dt.toString() ); What must we add to Date to make this happen?

Example class public class Date { public String mMonth; public int mDay; public int mYear; public String toString ( ) { return mMonth + " " + mDay + ", " + mYear; }

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

Methods in general 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; }

Example methods public void setDate ( String m, int d, int y ) { mMonth = m; mDay = d; mYear = y; } /** yet another overloaded version of setDate with * a single parameter of type int */ public void setDate ( int year ) { setDate( "january", 1, year );

Return statement 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: public boolean checkRange ( int i ) { if (1<=i && i<=6) return true; return false; }

Returning boolean 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 Class Declared w/in methods Function parameters can be thought of as local variables Class Declared w/in a class Not declared w/in any method We will adopt the convention of starting all of these class variables with m. Ex. String mMonth;

The this parameter Every (non static) method has an implicit, pre-defined parameter called this. this can be used to refer to class variables or methods. this is a keyword. 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. But our convention avoids this situation in most cases. So you shouldn’t need to use this very often. But it doesn’t hurt: public int getDay ( ) { return this.mDay; }

When we must use this? int where = 12; public void foo ( int where ) { } 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. What method did we use? 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 == other.mYear ); } equalsIgnoreCase might be good to use here.

A function that converts the string in mMonth to the corresponding int might be useful. public int convertMonth ( ) { // (again equalsIgnoreCase might be better) if (mMonth.equals("january")) return 1; if (mMonth.equals("february")) return 2; … return -1; }

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

I personally would never write it this way! public boolean precedes ( Date other ) { return ( ( mYear < other.mYear) || (mYear==other.mYear && convertMonth()<other.convertMonth()) || (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 && convertMonth()<other.convertMonth()) || (mYear==other.mYear && mMonth.equals(other.mMonth) && mDay<other.mDay) ); } public boolean precedes2 ( Date other ) { if (mYear < other.mYear) return true; if (mYear > other.mYear) return false; //year must be the same if (convertMonth() < other.convertMonth()) return true; if (mMonth.equals(other.mMonth) && mDay < other.mDay) return true; return false;

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